|
22 | 22 |
|
23 | 23 | if TYPE_CHECKING:
|
24 | 24 | from collections.abc import Iterator
|
| 25 | + from typing import Literal |
25 | 26 |
|
26 | 27 | from docutils.nodes import Node, Text
|
| 28 | + from typing_extensions import TypeAlias, TypeIs |
27 | 29 |
|
28 | 30 | from sphinx.application import Sphinx
|
29 | 31 | from sphinx.config import Config
|
30 | 32 | from sphinx.domains.std import StandardDomain
|
31 | 33 | from sphinx.environment import BuildEnvironment
|
32 | 34 | from sphinx.util.typing import ExtensionMetadata
|
33 | 35 |
|
| 36 | + _DEFAULT_SUBSTITUTION_NAMES: TypeAlias = Literal[ |
| 37 | + 'version', |
| 38 | + 'release', |
| 39 | + 'today', |
| 40 | + 'translation progress', |
| 41 | + ] |
| 42 | + |
34 | 43 |
|
35 | 44 | logger = logging.getLogger(__name__)
|
36 | 45 |
|
37 |
| -default_substitutions = { |
| 46 | +_DEFAULT_SUBSTITUTIONS = frozenset({ |
38 | 47 | 'version',
|
39 | 48 | 'release',
|
40 | 49 | 'today',
|
41 | 50 | 'translation progress',
|
42 |
| -} |
| 51 | +}) |
43 | 52 |
|
44 | 53 |
|
45 | 54 | class SphinxTransform(Transform):
|
@@ -105,20 +114,25 @@ class DefaultSubstitutions(SphinxTransform):
|
105 | 114 |
|
106 | 115 | def apply(self, **kwargs: Any) -> None:
|
107 | 116 | # 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) |
109 | 118 | 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)) |
122 | 136 |
|
123 | 137 |
|
124 | 138 | def _calculate_translation_progress(document: nodes.document) -> str:
|
@@ -263,15 +277,15 @@ class ExtraTranslatableNodes(SphinxTransform):
|
263 | 277 | default_priority = 10
|
264 | 278 |
|
265 | 279 | 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) |
268 | 282 | if not target_nodes:
|
269 | 283 | return
|
270 | 284 |
|
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) |
273 | 287 |
|
274 |
| - for node in self.document.findall(is_translatable_node): # type: nodes.Element |
| 288 | + for node in self.document.findall(is_translatable_node): |
275 | 289 | node['translatable'] = True
|
276 | 290 |
|
277 | 291 |
|
|
0 commit comments