From 10bd4a22354fe2800a7f6093b154ecd7556913f0 Mon Sep 17 00:00:00 2001 From: Arthur Guilliman Date: Tue, 13 Oct 2026 18:42:48 +0800 Subject: [PATCH 1/2] gh-142714: Strip hidden parts of the PEG grammar in all builders Apply PEGLexer's stripping to peg literal blocks before the i18n transform, so the catalogs (and the text builder) only get what the rendered page shows. This also stops the hidden rules from showing on translated pages, which happens when the "rules for invalid syntax" comment is translated (as on zh-tw). --- Doc/tools/extensions/lexers/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Doc/tools/extensions/lexers/__init__.py b/Doc/tools/extensions/lexers/__init__.py index e12ac5be8139cc2..584ad80dee39dd4 100644 --- a/Doc/tools/extensions/lexers/__init__.py +++ b/Doc/tools/extensions/lexers/__init__.py @@ -1,12 +1,33 @@ +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) + app.add_transform(StripPEGBlocks) return { "version": "1.0", From 75861d0fc16db3803317b2349a032a2f1f025d99 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 28 Oct 2026 15:55:44 +0000 Subject: [PATCH 2/2] Add a comment --- Doc/tools/extensions/lexers/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/tools/extensions/lexers/__init__.py b/Doc/tools/extensions/lexers/__init__.py index 584ad80dee39dd4..580ec172d876f99 100644 --- a/Doc/tools/extensions/lexers/__init__.py +++ b/Doc/tools/extensions/lexers/__init__.py @@ -27,6 +27,7 @@ def setup(app): 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 {