|
6 | 6 | from sphinx.util.docutils import SphinxDirective |
7 | 7 | from sphinx.util.nodes import make_id |
8 | 8 |
|
| 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 | + |
9 | 45 | 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 | + |
10 | 52 | group_name = options['group'] |
11 | 53 |
|
12 | 54 | # Docutils elements have a `rawsource` attribute that is supposed to be |
@@ -99,40 +141,14 @@ def make_snippet(directive, options, content): |
99 | 141 | return [node] |
100 | 142 |
|
101 | 143 |
|
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 |
117 | 146 |
|
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. |
120 | 151 | """ |
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): |
136 | 152 | has_content = False |
137 | 153 | required_arguments = 1 |
138 | 154 | optional_arguments = 0 |
|
0 commit comments