Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Docutils 0.19 footnote styles #10599

Merged
merged 10 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -26,6 +26,9 @@ Features added
Pradyun Gedam.
* #10619: LaTeX: new ``shadowShadowColor`` and ``shadowBorderColor`` configurations
for :ref:`'sphinxsetup' <latexsphinxsetup>` key of :confval:`latex_elements`
* #10599: HTML Theme: Wrap consecutive footnotes in an ``<aside>`` element when
using Docutils 0.18 or later, to allow for easier styling. This matches the
behaviour introduced in Docutils 0.19. Patch by Adam Turner.

Bugs fixed
----------
Expand Down
8 changes: 3 additions & 5 deletions sphinx/themes/basic/static/basic.css_t
Expand Up @@ -629,7 +629,7 @@ ul.simple p {
margin-bottom: 0;
}

/* Docutils 0.17 and older (footnotes & citations) */
{%- if docutils_version_info[:2] < (0, 18) %}
dl.footnote > dt,
dl.citation > dt {
float: left;
Expand All @@ -646,8 +646,7 @@ dl.citation > dd:after {
content: "";
clear: both;
}

/* Docutils 0.18+ (footnotes & citations) */
{%- elif docutils_version_info[:2] >= (0, 18) %}
aside.footnote > span,
div.citation > span {
float: left;
Expand All @@ -671,8 +670,7 @@ div.citation > p:last-of-type:after {
content: "";
clear: both;
}

/* Footnotes & citations ends */
{%- endif %}

dl.field-list {
display: grid;
Expand Down
39 changes: 38 additions & 1 deletion sphinx/util/docutils.py
Expand Up @@ -18,6 +18,7 @@
from docutils.parsers.rst.states import Inliner
from docutils.statemachine import State, StateMachine, StringList
from docutils.utils import Reporter, unescape
from docutils.writers._html_base import HTMLTranslator # NoQA

from sphinx.deprecation import RemovedInSphinx70Warning, deprecated_alias
from sphinx.errors import SphinxError
Expand Down Expand Up @@ -182,10 +183,46 @@ def using_user_docutils_conf(confdir: Optional[str]) -> Generator[None, None, No
os.environ['DOCUTILSCONFIG'] = docutilsconfig


@contextmanager
def du19_footnotes() -> Generator[None, None, None]:
tk0miya marked this conversation as resolved.
Show resolved Hide resolved
def visit_footnote(self, node):
label_style = self.settings.footnote_references
if not isinstance(node.previous_sibling(), type(node)):
self.body.append(f'<aside class="footnote-list {label_style}">\n')
self.body.append(self.starttag(node, 'aside',
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved
classes=[node.tagname, label_style],
role="note"))

def depart_footnote(self, node):
self.body.append('</aside>\n')
if not isinstance(node.next_node(descend=False, siblings=True),
type(node)):
self.body.append('</aside>\n')

old_visit_footnote = HTMLTranslator.visit_footnote
old_depart_footnote = HTMLTranslator.depart_footnote

# Only apply on Docutils 0.18 or 0.18.1, as 0.17 and earlier used a <dl> based
# approach, and 0.19 and later use the fixed approach by default.
if docutils.__version_info__[:2] == (0, 18):
HTMLTranslator.visit_footnote = visit_footnote # type: ignore[assignment]
HTMLTranslator.depart_footnote = depart_footnote # type: ignore[assignment]

try:
yield
finally:
if docutils.__version_info__[:2] == (0, 18):
HTMLTranslator.visit_footnote = old_visit_footnote # type: ignore[assignment]
HTMLTranslator.depart_footnote = old_depart_footnote # type: ignore[assignment]


@contextmanager
def patch_docutils(confdir: Optional[str] = None) -> Generator[None, None, None]:
"""Patch to docutils temporarily."""
with patched_get_language(), patched_rst_get_language(), using_user_docutils_conf(confdir):
with patched_get_language(), \
patched_rst_get_language(), \
using_user_docutils_conf(confdir), \
du19_footnotes():
yield


Expand Down