From a4d39ceee8c8b9247f1ccd3ec03563588eb60896 Mon Sep 17 00:00:00 2001 From: xxks-kkk Date: Mon, 3 Jan 2022 00:45:52 -0600 Subject: [PATCH] Fix figure numbering is overriden when use the extension ** 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 https://github.com/xxks-kkk/sphinxcontrib-pseudocode/issues/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: ** --- sphinxcontrib/pseudocode.py | 10 ++++++---- .../roots/test-basic/_static}/quicksort-demo.png | Bin tests/roots/test-basic/conf.py | 4 +++- tests/roots/test-basic/index.rst | 6 +++++- tests/test_html.py | 1 + 5 files changed, 15 insertions(+), 6 deletions(-) rename {img => tests/roots/test-basic/_static}/quicksort-demo.png (100%) diff --git a/sphinxcontrib/pseudocode.py b/sphinxcontrib/pseudocode.py index a7a1e42..2968d80 100644 --- a/sphinxcontrib/pseudocode.py +++ b/sphinxcontrib/pseudocode.py @@ -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.""" @@ -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 @@ -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( diff --git a/img/quicksort-demo.png b/tests/roots/test-basic/_static/quicksort-demo.png similarity index 100% rename from img/quicksort-demo.png rename to tests/roots/test-basic/_static/quicksort-demo.png diff --git a/tests/roots/test-basic/conf.py b/tests/roots/test-basic/conf.py index e568211..eb9924e 100644 --- a/tests/roots/test-basic/conf.py +++ b/tests/roots/test-basic/conf.py @@ -1,2 +1,4 @@ extensions = ['sphinxcontrib.pseudocode'] -exclude_patterns = ['_build'] \ No newline at end of file +exclude_patterns = ['_build'] +numfig = True +html_static_path = ['_static'] \ No newline at end of file diff --git a/tests/roots/test-basic/index.rst b/tests/roots/test-basic/index.rst index 633123b..f12ad59 100644 --- a/tests/roots/test-basic/index.rst +++ b/tests/roots/test-basic/index.rst @@ -6,4 +6,8 @@ Hi, basic test \begin{algorithmic} \PRINT \texttt{'hello world'} - \end{algorithmic} \ No newline at end of file + \end{algorithmic} + +.. figure:: /_static/quicksort-demo.png + + This figure should have a numbering in this caption. \ No newline at end of file diff --git a/tests/test_html.py b/tests/test_html.py index def1a41..877cc2b 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -16,3 +16,4 @@ def index(app, build_all): def test_html_raw(index): assert '' in index assert '' in index + assert 'Fig. 1 ' in index