Skip to content

Commit

Permalink
Enable automatic formatting for sphinx/transforms/ (#12971)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner authored Oct 4, 2024
1 parent 3a066f2 commit a6e4490
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 132 deletions.
1 change: 0 additions & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,5 @@ exclude = [
"sphinx/ext/todo.py",
"sphinx/ext/viewcode.py",
"sphinx/registry.py",
"sphinx/transforms/*",
"sphinx/writers/*",
]
53 changes: 28 additions & 25 deletions sphinx/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ def apply(self, **kwargs: Any) -> None:
if (name := ref['refname']) in to_handle:
ref.replace_self(self._handle_default_substitution(name))

def _handle_default_substitution(self, name: _DEFAULT_SUBSTITUTION_NAMES) -> nodes.Text:
def _handle_default_substitution(
self, name: _DEFAULT_SUBSTITUTION_NAMES
) -> nodes.Text:
if name == 'translation progress':
# special handling: calculate translation progress
return nodes.Text(_calculate_translation_progress(self.document))
if name == 'today':
if text := self.config.today:
return nodes.Text(text)
# special handling: can also specify a strftime format
return nodes.Text(format_date(
self.config.today_fmt or _('%b %d, %Y'),
language=self.config.language,
))
today_fmt = self.config.today_fmt or _('%b %d, %Y')
return nodes.Text(format_date(today_fmt, language=self.config.language))
# config.version and config.release
return nodes.Text(getattr(self.config, name))

Expand Down Expand Up @@ -185,8 +185,7 @@ class HandleCodeBlocks(SphinxTransform):
def apply(self, **kwargs: Any) -> None:
# move doctest blocks out of blockquotes
for node in self.document.findall(nodes.block_quote):
if all(isinstance(child, nodes.doctest_block) for child
in node.children):
if all(isinstance(child, nodes.doctest_block) for child in node.children):
node.replace_self(node.children)
# combine successive doctest blocks
# for node in self.document.findall(nodes.doctest_block):
Expand All @@ -211,9 +210,11 @@ def apply(self, **kwargs: Any) -> None:
domain: StandardDomain = self.env.domains.standard_domain

for node in self.document.findall(nodes.Element):
if (domain.is_enumerable_node(node) and
domain.get_numfig_title(node) is not None and
node['ids'] == []):
if (
domain.is_enumerable_node(node)
and domain.get_numfig_title(node) is not None
and node['ids'] == []
):
self.document.note_implicit_target(node)


Expand Down Expand Up @@ -262,8 +263,13 @@ class AutoIndexUpgrader(SphinxTransform):
def apply(self, **kwargs: Any) -> None:
for node in self.document.findall(addnodes.index):
if 'entries' in node and any(len(entry) == 4 for entry in node['entries']):
msg = __('4 column based index found. '
'It might be a bug of extensions you use: %r') % node['entries']
msg = (
__(
'4 column based index found. '
'It might be a bug of extensions you use: %r'
)
% node['entries']
)
logger.warning(msg, location=node)
for i, entry in enumerate(node['entries']):
if len(entry) == 4:
Expand Down Expand Up @@ -302,32 +308,32 @@ def apply(self, **kwargs: Any) -> None:
# note we do not warn on duplicate footnotes here
# (i.e. where the name has been moved to dupnames)
# since this is already reported by docutils
if not node['backrefs'] and node["names"]:
if not node['backrefs'] and node['names']:
logger.warning(
__('Footnote [%s] is not referenced.'),
node['names'][0] if node['names'] else node['dupnames'][0],
type='ref',
subtype='footnote',
location=node
location=node,
)
for node in self.document.symbol_footnotes:
if not node['backrefs']:
logger.warning(
__('Footnote [*] is not referenced.'),
type='ref',
subtype='footnote',
location=node
location=node,
)
for node in self.document.autofootnotes:
# note we do not warn on duplicate footnotes here
# (i.e. where the name has been moved to dupnames)
# since this is already reported by docutils
if not node['backrefs'] and node["names"]:
if not node['backrefs'] and node['names']:
logger.warning(
__('Footnote [#] is not referenced.'),
type='ref',
subtype='footnote',
location=node
location=node,
)


Expand Down Expand Up @@ -403,10 +409,7 @@ def is_available(self) -> bool:

# confirm selected language supports smart_quotes or not
language = self.env.settings['language_code']
return any(
tag in smartchars.quotes
for tag in normalize_language_tag(language)
)
return any(tag in smartchars.quotes for tag in normalize_language_tag(language))

def get_tokens(self, txtnodes: list[Text]) -> Iterator[tuple[str, str]]:
# A generator that yields ``(texttype, nodetext)`` tuples for a list
Expand Down Expand Up @@ -439,13 +442,13 @@ class GlossarySorter(SphinxTransform):

def apply(self, **kwargs: Any) -> None:
for glossary in self.document.findall(addnodes.glossary):
if glossary["sorted"]:
if glossary['sorted']:
definition_list = cast(nodes.definition_list, glossary[0])
definition_list[:] = sorted(
definition_list,
key=lambda item: unicodedata.normalize(
'NFD',
cast(nodes.term, item)[0].astext().lower()),
'NFD', cast(nodes.term, item)[0].astext().lower()
),
)


Expand Down Expand Up @@ -508,7 +511,7 @@ def _reorder_index_target_nodes(start_node: nodes.target) -> None:
first_idx = parent.index(nodes_to_reorder[0])
last_idx = parent.index(nodes_to_reorder[-1])
if first_idx + len(nodes_to_reorder) - 1 == last_idx:
parent[first_idx:last_idx + 1] = sorted(nodes_to_reorder, key=_sort_key)
parent[first_idx : last_idx + 1] = sorted(nodes_to_reorder, key=_sort_key)


def _sort_key(node: nodes.Node) -> int:
Expand Down
5 changes: 3 additions & 2 deletions sphinx/transforms/compact_bullet_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def visit_bullet_list(self, node: nodes.bullet_list) -> None:
pass

def visit_list_item(self, node: nodes.list_item) -> None:
children: list[Node] = [child for child in node.children
if not isinstance(child, nodes.Invisible)]
children: list[Node] = [
child for child in node.children if not isinstance(child, nodes.Invisible)
]
if len(children) != 1:
raise nodes.NodeFound
if not isinstance(children[0], nodes.paragraph):
Expand Down
Loading

0 comments on commit a6e4490

Please sign in to comment.