Skip to content

Commit b10d78b

Browse files
committed
Adjust docstrings & move GrammarSnippetDirective class up
1 parent 9139e25 commit b10d78b

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

Doc/tools/extensions/grammar_snippet.py

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,49 @@
66
from sphinx.util.docutils import SphinxDirective
77
from sphinx.util.nodes import make_id
88

9+
10+
class GrammarSnippetDirective(SphinxDirective):
11+
"""Transform a grammar-snippet directive to a Sphinx literal_block
12+
13+
That is, turn something like:
14+
15+
.. grammar-snippet:: file
16+
:group: python-grammar
17+
18+
file: (NEWLINE | statement)*
19+
20+
into something similar to Sphinx productionlist, but better suited
21+
for our needs:
22+
- Instead of `::=`, use a colon, as in `Grammar/python.gram`
23+
- Show the listing almost as is, with no auto-aligment.
24+
The only special character is the backtick, which marks tokens.
25+
26+
Unlike Sphinx's productionlist, this directive supports options.
27+
The "group" must be given as a named option.
28+
The content must be preceded by a blank line (like with most ReST
29+
directives).
30+
"""
31+
has_content = True
32+
option_spec = {
33+
'group': directives.unchanged,
34+
}
35+
36+
# We currently ignore arguments.
37+
required_arguments = 0
38+
optional_arguments = 1
39+
final_argument_whitespace = True
40+
41+
def run(self):
42+
return make_snippet(self, self.options, self.content)
43+
44+
945
def make_snippet(directive, options, content):
46+
"""Create a literal block from options & content.
47+
48+
This implements the common functionality for GrammarSnippetDirective
49+
and CompatProductionList.
50+
"""
51+
1052
group_name = options['group']
1153

1254
# Docutils elements have a `rawsource` attribute that is supposed to be
@@ -99,40 +141,14 @@ def make_snippet(directive, options, content):
99141
return [node]
100142

101143

102-
class GrammarSnippetDirective(SphinxDirective):
103-
"""Transform a grammar-snippet directive to a Sphinx productionlist
104-
105-
That is, turn something like:
106-
107-
.. grammar-snippet:: file
108-
:group: python-grammar
109-
110-
file: (NEWLINE | statement)*
111-
112-
into something similar to Sphinx productionlist, but better suited
113-
for our needs:
114-
- Instead of `::=`, use a colon, as in `Grammar/python.gram`
115-
- Show the listing almost as is, with no auto-aligment.
116-
The only special character is the backtick, which marks tokens.
144+
class CompatProductionList(SphinxDirective):
145+
"""Create grammar snippets from ReST productionlist syntax
117146
118-
Unlike Sphinx's productionlist, this directive supports options.
119-
The "group" must be given as an option.
147+
This is intended to be a transitional directive, used while we switch
148+
from productionlist to grammar-snippet.
149+
It makes existing docs that use the ReST syntax look like grammar-snippet,
150+
as much as possible.
120151
"""
121-
has_content = True
122-
option_spec = {
123-
'group': directives.unchanged,
124-
}
125-
126-
# We currently ignore arguments.
127-
required_arguments = 0
128-
optional_arguments = 1
129-
final_argument_whitespace = True
130-
131-
def run(self):
132-
return make_snippet(self, self.options, self.content)
133-
134-
135-
class CompatProductionList(SphinxDirective):
136152
has_content = False
137153
required_arguments = 1
138154
optional_arguments = 0

0 commit comments

Comments
 (0)