From 99d125271efe02422cd23b263d6c85584caa9bbf Mon Sep 17 00:00:00 2001 From: Aurora-NICExq <239583721+Aurora-NICExq@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:03:04 +0800 Subject: [PATCH] gh-142714: Exclude stripped PEG grammar text from translation catalogs (GH-159305) (cherry picked from commit 19270d3bbb58c08a97bd9b481655adf42bdfbe3c) Co-authored-by: Aurora-NICExq <239583721+Aurora-NICExq@users.noreply.github.com> Co-authored-by: Stan Ulbrych --- Doc/tools/extensions/lexers/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Doc/tools/extensions/lexers/__init__.py b/Doc/tools/extensions/lexers/__init__.py index e12ac5be8139cc2..580ec172d876f99 100644 --- a/Doc/tools/extensions/lexers/__init__.py +++ b/Doc/tools/extensions/lexers/__init__.py @@ -1,12 +1,34 @@ +from docutils import nodes +from sphinx.transforms import SphinxTransform + from .asdl_lexer import ASDLLexer from .peg_lexer import PEGLexer +class StripPEGBlocks(SphinxTransform): + # Strip PEG blocks like PEGLexer does when highlighting them, so that + # other builders (gettext in particular) get the text the page shows. + # Must run before the i18n Locale transform (20). + default_priority = 10 + + def apply(self): + lexer = PEGLexer() + for node in self.document.findall(nodes.literal_block): + if node.get("language") != "peg": + continue + tokens = lexer.get_tokens_unprocessed(node.rawsource) + text = "".join(value for _, _, value in tokens) + node.rawsource = text + node[:] = [nodes.Text(text)] + + def setup(app): # Used for highlighting Parser/Python.asdl in library/ast.rst app.add_lexer("asdl", ASDLLexer) # Used for highlighting Grammar/python.gram in reference/grammar.rst app.add_lexer("peg", PEGLexer) + # Strips hidden grammar parts before translation catalogs are built + app.add_transform(StripPEGBlocks) return { "version": "1.0",