Skip to content

Commit f10f0b6

Browse files
authored
Various miscellaneous improvements (#12578)
1 parent b1ed87e commit f10f0b6

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

sphinx/builders/html/__init__.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -969,26 +969,24 @@ def has_wildcard(pattern: str) -> bool:
969969

970970
# user sidebar settings
971971
html_sidebars = self.get_builder_config('sidebars', 'html')
972-
for pattern, patsidebars in html_sidebars.items():
972+
msg = __('page %s matches two patterns in html_sidebars: %r and %r')
973+
for pattern, pat_sidebars in html_sidebars.items():
973974
if patmatch(pagename, pattern):
974-
if matched:
975-
if has_wildcard(pattern):
976-
# warn if both patterns contain wildcards
977-
if has_wildcard(matched):
978-
logger.warning(__('page %s matches two patterns in '
979-
'html_sidebars: %r and %r'),
980-
pagename, matched, pattern)
981-
# else the already matched pattern is more specific
982-
# than the present one, because it contains no wildcard
983-
continue
975+
if matched and has_wildcard(pattern):
976+
# warn if both patterns contain wildcards
977+
if has_wildcard(matched):
978+
logger.warning(msg, pagename, matched)
979+
# else the already matched pattern is more specific
980+
# than the present one, because it contains no wildcard
981+
continue
984982
matched = pattern
985-
sidebars = patsidebars
983+
sidebars = pat_sidebars
986984

987985
if len(sidebars) == 0:
988986
# keep defaults
989987
pass
990988

991-
ctx['sidebars'] = sidebars
989+
ctx['sidebars'] = list(sidebars)
992990

993991
# --------- these are overwritten by the serialization builder
994992

sphinx/domains/python/_object.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ def make_xref(
8989

9090
return result
9191

92+
_delimiters_re = re.compile(
93+
r'(\s*[\[\]\(\),](?:\s*o[rf]\s)?\s*|\s+o[rf]\s+|\s*\|\s*|\.\.\.)'
94+
)
95+
9296
def make_xrefs(
9397
self,
9498
rolename: str,
@@ -100,9 +104,7 @@ def make_xrefs(
100104
inliner: Inliner | None = None,
101105
location: Node | None = None,
102106
) -> list[Node]:
103-
delims = r'(\s*[\[\]\(\),](?:\s*o[rf]\s)?\s*|\s+o[rf]\s+|\s*\|\s*|\.\.\.)'
104-
delims_re = re.compile(delims)
105-
sub_targets = re.split(delims, target)
107+
sub_targets = self._delimiters_re.split(target)
106108

107109
split_contnode = bool(contnode and contnode.astext() == target)
108110

@@ -112,13 +114,13 @@ def make_xrefs(
112114
if split_contnode:
113115
contnode = nodes.Text(sub_target)
114116

115-
if in_literal or delims_re.match(sub_target):
117+
if in_literal or self._delimiters_re.match(sub_target):
116118
results.append(contnode or innernode(sub_target, sub_target)) # type: ignore[call-arg]
117119
else:
118120
results.append(self.make_xref(rolename, domain, sub_target,
119121
innernode, contnode, env, inliner, location))
120122

121-
if sub_target in ('Literal', 'typing.Literal', '~typing.Literal'):
123+
if sub_target in {'Literal', 'typing.Literal', '~typing.Literal'}:
122124
in_literal = True
123125

124126
return results

sphinx/transforms/__init__.py

+34-20
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,33 @@
2222

2323
if TYPE_CHECKING:
2424
from collections.abc import Iterator
25+
from typing import Literal
2526

2627
from docutils.nodes import Node, Text
28+
from typing_extensions import TypeAlias, TypeIs
2729

2830
from sphinx.application import Sphinx
2931
from sphinx.config import Config
3032
from sphinx.domains.std import StandardDomain
3133
from sphinx.environment import BuildEnvironment
3234
from sphinx.util.typing import ExtensionMetadata
3335

36+
_DEFAULT_SUBSTITUTION_NAMES: TypeAlias = Literal[
37+
'version',
38+
'release',
39+
'today',
40+
'translation progress',
41+
]
42+
3443

3544
logger = logging.getLogger(__name__)
3645

37-
default_substitutions = {
46+
_DEFAULT_SUBSTITUTIONS = frozenset({
3847
'version',
3948
'release',
4049
'today',
4150
'translation progress',
42-
}
51+
})
4352

4453

4554
class SphinxTransform(Transform):
@@ -105,20 +114,25 @@ class DefaultSubstitutions(SphinxTransform):
105114

106115
def apply(self, **kwargs: Any) -> None:
107116
# only handle those not otherwise defined in the document
108-
to_handle = default_substitutions - set(self.document.substitution_defs)
117+
to_handle = _DEFAULT_SUBSTITUTIONS - set(self.document.substitution_defs)
109118
for ref in self.document.findall(nodes.substitution_reference):
110-
refname = ref['refname']
111-
if refname in to_handle:
112-
if refname == 'translation progress':
113-
# special handling: calculate translation progress
114-
text = _calculate_translation_progress(self.document)
115-
else:
116-
text = self.config[refname]
117-
if refname == 'today' and not text:
118-
# special handling: can also specify a strftime format
119-
text = format_date(self.config.today_fmt or _('%b %d, %Y'),
120-
language=self.config.language)
121-
ref.replace_self(nodes.Text(text))
119+
if (name := ref['refname']) in to_handle:
120+
ref.replace_self(self._handle_default_substitution(name))
121+
122+
def _handle_default_substitution(self, name: _DEFAULT_SUBSTITUTION_NAMES) -> nodes.Text:
123+
if name == 'translation progress':
124+
# special handling: calculate translation progress
125+
return nodes.Text(_calculate_translation_progress(self.document))
126+
if name == 'today':
127+
if text := self.config.today:
128+
return nodes.Text(text)
129+
# special handling: can also specify a strftime format
130+
return nodes.Text(format_date(
131+
self.config.today_fmt or _('%b %d, %Y'),
132+
language=self.config.language,
133+
))
134+
# config.version and config.release
135+
return nodes.Text(getattr(self.config, name))
122136

123137

124138
def _calculate_translation_progress(document: nodes.document) -> str:
@@ -263,15 +277,15 @@ class ExtraTranslatableNodes(SphinxTransform):
263277
default_priority = 10
264278

265279
def apply(self, **kwargs: Any) -> None:
266-
targets = self.config.gettext_additional_targets
267-
target_nodes = [v for k, v in TRANSLATABLE_NODES.items() if k in targets]
280+
targets = frozenset(self.config.gettext_additional_targets)
281+
target_nodes = tuple(v for k, v in TRANSLATABLE_NODES.items() if k in targets)
268282
if not target_nodes:
269283
return
270284

271-
def is_translatable_node(node: Node) -> bool:
272-
return isinstance(node, tuple(target_nodes))
285+
def is_translatable_node(node: Node) -> TypeIs[nodes.Element]:
286+
return isinstance(node, target_nodes)
273287

274-
for node in self.document.findall(is_translatable_node): # type: nodes.Element
288+
for node in self.document.findall(is_translatable_node):
275289
node['translatable'] = True
276290

277291

0 commit comments

Comments
 (0)