Skip to content

Commit 0d274ec

Browse files
committed
Fix nesting of unnumbered directives in nodes.py
Improves handling of nested unenumerable nodes by correctly identifying the appropriate occurrence of '<p class="admonition-title">' in the body. Updates the list_rindex function to support skipping occurrences, addressing issue #165.
1 parent c3e0301 commit 0d274ec

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
- Inconsistencies and missing colors within CSS styles. [b337c21](https://github.com/executablebooks/sphinx-proof/commit/b337c21675464224a4418beb9e70d3b6dc8e4127)
1313
- Missing documentation for notation directive. [6f33744](https://github.com/executablebooks/sphinx-proof/commit/6f33744544b7596a30beba5b8807491d642b77a7) and [b15c7c0](https://github.com/executablebooks/sphinx-proof/commit/b15c7c09adefe85e0fdb8dea6dc460bd0f51fb1f)
14+
- Nesting of unnumbered directives, fixing [\#165](https://github.com/executablebooks/sphinx-proof/issues/165).
1415

1516
## v0.3.0 (2025-10-20)
1617

sphinx_proof/nodes.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,22 @@ def depart_unenumerable_node(self, node: Node) -> None:
6969
self.body.append(latex_admonition_end)
7070
else:
7171
if title == "":
72-
idx = list_rindex(self.body, '<p class="admonition-title">') + 1
72+
# because of nesting, first find out the correct occurrence
73+
logger.info("Departing unenumerable node with empty title", color="blue")
74+
logger.info(f"Node: {node.pformat()}", color="green")
75+
closer_found = False
76+
skip = 0
77+
while not closer_found:
78+
idx = list_rindex(self.body, '<p class="admonition-title">', skip) + 1
79+
closer = self.body[idx]
80+
logger.info(f"Closer found: {closer}", color="yellow")
81+
if "</p>" in closer:
82+
closer_found = True
83+
else:
84+
skip += 1
85+
# Here the issue is generated
86+
# a closing </p> should be found.
87+
7388
else:
7489
idx = list_rindex(self.body, title)
7590
element = f"<span>{_(realtyp.title())} </span>"
@@ -122,11 +137,15 @@ def find_parent(env, node, parent_tag):
122137
return None
123138

124139

125-
def list_rindex(li, x) -> int:
126-
"""Getting the last occurence of an item in a list."""
140+
def list_rindex(li, x, skip=0) -> int:
141+
"""Getting the last occurrence of an item in a list."""
142+
"""Skipping the first skip occurrences from the end."""
127143
for i in reversed(range(len(li))):
128144
if li[i] == x:
129-
return i
145+
if skip == 0:
146+
return i
147+
else:
148+
skip -= 1
130149
raise ValueError("{} is not in list".format(x))
131150

132151

0 commit comments

Comments
 (0)