Skip to content

Commit 10bd4a2

Browse files
committed
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).
1 parent c04ad8c commit 10bd4a2

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎Doc/tools/extensions/lexers/__init__.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1+
from docutils import nodes
2+
from sphinx.transforms import SphinxTransform
3+
14
from .asdl_lexer import ASDLLexer
25
from .peg_lexer import PEGLexer
36

47

8+
class StripPEGBlocks(SphinxTransform):
9+
# Strip PEG blocks like PEGLexer does when highlighting them, so that
10+
# other builders (gettext in particular) get the text the page shows.
11+
# Must run before the i18n Locale transform (20).
12+
default_priority = 10
13+
14+
def apply(self):
15+
lexer = PEGLexer()
16+
for node in self.document.findall(nodes.literal_block):
17+
if node.get("language") != "peg":
18+
continue
19+
tokens = lexer.get_tokens_unprocessed(node.rawsource)
20+
text = "".join(value for _, _, value in tokens)
21+
node.rawsource = text
22+
node[:] = [nodes.Text(text)]
23+
24+
525
def setup(app):
626
# Used for highlighting Parser/Python.asdl in library/ast.rst
727
app.add_lexer("asdl", ASDLLexer)
828
# Used for highlighting Grammar/python.gram in reference/grammar.rst
929
app.add_lexer("peg", PEGLexer)
30+
app.add_transform(StripPEGBlocks)
1031

1132
return {
1233
"version": "1.0",

0 commit comments

Comments
 (0)