179
|
1 |
# $Id: __init__.py 4802 2006-11-12 18:02:17Z goodger $
|
|
2 |
# Author: David Goodger <goodger@python.org>
|
|
3 |
# Copyright: This module has been placed in the public domain.
|
|
4 |
|
|
5 |
"""
|
|
6 |
This is ``docutils.parsers.rst`` package. It exports a single class, `Parser`,
|
|
7 |
the reStructuredText parser.
|
|
8 |
|
|
9 |
|
|
10 |
Usage
|
|
11 |
=====
|
|
12 |
|
|
13 |
1. Create a parser::
|
|
14 |
|
|
15 |
parser = docutils.parsers.rst.Parser()
|
|
16 |
|
|
17 |
Several optional arguments may be passed to modify the parser's behavior.
|
|
18 |
Please see `Customizing the Parser`_ below for details.
|
|
19 |
|
|
20 |
2. Gather input (a multi-line string), by reading a file or the standard
|
|
21 |
input::
|
|
22 |
|
|
23 |
input = sys.stdin.read()
|
|
24 |
|
|
25 |
3. Create a new empty `docutils.nodes.document` tree::
|
|
26 |
|
|
27 |
document = docutils.utils.new_document(source, settings)
|
|
28 |
|
|
29 |
See `docutils.utils.new_document()` for parameter details.
|
|
30 |
|
|
31 |
4. Run the parser, populating the document tree::
|
|
32 |
|
|
33 |
parser.parse(input, document)
|
|
34 |
|
|
35 |
|
|
36 |
Parser Overview
|
|
37 |
===============
|
|
38 |
|
|
39 |
The reStructuredText parser is implemented as a state machine, examining its
|
|
40 |
input one line at a time. To understand how the parser works, please first
|
|
41 |
become familiar with the `docutils.statemachine` module, then see the
|
|
42 |
`states` module.
|
|
43 |
|
|
44 |
|
|
45 |
Customizing the Parser
|
|
46 |
----------------------
|
|
47 |
|
|
48 |
Anything that isn't already customizable is that way simply because that type
|
|
49 |
of customizability hasn't been implemented yet. Patches welcome!
|
|
50 |
|
|
51 |
When instantiating an object of the `Parser` class, two parameters may be
|
|
52 |
passed: ``rfc2822`` and ``inliner``. Pass ``rfc2822=1`` to enable an initial
|
|
53 |
RFC-2822 style header block, parsed as a "field_list" element (with "class"
|
|
54 |
attribute set to "rfc2822"). Currently this is the only body-level element
|
|
55 |
which is customizable without subclassing. (Tip: subclass `Parser` and change
|
|
56 |
its "state_classes" and "initial_state" attributes to refer to new classes.
|
|
57 |
Contact the author if you need more details.)
|
|
58 |
|
|
59 |
The ``inliner`` parameter takes an instance of `states.Inliner` or a subclass.
|
|
60 |
It handles inline markup recognition. A common extension is the addition of
|
|
61 |
further implicit hyperlinks, like "RFC 2822". This can be done by subclassing
|
|
62 |
`states.Inliner`, adding a new method for the implicit markup, and adding a
|
|
63 |
``(pattern, method)`` pair to the "implicit_dispatch" attribute of the
|
|
64 |
subclass. See `states.Inliner.implicit_inline()` for details. Explicit
|
|
65 |
inline markup can be customized in a `states.Inliner` subclass via the
|
|
66 |
``patterns.initial`` and ``dispatch`` attributes (and new methods as
|
|
67 |
appropriate).
|
|
68 |
"""
|
|
69 |
|
|
70 |
__docformat__ = 'reStructuredText'
|
|
71 |
|
|
72 |
|
|
73 |
import docutils.parsers
|
|
74 |
import docutils.statemachine
|
|
75 |
from docutils.parsers.rst import states
|
|
76 |
from docutils import frontend, nodes
|
|
77 |
|
|
78 |
|
|
79 |
class Parser(docutils.parsers.Parser):
|
|
80 |
|
|
81 |
"""The reStructuredText parser."""
|
|
82 |
|
|
83 |
supported = ('restructuredtext', 'rst', 'rest', 'restx', 'rtxt', 'rstx')
|
|
84 |
"""Aliases this parser supports."""
|
|
85 |
|
|
86 |
settings_spec = (
|
|
87 |
'reStructuredText Parser Options',
|
|
88 |
None,
|
|
89 |
(('Recognize and link to standalone PEP references (like "PEP 258").',
|
|
90 |
['--pep-references'],
|
|
91 |
{'action': 'store_true', 'validator': frontend.validate_boolean}),
|
|
92 |
('Base URL for PEP references '
|
|
93 |
'(default "http://www.python.org/dev/peps/").',
|
|
94 |
['--pep-base-url'],
|
|
95 |
{'metavar': '<URL>', 'default': 'http://www.python.org/dev/peps/',
|
|
96 |
'validator': frontend.validate_url_trailing_slash}),
|
|
97 |
('Template for PEP file part of URL. (default "pep-%04d")',
|
|
98 |
['--pep-file-url-template'],
|
|
99 |
{'metavar': '<URL>', 'default': 'pep-%04d'}),
|
|
100 |
('Recognize and link to standalone RFC references (like "RFC 822").',
|
|
101 |
['--rfc-references'],
|
|
102 |
{'action': 'store_true', 'validator': frontend.validate_boolean}),
|
|
103 |
('Base URL for RFC references (default "http://www.faqs.org/rfcs/").',
|
|
104 |
['--rfc-base-url'],
|
|
105 |
{'metavar': '<URL>', 'default': 'http://www.faqs.org/rfcs/',
|
|
106 |
'validator': frontend.validate_url_trailing_slash}),
|
|
107 |
('Set number of spaces for tab expansion (default 8).',
|
|
108 |
['--tab-width'],
|
|
109 |
{'metavar': '<width>', 'type': 'int', 'default': 8,
|
|
110 |
'validator': frontend.validate_nonnegative_int}),
|
|
111 |
('Remove spaces before footnote references.',
|
|
112 |
['--trim-footnote-reference-space'],
|
|
113 |
{'action': 'store_true', 'validator': frontend.validate_boolean}),
|
|
114 |
('Leave spaces before footnote references.',
|
|
115 |
['--leave-footnote-reference-space'],
|
|
116 |
{'action': 'store_false', 'dest': 'trim_footnote_reference_space'}),
|
|
117 |
('Disable directives that insert the contents of external file '
|
|
118 |
'("include" & "raw"); replaced with a "warning" system message.',
|
|
119 |
['--no-file-insertion'],
|
|
120 |
{'action': 'store_false', 'default': 1,
|
|
121 |
'dest': 'file_insertion_enabled',
|
|
122 |
'validator': frontend.validate_boolean}),
|
|
123 |
('Enable directives that insert the contents of external file '
|
|
124 |
'("include" & "raw"). Enabled by default.',
|
|
125 |
['--file-insertion-enabled'],
|
|
126 |
{'action': 'store_true'}),
|
|
127 |
('Disable the "raw" directives; replaced with a "warning" '
|
|
128 |
'system message.',
|
|
129 |
['--no-raw'],
|
|
130 |
{'action': 'store_false', 'default': 1, 'dest': 'raw_enabled',
|
|
131 |
'validator': frontend.validate_boolean}),
|
|
132 |
('Enable the "raw" directive. Enabled by default.',
|
|
133 |
['--raw-enabled'],
|
|
134 |
{'action': 'store_true'}),))
|
|
135 |
|
|
136 |
config_section = 'restructuredtext parser'
|
|
137 |
config_section_dependencies = ('parsers',)
|
|
138 |
|
|
139 |
def __init__(self, rfc2822=None, inliner=None):
|
|
140 |
if rfc2822:
|
|
141 |
self.initial_state = 'RFC2822Body'
|
|
142 |
else:
|
|
143 |
self.initial_state = 'Body'
|
|
144 |
self.state_classes = states.state_classes
|
|
145 |
self.inliner = inliner
|
|
146 |
|
|
147 |
def parse(self, inputstring, document):
|
|
148 |
"""Parse `inputstring` and populate `document`, a document tree."""
|
|
149 |
self.setup_parse(inputstring, document)
|
|
150 |
self.statemachine = states.RSTStateMachine(
|
|
151 |
state_classes=self.state_classes,
|
|
152 |
initial_state=self.initial_state,
|
|
153 |
debug=document.reporter.debug_flag)
|
|
154 |
inputlines = docutils.statemachine.string2lines(
|
|
155 |
inputstring, tab_width=document.settings.tab_width,
|
|
156 |
convert_whitespace=1)
|
|
157 |
self.statemachine.run(inputlines, document, inliner=self.inliner)
|
|
158 |
self.finish_parse()
|
|
159 |
|
|
160 |
|
|
161 |
class DirectiveError(Exception):
|
|
162 |
|
|
163 |
"""
|
|
164 |
Store a message and a system message level.
|
|
165 |
|
|
166 |
To be thrown from inside directive code.
|
|
167 |
|
|
168 |
Do not instantiate directly -- use `Directive.directive_error()`
|
|
169 |
instead!
|
|
170 |
"""
|
|
171 |
|
|
172 |
def __init__(self, level, message):
|
|
173 |
"""
|
|
174 |
Initialize with message `message`. `level` is a system message level.
|
|
175 |
"""
|
|
176 |
Exception.__init__(self)
|
|
177 |
self.level = level
|
|
178 |
self.message = message
|
|
179 |
|
|
180 |
|
|
181 |
class Directive:
|
|
182 |
|
|
183 |
"""
|
|
184 |
Base class for reStructuredText directives.
|
|
185 |
|
|
186 |
The following attributes may be set by subclasses. They are
|
|
187 |
interpreted by the directive parser (which runs the directive
|
|
188 |
class):
|
|
189 |
|
|
190 |
- `required_arguments`: The number of required arguments (default:
|
|
191 |
0).
|
|
192 |
|
|
193 |
- `optional_arguments`: The number of optional arguments (default:
|
|
194 |
0).
|
|
195 |
|
|
196 |
- `final_argument_whitespace`: A boolean, indicating if the final
|
|
197 |
argument may contain whitespace (default: False).
|
|
198 |
|
|
199 |
- `option_spec`: A dictionary, mapping known option names to
|
|
200 |
conversion functions such as `int` or `float` (default: {}, no
|
|
201 |
options). Several conversion functions are defined in the
|
|
202 |
directives/__init__.py module.
|
|
203 |
|
|
204 |
Option conversion functions take a single parameter, the option
|
|
205 |
argument (a string or ``None``), validate it and/or convert it
|
|
206 |
to the appropriate form. Conversion functions may raise
|
|
207 |
`ValueError` and `TypeError` exceptions.
|
|
208 |
|
|
209 |
- `has_content`: A boolean; True if content is allowed. Client
|
|
210 |
code must handle the case where content is required but not
|
|
211 |
supplied (an empty content list will be supplied).
|
|
212 |
|
|
213 |
Arguments are normally single whitespace-separated words. The
|
|
214 |
final argument may contain whitespace and/or newlines if
|
|
215 |
`final_argument_whitespace` is True.
|
|
216 |
|
|
217 |
If the form of the arguments is more complex, specify only one
|
|
218 |
argument (either required or optional) and set
|
|
219 |
`final_argument_whitespace` to True; the client code must do any
|
|
220 |
context-sensitive parsing.
|
|
221 |
|
|
222 |
When a directive implementation is being run, the directive class
|
|
223 |
is instantiated, and the `run()` method is executed. During
|
|
224 |
instantiation, the following instance variables are set:
|
|
225 |
|
|
226 |
- ``name`` is the directive type or name (string).
|
|
227 |
|
|
228 |
- ``arguments`` is the list of positional arguments (strings).
|
|
229 |
|
|
230 |
- ``options`` is a dictionary mapping option names (strings) to
|
|
231 |
values (type depends on option conversion functions; see
|
|
232 |
`option_spec` above).
|
|
233 |
|
|
234 |
- ``content`` is a list of strings, the directive content line by line.
|
|
235 |
|
|
236 |
- ``lineno`` is the line number of the first line of the directive.
|
|
237 |
|
|
238 |
- ``content_offset`` is the line offset of the first line of the content from
|
|
239 |
the beginning of the current input. Used when initiating a nested parse.
|
|
240 |
|
|
241 |
- ``block_text`` is a string containing the entire directive.
|
|
242 |
|
|
243 |
- ``state`` is the state which called the directive function.
|
|
244 |
|
|
245 |
- ``state_machine`` is the state machine which controls the state which called
|
|
246 |
the directive function.
|
|
247 |
|
|
248 |
Directive functions return a list of nodes which will be inserted
|
|
249 |
into the document tree at the point where the directive was
|
|
250 |
encountered. This can be an empty list if there is nothing to
|
|
251 |
insert.
|
|
252 |
|
|
253 |
For ordinary directives, the list must contain body elements or
|
|
254 |
structural elements. Some directives are intended specifically
|
|
255 |
for substitution definitions, and must return a list of `Text`
|
|
256 |
nodes and/or inline elements (suitable for inline insertion, in
|
|
257 |
place of the substitution reference). Such directives must verify
|
|
258 |
substitution definition context, typically using code like this::
|
|
259 |
|
|
260 |
if not isinstance(state, states.SubstitutionDef):
|
|
261 |
error = state_machine.reporter.error(
|
|
262 |
'Invalid context: the "%s" directive can only be used '
|
|
263 |
'within a substitution definition.' % (name),
|
|
264 |
nodes.literal_block(block_text, block_text), line=lineno)
|
|
265 |
return [error]
|
|
266 |
"""
|
|
267 |
|
|
268 |
# There is a "Creating reStructuredText Directives" how-to at
|
|
269 |
# <http://docutils.sf.net/docs/howto/rst-directives.html>. If you
|
|
270 |
# update this docstring, please update the how-to as well.
|
|
271 |
|
|
272 |
required_arguments = 0
|
|
273 |
"""Number of required directive arguments."""
|
|
274 |
|
|
275 |
optional_arguments = 0
|
|
276 |
"""Number of optional arguments after the required arguments."""
|
|
277 |
|
|
278 |
final_argument_whitespace = False
|
|
279 |
"""May the final argument contain whitespace?"""
|
|
280 |
|
|
281 |
option_spec = None
|
|
282 |
"""Mapping of option names to validator functions."""
|
|
283 |
|
|
284 |
has_content = False
|
|
285 |
"""May the directive have content?"""
|
|
286 |
|
|
287 |
def __init__(self, name, arguments, options, content, lineno,
|
|
288 |
content_offset, block_text, state, state_machine):
|
|
289 |
self.name = name
|
|
290 |
self.arguments = arguments
|
|
291 |
self.options = options
|
|
292 |
self.content = content
|
|
293 |
self.lineno = lineno
|
|
294 |
self.content_offset = content_offset
|
|
295 |
self.block_text = block_text
|
|
296 |
self.state = state
|
|
297 |
self.state_machine = state_machine
|
|
298 |
|
|
299 |
def run(self):
|
|
300 |
raise NotImplementedError('Must override run() is subclass.')
|
|
301 |
|
|
302 |
# Directive errors:
|
|
303 |
|
|
304 |
def directive_error(self, level, message):
|
|
305 |
"""
|
|
306 |
Return a DirectiveError suitable for being thrown as an exception.
|
|
307 |
|
|
308 |
Call "raise self.directive_error(level, message)" from within
|
|
309 |
a directive implementation to return one single system message
|
|
310 |
at level `level`, which automatically gets the directive block
|
|
311 |
and the line number added.
|
|
312 |
|
|
313 |
You'd often use self.error(message) instead, which will
|
|
314 |
generate an ERROR-level directive error.
|
|
315 |
"""
|
|
316 |
return DirectiveError(level, message)
|
|
317 |
|
|
318 |
def debug(self, message):
|
|
319 |
return self.directive_error(0, message)
|
|
320 |
|
|
321 |
def info(self, message):
|
|
322 |
return self.directive_error(1, message)
|
|
323 |
|
|
324 |
def warning(self, message):
|
|
325 |
return self.directive_error(2, message)
|
|
326 |
|
|
327 |
def error(self, message):
|
|
328 |
return self.directive_error(3, message)
|
|
329 |
|
|
330 |
def severe(self, message):
|
|
331 |
return self.directive_error(4, message)
|
|
332 |
|
|
333 |
# Convenience methods:
|
|
334 |
|
|
335 |
def assert_has_content(self):
|
|
336 |
"""
|
|
337 |
Throw an ERROR-level DirectiveError if the directive doesn't
|
|
338 |
have contents.
|
|
339 |
"""
|
|
340 |
if not self.content:
|
|
341 |
raise self.error('Content block expected for the "%s" directive; '
|
|
342 |
'none found.' % self.name)
|
|
343 |
|
|
344 |
|
|
345 |
def convert_directive_function(directive_fn):
|
|
346 |
"""
|
|
347 |
Define & return a directive class generated from `directive_fn`.
|
|
348 |
|
|
349 |
`directive_fn` uses the old-style, functional interface.
|
|
350 |
"""
|
|
351 |
|
|
352 |
class FunctionalDirective(Directive):
|
|
353 |
|
|
354 |
option_spec = getattr(directive_fn, 'options', None)
|
|
355 |
has_content = getattr(directive_fn, 'content', False)
|
|
356 |
_argument_spec = getattr(directive_fn, 'arguments', (0, 0, False))
|
|
357 |
required_arguments, optional_arguments, final_argument_whitespace \
|
|
358 |
= _argument_spec
|
|
359 |
|
|
360 |
def run(self):
|
|
361 |
return directive_fn(
|
|
362 |
self.name, self.arguments, self.options, self.content,
|
|
363 |
self.lineno, self.content_offset, self.block_text,
|
|
364 |
self.state, self.state_machine)
|
|
365 |
|
|
366 |
# Return new-style directive.
|
|
367 |
return FunctionalDirective
|