Skip to content

Commit 19270d3

Browse files
gh-142714: Exclude stripped PEG grammar text from translation catalogs (#159305)
Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 87f8c0d commit 19270d3

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
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+
# Strips hidden grammar parts before translation catalogs are built
31+
app.add_transform(StripPEGBlocks)
1032

1133
return {
1234
"version": "1.0",

0 commit comments

Comments
 (0)