Skip to content

Commit 616faf8

Browse files
authored
Simplify _publish_msgstr() (#13670)
1 parent be6593b commit 616faf8

File tree

1 file changed

+36
-65
lines changed

1 file changed

+36
-65
lines changed

sphinx/transforms/i18n.py

Lines changed: 36 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22

33
from __future__ import annotations
44

5-
import contextlib
65
from re import DOTALL, match
76
from textwrap import indent
87
from typing import TYPE_CHECKING, Any, TypeVar
98

9+
import docutils.utils
1010
from docutils import nodes
11-
from docutils.io import StringInput
1211

1312
from sphinx import addnodes
1413
from sphinx.domains.std import make_glossary_term, split_term_classifiers
1514
from sphinx.errors import ConfigError
16-
from sphinx.io import SphinxBaseReader
1715
from sphinx.locale import __
1816
from sphinx.locale import init as init_locale
19-
from sphinx.transforms import AutoIndexUpgrader, DoctreeReadEvent, SphinxTransform
20-
from sphinx.transforms.references import SphinxDomains
17+
from sphinx.transforms import SphinxTransform
2118
from sphinx.util import get_filetype, logging
19+
from sphinx.util.docutils import LoggingReporter
2220
from sphinx.util.i18n import docname_to_domain
2321
from sphinx.util.index_entries import split_index_msg
2422
from sphinx.util.nodes import (
@@ -28,11 +26,12 @@
2826
extract_messages,
2927
traverse_translatable_index,
3028
)
31-
from sphinx.versioning import UIDTransform
3229

3330
if TYPE_CHECKING:
3431
from collections.abc import Sequence
3532

33+
from docutils.frontend import Values
34+
3635
from sphinx.application import Sphinx
3736
from sphinx.config import Config
3837
from sphinx.environment import BuildEnvironment
@@ -52,77 +51,49 @@
5251
N = TypeVar('N', bound=nodes.Node)
5352

5453

55-
class _SphinxI18nReader(SphinxBaseReader):
56-
"""A document reader for internationalisation (i18n).
57-
58-
This returns the source line number of the original text
59-
as the current source line number to let users know where
60-
the error happened, because the translated texts are
61-
partial and they don't have correct line numbers.
62-
"""
63-
64-
def __init__(
65-
self, *args: Any, registry: SphinxComponentRegistry, **kwargs: Any
66-
) -> None:
67-
super().__init__(*args, **kwargs)
68-
unused = frozenset({
69-
PreserveTranslatableMessages,
70-
Locale,
71-
RemoveTranslatableInline,
72-
AutoIndexUpgrader,
73-
SphinxDomains,
74-
DoctreeReadEvent,
75-
UIDTransform,
76-
})
77-
transforms = self.transforms + registry.get_transforms()
78-
self.transforms = [
79-
transform for transform in transforms if transform not in unused
80-
]
81-
82-
83-
def publish_msgstr(
54+
def _publish_msgstr(
8455
source: str,
8556
source_path: str,
8657
source_line: int,
87-
config: Config,
88-
settings: Any,
8958
*,
59+
config: Config,
9060
env: BuildEnvironment,
9161
registry: SphinxComponentRegistry,
62+
settings: Values,
9263
) -> nodes.Element:
9364
"""Publish msgstr (single line) into docutils document
9465
9566
:param str source: source text
9667
:param str source_path: source path for warning indication
9768
:param source_line: source line for warning indication
98-
:param sphinx.config.Config config: sphinx config
9969
:param docutils.frontend.Values settings: docutils settings
100-
:return: document
101-
:rtype: docutils.nodes.document
70+
:param sphinx.config.Config config: sphinx config
10271
:param sphinx.environment.BuildEnvironment env: sphinx environment
10372
:param sphinx.registry.SphinxComponentRegistry registry: sphinx registry
73+
:return: document
74+
:rtype: docutils.nodes.document
10475
"""
76+
filetype = get_filetype(config.source_suffix, source_path)
77+
doc = docutils.utils.new_document(
78+
f'{source_path}:{source_line}:<translated>', settings
79+
)
80+
doc.reporter = LoggingReporter.from_reporter(doc.reporter)
81+
82+
# clear rst_prolog temporarily
83+
rst_prolog = config.rst_prolog
84+
config.rst_prolog = None
10585
try:
106-
# clear rst_prolog temporarily
107-
rst_prolog = config.rst_prolog
108-
config.rst_prolog = None
109-
110-
reader = _SphinxI18nReader(registry=registry)
111-
filetype = get_filetype(config.source_suffix, source_path)
11286
parser = registry.create_source_parser(filetype, config=config, env=env)
113-
doc = reader.read(
114-
source=StringInput(
115-
source=source, source_path=f'{source_path}:{source_line}:<translated>'
116-
),
117-
parser=parser,
118-
settings=settings,
119-
)
120-
with contextlib.suppress(IndexError): # empty node
121-
return doc[0]
122-
return doc
87+
parser.parse(source, doc)
88+
doc.current_source = doc.current_line = None
12389
finally:
12490
config.rst_prolog = rst_prolog
12591

92+
try:
93+
return doc[0] # type: ignore[return-value]
94+
except IndexError: # empty node
95+
return doc
96+
12697

12798
def parse_noqa(source: str) -> tuple[str, bool]:
12899
m = match(r'(.*)(?<!\\)#\s*noqa\s*$', source, DOTALL)
@@ -470,14 +441,14 @@ def apply(self, **kwargs: Any) -> None:
470441
if isinstance(node, LITERAL_TYPE_NODES):
471442
msgstr = '::\n\n' + indent(msgstr, ' ' * 3)
472443

473-
patch = publish_msgstr(
444+
patch = _publish_msgstr(
474445
msgstr,
475446
source,
476447
node.line, # type: ignore[arg-type]
477-
self.config,
478-
settings,
448+
config=self.config,
479449
env=self.env,
480450
registry=self.env._registry,
451+
settings=settings,
481452
)
482453
# FIXME: no warnings about inconsistent references in this part
483454
# XXX doctest and other block markup
@@ -491,14 +462,14 @@ def apply(self, **kwargs: Any) -> None:
491462
if isinstance(node, nodes.term):
492463
for _id in node['ids']:
493464
term, first_classifier = split_term_classifiers(msgstr)
494-
patch = publish_msgstr(
465+
patch = _publish_msgstr(
495466
term or '',
496467
source,
497468
node.line, # type: ignore[arg-type]
498-
self.config,
499-
settings,
469+
config=self.config,
500470
env=self.env,
501471
registry=self.env._registry,
472+
settings=settings,
502473
)
503474
updater.patch = make_glossary_term(
504475
self.env,
@@ -569,14 +540,14 @@ def apply(self, **kwargs: Any) -> None:
569540
# This generates: <section ...><title>msgstr</title></section>
570541
msgstr = msgstr + '\n' + '=' * len(msgstr) * 2
571542

572-
patch = publish_msgstr(
543+
patch = _publish_msgstr(
573544
msgstr,
574545
source,
575546
node.line, # type: ignore[arg-type]
576-
self.config,
577-
settings,
547+
config=self.config,
578548
env=self.env,
579549
registry=self.env._registry,
550+
settings=settings,
580551
)
581552
# Structural Subelements phase2
582553
if isinstance(node, nodes.title):

0 commit comments

Comments
 (0)