Skip to content

Commit 46497a9

Browse files
authored
Merge pull request #350 from machow/fix-interlinks-cross-site
docs: use fast interlinks
2 parents 79e11e2 + ad6566a commit 46497a9

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

docs/_quarto.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ project:
55
- examples/single-page
66
- examples/pkgdown
77
- examples/auto-package
8+
- objects.txt
9+
- objects-test.txt
810

911
metadata-files:
1012
- api/_sidebar.yml
@@ -13,11 +15,16 @@ filters:
1315
- "interlinks"
1416

1517
interlinks:
18+
fast: true
1619
sources:
1720
python:
1821
url: https://docs.python.org/3/
1922
griffe:
2023
url: https://mkdocstrings.github.io/griffe/
24+
qd2:
25+
url: https://pr-350--quartodoc.netlify.app/
26+
inv: objects-test.txt
27+
2128

2229
website:
2330
title: "quartodoc"

docs/get-started/interlinks.qmd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,22 @@ interlinks:
4343
url: https://numpy.org/doc/stable/
4444
python:
4545
url: https://docs.python.org/3/
46+
quartodoc-test:
47+
url: https://machow.github.io/quartodoc
48+
inv: objects-test.txt
4649
```
4750
4851
Notice 2 important pieces in this config:
4952
5053
* The `numpy` and `python` fields indicate that we're getting inventories for the
5154
library numpy, and python builtin libraries.
5255
* The `url` fields indicate where inventory files can be found.
56+
* The `inv` field lets you specify the name of the inventory file. By default, it assumes its `objects.inv`.
5357

5458
By default, downloaded inventory files will be saved in the `_inv` folder of your
5559
documentation directory.
5660

61+
5762
### Experimental fast option
5863

5964
Use the experimental `fast: true` option to speed up the interlinks filter.
@@ -175,6 +180,9 @@ Most sphinx sites name them `object.inv`:
175180
See the [sphobjinv docs](https://sphobjinv.readthedocs.io/en/stable/) for thorough
176181
details on these files, and how they're used in sphinx.
177182

183+
:::{.callout-note}
184+
[objects-test.txt](https://github.com/machow/quartodoc/tree/main/docs/objects-test.txt) is an example file with one entry: [](`qd2.Auto`).
185+
:::
178186

179187
## More information
180188

docs/objects-test.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Sphinx inventory version 2
2+
# Project: quartodoc
3+
# Version: 0.0.9999
4+
# The remainder of this file is compressed using zlib.
5+
qd2.Auto py:class 1 api/Auto.html#quartodoc.Auto -

quartodoc/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from watchdog.events import PatternMatchingEventHandler
1313
from quartodoc import Builder, convert_inventory
1414
from ._pydantic_compat import BaseModel
15+
from .interlinks import inventory_from_url
1516

1617

1718
def get_package_path(package_name):
@@ -258,7 +259,8 @@ def interlinks(config, dry_run, fast):
258259
continue
259260

260261
url = v["url"] + v.get("inv", "objects.inv")
261-
inv = soi.Inventory(url=url)
262+
263+
inv = inventory_from_url(url)
262264

263265
p_dst = p_root / cache / f"{k}_objects"
264266
p_dst.parent.mkdir(exist_ok=True, parents=True)

quartodoc/interlinks.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
loaded specification.
1010
"""
1111

12-
1312
from __future__ import annotations
1413

1514
import os
1615
import itertools
1716
import json
17+
import requests
18+
import sphobjinv
1819
import warnings
1920
import yaml
2021

@@ -38,6 +39,35 @@ class InvLookupError(Exception):
3839
"""An error looking up an entry from inventory files."""
3940

4041

42+
# Save inventories from url ---------------------------------------------------
43+
# Note that this is the one piece used by quartodoc (whereas everything else
44+
# in this module is a model of the lua filter behavior).
45+
46+
47+
def inventory_from_url(url: str) -> sphobjinv.Inventory:
48+
"""Return an inventory file by fetching from a url.
49+
50+
Use the prefix file:// to load the file from disk.
51+
"""
52+
53+
if url.startswith("file://"):
54+
with open(url.replace("file://", "", 1), "rb") as f:
55+
raw_content = f.read()
56+
else:
57+
r = requests.get(url)
58+
r.raise_for_status()
59+
raw_content = r.content
60+
61+
if url.endswith(".inv"):
62+
inv = sphobjinv.Inventory(zlib=raw_content)
63+
elif url.endswith(".txt"):
64+
inv = sphobjinv.Inventory(plaintext=raw_content)
65+
else:
66+
raise NotImplementedError("Inventories must be .txt or .inv files.")
67+
68+
return inv
69+
70+
4171
# Utility functions -----------------------------------------------------------
4272

4373

quartodoc/tests/test_interlinks.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import sphobjinv
23
import pytest
34
import yaml
45

@@ -10,6 +11,7 @@
1011
TestSpecEntry,
1112
parse_md_style_link,
1213
Link,
14+
inventory_from_url,
1315
)
1416
from importlib_resources import files
1517

@@ -90,3 +92,26 @@ def test_spec_entry(invs: Inventories, entry: TestSpecEntry):
9092
assert entry.output_text == el.content
9193
elif entry.output_element:
9294
assert el == entry.output_element
95+
96+
97+
def test_inventory_from_url_local_roundtrip(tmp_path):
98+
inv = sphobjinv.Inventory()
99+
inv.project = "abc"
100+
inv.version = "0.0.1"
101+
102+
soi_items = [
103+
sphobjinv.DataObjStr(
104+
name="foo", domain="py", role="method", priority="1", uri="$", dispname="-"
105+
)
106+
]
107+
inv.objects.extend(soi_items)
108+
109+
text = inv.data_file()
110+
sphobjinv.writebytes(tmp_path / "objects.txt", text)
111+
sphobjinv.writebytes(tmp_path / "objects.inv", sphobjinv.compress(text))
112+
113+
res1 = inventory_from_url("file://" + str(tmp_path / "objects.txt"))
114+
res2 = inventory_from_url("file://" + str(tmp_path / "objects.inv"))
115+
116+
assert isinstance(res1, sphobjinv.Inventory)
117+
assert isinstance(res2, sphobjinv.Inventory)

0 commit comments

Comments
 (0)