Skip to content

Commit

Permalink
Fix figure numbering is overriden when use the extension
Browse files Browse the repository at this point in the history
** Why is this change required? **

When numfig is set to true, figure directives are automatically
numbered. However, before this fix, the caption of figures is
overriden and the automatically numbering is gone. This commit
fixed the issue (also listed on github
#14).

** How does this change solve the issue? **

Instead of using nodes.caption directly (also with override when
add_node, which causes bug), we create a subclass pseudocodeCaption
and use it instead.

** Does this commit cause any expected behavior to change?  If so, what? **

** Issue: **

** Design Docs: **
  • Loading branch information
xxks-kkk committed Jan 3, 2022
1 parent bada2e4 commit a4d39ce
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
10 changes: 6 additions & 4 deletions sphinxcontrib/pseudocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class pseudocodeContentNode(nodes.General, nodes.Element):
"""Content of pseudocode."""
pass

class pseudocodeCaption(nodes.caption):
"""Caption of pseudocode."""
pass

class Pseudocode(Directive):
"""An environment for pseudocode."""
Expand Down Expand Up @@ -173,12 +176,12 @@ def pseudocode_wrapper(directive, node, caption=None):
"""Parse caption, and append it to the node."""
parsed = nodes.Element()
if caption is None:
caption_node = nodes.caption()
caption_node = pseudocodeCaption()
else:
directive.state.nested_parse(
ViewList([caption], source=""), directive.content_offset, parsed
)
caption_node = nodes.caption(parsed[0].rawsource, "", *parsed[0].children)
caption_node = pseudocodeCaption(parsed[0].rawsource, "", *parsed[0].children)
caption_node.source = parsed[0].source
caption_node.line = parsed[0].line
node += caption_node
Expand Down Expand Up @@ -255,8 +258,7 @@ def setup(app):
html=(html_visit_stuff_node, html_depart_stuff_node),
)
app.add_node(
nodes.caption,
override=True,
pseudocodeCaption,
html=(html_visit_caption_node, html_depart_caption_node),
)
app.add_node(
Expand Down
File renamed without changes
4 changes: 3 additions & 1 deletion tests/roots/test-basic/conf.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
extensions = ['sphinxcontrib.pseudocode']
exclude_patterns = ['_build']
exclude_patterns = ['_build']
numfig = True
html_static_path = ['_static']
6 changes: 5 additions & 1 deletion tests/roots/test-basic/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ Hi, basic test

\begin{algorithmic}
\PRINT \texttt{'hello world'}
\end{algorithmic}
\end{algorithmic}

.. figure:: /_static/quicksort-demo.png

This figure should have a numbering in this caption.
1 change: 1 addition & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ def index(app, build_all):
def test_html_raw(index):
assert '<script src="https://cdn.jsdelivr.net/npm/pseudocode@latest/build/pseudocode.js"></script>' in index
assert '<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.js"></script>' in index
assert '<span class="caption-number">Fig. 1 </span>' in index

0 comments on commit a4d39ce

Please sign in to comment.