Skip to content

Commit

Permalink
Merge pull request #9053 from tk0miya/title_node_for_toctree_caption
Browse files Browse the repository at this point in the history
Fix #8870: Use title node for the caption of toctree
  • Loading branch information
tk0miya committed Apr 8, 2021
2 parents dfc0a28 + 5604f9c commit ce5d66e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -152,6 +152,8 @@ Features added
Bugs fixed
----------

* #8870: The style of toctree captions has been changed with docutils-0.17

Testing
--------

Expand Down
2 changes: 1 addition & 1 deletion sphinx/environment/adapters/toctree.py
Expand Up @@ -236,7 +236,7 @@ def _entries_from_toctree(toctreenode: addnodes.toctree, parents: List[str],
newnode = addnodes.compact_paragraph('', '')
caption = toctree.attributes.get('caption')
if caption:
caption_node = nodes.caption(caption, '', *[nodes.Text(caption)])
caption_node = nodes.title(caption, '', *[nodes.Text(caption)])
caption_node.line = toctree.line
caption_node.source = toctree.source
caption_node.rawsource = toctree['rawcaption']
Expand Down
7 changes: 6 additions & 1 deletion sphinx/writers/html.py
Expand Up @@ -396,7 +396,12 @@ def depart_term(self, node: Element) -> None:

# overwritten
def visit_title(self, node: Element) -> None:
super().visit_title(node)
if isinstance(node.parent, addnodes.compact_paragraph) and node.parent.get('toctree'):
self.body.append(self.starttag(node, 'p', '', CLASS='caption'))
self.body.append('<span class="caption-text">')
self.context.append('</span></p>\n')
else:
super().visit_title(node)
self.add_secnumber(node)
self.add_fignumber(node.parent)
if isinstance(node.parent, nodes.table):
Expand Down
7 changes: 6 additions & 1 deletion sphinx/writers/html5.py
Expand Up @@ -347,7 +347,12 @@ def depart_term(self, node: Element) -> None:

# overwritten
def visit_title(self, node: Element) -> None:
super().visit_title(node)
if isinstance(node.parent, addnodes.compact_paragraph) and node.parent.get('toctree'):
self.body.append(self.starttag(node, 'p', '', CLASS='caption'))
self.body.append('<span class="caption-text">')
self.context.append('</span></p>\n')
else:
super().visit_title(node)
self.add_secnumber(node)
self.add_fignumber(node.parent)
if isinstance(node.parent, nodes.table):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_environment_toctree.py
Expand Up @@ -10,7 +10,7 @@

import pytest
from docutils import nodes
from docutils.nodes import bullet_list, caption, comment, list_item, reference
from docutils.nodes import bullet_list, comment, list_item, reference, title

from sphinx import addnodes
from sphinx.addnodes import compact_paragraph, only
Expand Down Expand Up @@ -211,7 +211,7 @@ def test_get_toctree_for(app):
app.build()
toctree = TocTree(app.env).get_toctree_for('index', app.builder, collapse=False)
assert_node(toctree,
[compact_paragraph, ([caption, "Table of Contents"],
[compact_paragraph, ([title, "Table of Contents"],
bullet_list,
bullet_list,
bullet_list)])
Expand Down Expand Up @@ -251,7 +251,7 @@ def test_get_toctree_for_collapse(app):
app.build()
toctree = TocTree(app.env).get_toctree_for('index', app.builder, collapse=True)
assert_node(toctree,
[compact_paragraph, ([caption, "Table of Contents"],
[compact_paragraph, ([title, "Table of Contents"],
bullet_list,
bullet_list,
bullet_list)])
Expand Down Expand Up @@ -283,7 +283,7 @@ def test_get_toctree_for_maxdepth(app):
toctree = TocTree(app.env).get_toctree_for('index', app.builder,
collapse=False, maxdepth=3)
assert_node(toctree,
[compact_paragraph, ([caption, "Table of Contents"],
[compact_paragraph, ([title, "Table of Contents"],
bullet_list,
bullet_list,
bullet_list)])
Expand Down Expand Up @@ -329,7 +329,7 @@ def test_get_toctree_for_includehidden(app):
toctree = TocTree(app.env).get_toctree_for('index', app.builder, collapse=False,
includehidden=False)
assert_node(toctree,
[compact_paragraph, ([caption, "Table of Contents"],
[compact_paragraph, ([title, "Table of Contents"],
bullet_list,
bullet_list)])

Expand Down
7 changes: 2 additions & 5 deletions tests/test_intl.py
Expand Up @@ -622,11 +622,8 @@ def test_html_meta(app):
assert expected_expr in result
expected_expr = '<meta content="I18N, SPHINX, MARKUP" name="keywords" />'
assert expected_expr in result
if docutils.__version_info__ < (0, 17):
expected_expr = '<p class="caption"><span class="caption-text">HIDDEN TOC</span></p>'
assert expected_expr in result
else:
expected_expr = '<p><span class="caption-text">HIDDEN TOC</span></p>'
expected_expr = '<p class="caption"><span class="caption-text">HIDDEN TOC</span></p>'
assert expected_expr in result


@sphinx_intl
Expand Down

0 comments on commit ce5d66e

Please sign in to comment.