Skip to content

fix(logic): correct two structural logic bugs in MetadataFigure.run() - #65

Merged
lkdmc merged 1 commit into
mainfrom
fix/logic-bugs
Mar 31, 2026
Merged

fix(logic): correct two structural logic bugs in MetadataFigure.run()#65
lkdmc merged 1 commit into
mainfrom
fix/logic-bugs

Conversation

@lkdmc

@lkdmc lkdmc commented Mar 31, 2026

Copy link
Copy Markdown
Collaborator

Summary

Two bugs that cause incorrect document tree structure or broken output in non-HTML builders.

1. self.content assigned a plain list instead of StringList (__init__.py:547)

When :number: is used without a caption, the code injected an HTML <span> placeholder by assigning a plain Python list to self.content. The parent Figure.run() expects a StringList; passing a plain list can cause subtle failures during caption parsing.

Additionally, the raw HTML <span class="invisible-caption-text">&nbsp;</span> was used regardless of the active builder. LaTeX, PDF, and other non-HTML builders render it as literal markup text, producing garbage in the output.

Fix:

  • Wrap the placeholder in StringList([...]).
  • Detect the builder name via env.app.builder.name; HTML-family builders (html, dirhtml, singlehtml, readthedocs) receive the HTML span; all other builders receive an RST escaped-space (\\ ) which produces no visible output.

2. {cite:empty} reference node appended to the document root (__init__.py:592)

self.state.document += para inserts the citation-registration paragraph directly at the top level of the Sphinx document tree. This bypasses the normal document-tree structure and can result in an extra paragraph appearing in unexpected locations or breaking document traversal.

Fix: collect the node in _extra_prefix_nodes and prepend it to the returned node list at the end of run(). Sphinx then inserts it at the correct position in the tree relative to the figure.

Test plan

  • Use :number: on a figure without a caption; build with the LaTeX builder and confirm no raw HTML appears in the .tex output.
  • Use :bib: with an existing key; confirm the build completes and the citation reference does not appear as an extra visible paragraph anywhere in the HTML output.
  • Existing HTML builds should continue to show the invisible-caption span correctly for numbered uncaptioned figures.

https://claude.ai/code/session_01ApsmWPWsKBesMV1rq2VenA

1. Wrong type for self.content when :number: is used without a caption:
   self.content was assigned a plain list instead of the StringList that
   Figure.run() expects.  Wrap the placeholder in StringList().
   Additionally, detect the active builder so that the HTML invisible-
   caption span is only injected for HTML-family builders; other builders
   (LaTeX, PDF, ...) now receive an RST escaped-space placeholder instead
   of literal HTML markup.

2. {cite:empty} reference node appended directly to the document root:
   self.state.document += para inserts the paragraph at the top level of
   the document tree, corrupting its structure.  The paragraph is now
   collected in _extra_prefix_nodes and prepended to the returned node
   list so that Sphinx places it at the correct location in the tree.

https://claude.ai/code/session_01ApsmWPWsKBesMV1rq2VenA
@lkdmc
lkdmc requested a review from douden as a code owner March 31, 2026 16:10
@lkdmc
lkdmc merged commit e88d175 into main Mar 31, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants