Skip to content

Commit 7f4f465

Browse files
committed
Have a separate Named Reference mapping for each .txt context
1 parent c127767 commit 7f4f465

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

snooty/postprocess.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,13 @@ class NamedReferenceHandlerPass1(Handler):
374374

375375
def __init__(self, context: Context) -> None:
376376
super().__init__(context)
377-
self.named_references: Dict[str, str] = {}
377+
self.named_references: Dict[FileId, Dict[str, str]] = defaultdict(dict)
378378

379379
def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
380380
if not isinstance(node, n.NamedReference):
381381
return
382382

383-
self.named_references[node.refname] = node.refuri
383+
self.named_references[fileid_stack.root][node.refname] = node.refuri
384384

385385

386386
class NamedReferenceHandlerPass2(Handler):
@@ -396,8 +396,10 @@ def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
396396
# Node is already populated with url; nothing to do
397397
return
398398

399-
refuri = self.context[NamedReferenceHandlerPass1].named_references.get(
400-
node.refname
399+
refuri = (
400+
self.context[NamedReferenceHandlerPass1]
401+
.named_references[fileid_stack.root]
402+
.get(node.refname)
401403
)
402404
if refuri is None:
403405
line = node.span[0]

snooty/test_postprocess.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,15 @@ def test_named_references() -> None:
17741774
17751775
Reference `GitHub`_
17761776
""",
1777+
# Should not be able to link to a named reference from another .txt
1778+
Path(
1779+
"source/foo.txt"
1780+
): """
1781+
.. include:: /fact-reference.rst
1782+
1783+
`docs link`_
1784+
""",
1785+
Path("source/fact-reference.rst"): "`docs link`_",
17771786
},
17781787
) as result:
17791788

@@ -1856,6 +1865,14 @@ def test_named_references() -> None:
18561865
""",
18571866
)
18581867

1868+
# Should not be able to link to a named reference from another .txt
1869+
active_file = "foo.txt"
1870+
diagnostics = result.diagnostics[FileId(active_file)]
1871+
assert len(diagnostics) == 1
1872+
active_file = "fact-reference.rst"
1873+
diagnostics = result.diagnostics[FileId(active_file)]
1874+
assert len(diagnostics) == 1
1875+
18591876

18601877
def test_contents_directive() -> None:
18611878
with make_test(

0 commit comments

Comments
 (0)