Skip to content

Commit a78f337

Browse files
committed
Fix links from docs in folders (#15)
1 parent 76e2b4f commit a78f337

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

docs/src/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ sphinx-codeautolink adheres to
1111
Unreleased
1212
----------
1313
- Correctly filter out names from concatenated sources (:issue:`14`)
14+
- Fix links in documents inside folder (:issue:`15`)
1415

1516
0.1.0 (2021-09-22)
1617
------------------

src/sphinx_codeautolink/extension/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def apply_links(self, app, exception):
8383
if self.do_nothing or exception is not None:
8484
return
8585

86-
uri = app.builder.outdir
87-
inv_file = posixpath.join(uri, INVENTORY_FILENAME)
86+
inv_file = posixpath.join(app.outdir, INVENTORY_FILENAME)
8887
if not Path(inv_file).exists():
8988
msg = (
9089
'sphinx-codeautolink: cannot locate object inventory '
@@ -93,16 +92,16 @@ def apply_links(self, app, exception):
9392
warn(msg, RuntimeWarning)
9493
return
9594

96-
inv = fetch_inventory(app, uri, inv_file)
95+
inv = fetch_inventory(app, app.outdir, inv_file)
9796
inter_inv = InventoryAdapter(app.env).main_inventory
98-
transposed = transpose_inventory(inter_inv, relative_to=uri)
99-
transposed.update(transpose_inventory(inv, relative_to=uri))
97+
transposed = transpose_inventory(inter_inv, relative_to=app.outdir)
98+
transposed.update(transpose_inventory(inv, relative_to=app.outdir))
10099

101100
for visitor in self.block_visitors:
102101
if not visitor.source_transforms:
103102
continue
104-
file = Path(uri) / (visitor.current_document + '.html')
105-
link_html(file, visitor.source_transforms, transposed)
103+
file = Path(app.outdir) / (visitor.current_document + '.html')
104+
link_html(file, app.outdir, visitor.source_transforms, transposed)
106105

107106
refs_file = Path(app.srcdir) / self.code_refs_file
108107
refs = {}

src/sphinx_codeautolink/extension/block.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,17 @@ def visit_literal_block(self, node: nodes.literal_block):
161161
self.source_transforms.append(transforms)
162162

163163

164-
def link_html(document: Path, transforms: List[SourceTransforms], inventory: dict):
164+
def link_html(
165+
document: Path, out_dir: str, transforms: List[SourceTransforms], inventory: dict
166+
):
165167
"""Inject links to code blocks on disk."""
166168
text = document.read_text('utf-8')
167169
soup = BeautifulSoup(text, 'html.parser')
168170
blocks = soup.find_all('div', attrs={'class': 'highlight-python notranslate'})
169171
inners = [block.select('div > pre')[0] for block in blocks]
170172

171-
link_pattern = '<a href="{link}" title="{title}">{text}</a>'
173+
up_lvls = len(document.relative_to(out_dir).parents) - 1
174+
link_pattern = '<a href="' + '../' * up_lvls + '{link}" title="{title}">{text}</a>'
172175
name_pattern = '<span class="n">{name}</span>'
173176
period = '<span class="o">.</span>'
174177

0 commit comments

Comments
 (0)