Skip to content

Commit f369892

Browse files
committed
Improve handling of nonumber nodes and title IDs
Adds IDs to title nodes when the 'nonumber' option is set, enabling more reliable identification of titles in HTML output. Refactors the depart_unenumerable_node function to use these IDs for inserting captions, simplifying the logic and improving robustness.
1 parent 0d274ec commit f369892

2 files changed

Lines changed: 11 additions & 22 deletions

File tree

sphinx_proof/directive.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ def run(self) -> List[Node]:
104104
node = node_type()
105105

106106
node.document = self.state.document
107-
node += nodes.title(title_text, "", *textnodes)
107+
node_title = nodes.title(title_text, "", *textnodes)
108+
if "nonumber" in self.options:
109+
# add ids to the title node for nonumber
110+
# nodes for later searching
111+
node_title["ids"].extend(ids)
112+
node += node_title
108113
node += section
109114

110115
# Set node attributes

sphinx_proof/nodes.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,16 @@ def visit_unenumerable_node(self, node: Node) -> None:
6262

6363
def depart_unenumerable_node(self, node: Node) -> None:
6464
realtyp = node.attributes.get("realtype", "")
65-
title = node.attributes.get("title", "")
65+
id = node.attributes.get("ids", [""])[0]
6666
if isinstance(self, LaTeXTranslator):
6767
idx = list_rindex(self.body, latex_admonition_start) + 2
6868
self.body.insert(idx, f"{realtyp.title()}")
6969
self.body.append(latex_admonition_end)
7070
else:
71-
if title == "":
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-
88-
else:
89-
idx = list_rindex(self.body, title)
90-
element = f"<span>{_(realtyp.title())} </span>"
71+
# use the id to find the correct title location
72+
search_str = f'<p class="admonition-title" id="{id}">'
73+
idx = list_rindex(self.body, search_str) + 1
74+
element = f'<span class="caption-number">{_(realtyp.title())} </span>'
9175
self.body.insert(idx, element)
9276
self.body.append("</div>")
9377

0 commit comments

Comments
 (0)