2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
6 This package contains Docutils parser modules.
9 __docformat__
= 'reStructuredText'
12 from importlib
import import_module
14 from docutils
import Component
, frontend
17 class Parser(Component
):
19 'Generic Parser Options',
21 (('Disable directives that insert the contents of an external file; '
22 'replaced with a "warning" system message.',
23 ['--no-file-insertion'],
24 {'action': 'store_false', 'default': 1,
25 'dest': 'file_insertion_enabled',
26 'validator': frontend
.validate_boolean
}),
27 ('Enable directives that insert the contents '
28 'of an external file. (default)',
29 ['--file-insertion-enabled'],
30 {'action': 'store_true'}),
31 ('Disable the "raw" directive; '
32 'replaced with a "warning" system message.',
34 {'action': 'store_false', 'default': 1, 'dest': 'raw_enabled',
35 'validator': frontend
.validate_boolean
}),
36 ('Enable the "raw" directive. (default)',
38 {'action': 'store_true'}),
39 ('Maximal number of characters in an input line. Default 10 000.',
40 ['--line-length-limit'],
41 {'metavar': '<length>', 'type': 'int', 'default': 10000,
42 'validator': frontend
.validate_nonnegative_int
}),
44 component_type
= 'parser'
45 config_section
= 'parsers'
47 def parse(self
, inputstring
, document
):
48 """Override to parse `inputstring` into document tree `document`."""
49 raise NotImplementedError('subclass must override this method')
51 def setup_parse(self
, inputstring
, document
):
52 """Initial parse setup. Call at start of `self.parse()`."""
53 self
.inputstring
= inputstring
54 # provide fallbacks in case the document has only generic settings
55 document
.settings
.setdefault('file_insertion_enabled', False)
56 document
.settings
.setdefault('raw_enabled', False)
57 document
.settings
.setdefault('line_length_limit', 10000)
58 self
.document
= document
59 document
.reporter
.attach_observer(document
.note_parse_message
)
61 def finish_parse(self
):
62 """Finalize parse details. Call at end of `self.parse()`."""
63 self
.document
.reporter
.detach_observer(
64 self
.document
.note_parse_message
)
68 'restructuredtext': 'rst',
72 'recommonmark': 'recommonmark_wrapper',
73 'commonmark': 'recommonmark_wrapper',
74 'markdown': 'recommonmark_wrapper'}
76 def get_parser_class(parser_name
):
77 """Return the Parser class from the `parser_name` module."""
78 parser_name
= parser_name
.lower()
79 if parser_name
in _parser_aliases
:
80 parser_name
= _parser_aliases
[parser_name
]
82 module
= import_module('docutils.parsers.'+parser_name
)
84 module
= import_module(parser_name
)