Skip to content

Commit ba04e22

Browse files
committed
fix: apply figure numbering with only tag
When using `.. only::` and then `.. figure::`, set the numbers correctly. Fixes #4726 Signed-off-by: Mike Fiedler <[email protected]>
1 parent eaebbec commit ba04e22

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

sphinx/environment/collectors/toctree.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,26 @@ def register_fignumber(
337337

338338
fignumbers[figure_id] = get_next_fignumber(figtype, secnum)
339339

340+
def should_process_only_node(node: addnodes.only) -> bool:
341+
"""Check if content in `only` node should be processed based on tags."""
342+
try:
343+
return env._tags.eval_condition(node['expr'])
344+
except Exception:
345+
# If evaluation fails, include the content
346+
return True
347+
340348
def _walk_doctree(
341349
docname: str, doctree: Element, secnum: tuple[int, ...]
342350
) -> None:
343351
nonlocal generated_docnames
344352
for subnode in doctree.children:
353+
# Skip content inside `only` directives that don't match current tags
354+
if isinstance(subnode, addnodes.only):
355+
if should_process_only_node(subnode):
356+
# Process the content of the `only` node
357+
_walk_doctree(docname, subnode, secnum)
358+
# Skip this node if it doesn't match tags
359+
continue
345360
if isinstance(subnode, nodes.section):
346361
next_secnum = get_section_number(docname, subnode)
347362
if next_secnum:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""Test configuration for numfig with only directive."""
2+
3+
numfig = True
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Test numfig with only directive
2+
================================
3+
4+
This is a test to reproduce issue #4726.
5+
6+
.. only:: latex
7+
8+
.. figure:: latex-image.png
9+
10+
Fig for latex
11+
12+
.. only:: html
13+
14+
.. figure:: html-image.png
15+
16+
Fig for html
17+
18+
.. figure:: common-image.png
19+
20+
Fig for all formats
21+
22+
The figures should be numbered consistently:
23+
- In HTML: "Fig for html" should be Fig. 1, "Fig for all formats" should be Fig. 2
24+
- In LaTeX: "Fig for latex" should be Fig. 1, "Fig for all formats" should be Fig. 2

tests/test_numfig_only.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Test numfig with only directive."""
2+
3+
from __future__ import annotations
4+
5+
import pytest
6+
7+
8+
@pytest.mark.sphinx('html', testroot='numfig-only', confoverrides={'numfig': True})
9+
def test_numfig_with_only_directive_html(app, status, warning):
10+
"""Test that figure numbers are assigned correctly with only directive in HTML."""
11+
app.build()
12+
13+
content = (app.outdir / 'index.html').read_text(encoding='utf-8')
14+
15+
# In HTML builds, only the HTML figure and common figure should be numbered
16+
# The HTML figure should be Fig. 1, and common figure should be Fig. 2
17+
18+
# Check that the HTML figure is numbered as Fig. 1
19+
assert 'Fig. 1' in content
20+
# Check that the common figure is numbered as Fig. 2
21+
assert 'Fig. 2' in content
22+
# Make sure there's no Fig. 3
23+
assert 'Fig. 3' not in content
24+
25+
26+
@pytest.mark.sphinx('latex', testroot='numfig-only', confoverrides={'numfig': True})
27+
def test_numfig_with_only_directive_latex(app, status, warning):
28+
"""Test that figure numbers are assigned correctly with only directive in LaTeX.
29+
30+
Note: This test verifies that figure numbering works correctly with the only directive.
31+
The actual content filtering (removing HTML-only content from LaTeX output) is handled
32+
by the OnlyNodeTransform which runs later in the build process.
33+
"""
34+
app.build(force_all=True)
35+
36+
content = (
37+
(app.outdir / 'projectnamenotset.tex')
38+
.read_text(encoding='utf8')
39+
.replace('\r\n', '\n')
40+
)
41+
42+
# Check that the HTML figure is numbered as Fig. 1
43+
assert 'Fig. 1' in content
44+
# Check that the common figure is numbered as Fig. 2
45+
assert 'Fig. 2' in content
46+
# Make sure there's no Fig. 3
47+
assert 'Fig. 3' not in content

0 commit comments

Comments
 (0)