Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/docutils/parsers/__init__.py: 89%
27 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:06 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:06 +0000
1# $Id$
2# Author: David Goodger <goodger@python.org>
3# Copyright: This module has been placed in the public domain.
5"""
6This package contains Docutils parser modules.
7"""
9__docformat__ = 'reStructuredText'
11from importlib import import_module
13from docutils import Component, frontend
16class Parser(Component):
17 settings_spec = (
18 'Generic Parser Options',
19 None,
20 (('Disable directives that insert the contents of an external file; '
21 'replaced with a "warning" system message.',
22 ['--no-file-insertion'],
23 {'action': 'store_false', 'default': 1,
24 'dest': 'file_insertion_enabled',
25 'validator': frontend.validate_boolean}),
26 ('Enable directives that insert the contents '
27 'of an external file. (default)',
28 ['--file-insertion-enabled'],
29 {'action': 'store_true'}),
30 ('Disable the "raw" directive; '
31 'replaced with a "warning" system message.',
32 ['--no-raw'],
33 {'action': 'store_false', 'default': 1, 'dest': 'raw_enabled',
34 'validator': frontend.validate_boolean}),
35 ('Enable the "raw" directive. (default)',
36 ['--raw-enabled'],
37 {'action': 'store_true'}),
38 ('Maximal number of characters in an input line. Default 10 000.',
39 ['--line-length-limit'],
40 {'metavar': '<length>', 'type': 'int', 'default': 10000,
41 'validator': frontend.validate_nonnegative_int}),
42 )
43 )
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)
67_parser_aliases = { # short names for known parsers
68 'null': 'docutils.parsers.null',
69 # reStructuredText
70 'rst': 'docutils.parsers.rst',
71 'restructuredtext': 'docutils.parsers.rst',
72 'rest': 'docutils.parsers.rst',
73 'restx': 'docutils.parsers.rst',
74 'rtxt': 'docutils.parsers.rst',
75 # 3rd-party Markdown parsers
76 'recommonmark': 'docutils.parsers.recommonmark_wrapper',
77 'myst': 'myst_parser.docutils_',
78 # 'pycmark': works out of the box
79 # dispatcher for 3rd-party Markdown parsers
80 'commonmark': 'docutils.parsers.commonmark_wrapper',
81 'markdown': 'docutils.parsers.commonmark_wrapper',
82 }
85def get_parser_class(parser_name):
86 """Return the Parser class from the `parser_name` module."""
87 name = parser_name.lower()
88 try:
89 module = import_module(_parser_aliases.get(name, name))
90 except ImportError as err:
91 raise ImportError(f'Parser "{parser_name}" not found. {err}')
92 return module.Parser