Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/docutils/parsers/__init__.py: 78%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

36 statements  

1# $Id$ 

2# Author: David Goodger <goodger@python.org> 

3# Copyright: This module has been placed in the public domain. 

4 

5""" 

6This package contains Docutils parser modules. 

7""" 

8 

9from __future__ import annotations 

10 

11__docformat__ = 'reStructuredText' 

12 

13import importlib 

14 

15from docutils import Component, frontend, transforms 

16 

17TYPE_CHECKING = False 

18if TYPE_CHECKING: 

19 from typing import Final 

20 

21 from docutils import nodes 

22 from docutils.transforms import Transform 

23 

24 

25class Parser(Component): 

26 settings_spec = ( 

27 'Generic Parser Options', 

28 None, 

29 (('Disable directives that insert the contents of an external file; ' 

30 'replaced with a "warning" system message.', 

31 ['--no-file-insertion'], 

32 {'action': 'store_false', 'default': True, 

33 'dest': 'file_insertion_enabled', 

34 'validator': frontend.validate_boolean}), 

35 ('Enable directives that insert the contents ' 

36 'of an external file. (default)', 

37 ['--file-insertion-enabled'], 

38 {'action': 'store_true'}), 

39 ('Disable the "raw" directive; ' 

40 'replaced with a "warning" system message.', 

41 ['--no-raw'], 

42 {'action': 'store_false', 'default': True, 'dest': 'raw_enabled', 

43 'validator': frontend.validate_boolean}), 

44 ('Enable the "raw" directive. (default)', 

45 ['--raw-enabled'], 

46 {'action': 'store_true'}), 

47 ('Maximal number of characters in an input line. Default 10 000.', 

48 ['--line-length-limit'], 

49 {'metavar': '<length>', 'type': 'int', 'default': 10_000, 

50 'validator': frontend.validate_nonnegative_int}), 

51 ('Validate the document tree after parsing.', 

52 ['--validate'], 

53 {'action': 'store_true', 

54 'validator': frontend.validate_boolean}), 

55 ('Do not validate the document tree. (default)', 

56 ['--no-validation'], 

57 {'action': 'store_false', 'dest': 'validate'}), 

58 ) 

59 ) 

60 component_type: Final = 'parser' 

61 config_section: Final = 'parsers' 

62 

63 def get_transforms(self) -> list[type[Transform]]: 

64 return super().get_transforms() + [transforms.universal.Validate] 

65 

66 def parse(self, inputstring: str, document: nodes.document) -> None: 

67 """Override to parse `inputstring` into document tree `document`.""" 

68 raise NotImplementedError('subclass must override this method') 

69 

70 def setup_parse(self, inputstring: str, document: nodes.document) -> None: 

71 """Initial parse setup. Call at start of `self.parse()`.""" 

72 self.inputstring = inputstring 

73 # provide fallbacks in case the document has only generic settings 

74 document.settings.setdefault('file_insertion_enabled', False) 

75 document.settings.setdefault('raw_enabled', False) 

76 document.settings.setdefault('line_length_limit', 10_000) 

77 self.document = document 

78 document.reporter.attach_observer(document.note_parse_message) 

79 

80 def finish_parse(self) -> None: 

81 """Finalize parse details. Call at end of `self.parse()`.""" 

82 self.document.reporter.detach_observer( 

83 self.document.note_parse_message) 

84 

85 

86PARSER_ALIASES = { # short names for known parsers 

87 'null': 'docutils.parsers.null', 

88 # reStructuredText 

89 'rst': 'docutils.parsers.rst', 

90 'restructuredtext': 'docutils.parsers.rst', 

91 'rest': 'docutils.parsers.rst', 

92 'restx': 'docutils.parsers.rst', 

93 'rtxt': 'docutils.parsers.rst', 

94 # Docutils XML 

95 'docutils_xml': 'docutils.parsers.docutils_xml', 

96 'xml': 'docutils.parsers.docutils_xml', 

97 # 3rd-party Markdown parsers 

98 'recommonmark': 'docutils.parsers.recommonmark_wrapper', 

99 'myst': 'myst_parser.docutils_', 

100 # 'pycmark': works out of the box 

101 # dispatcher for 3rd-party Markdown parsers 

102 'commonmark': 'docutils.parsers.commonmark_wrapper', 

103 'markdown': 'docutils.parsers.commonmark_wrapper', 

104 } 

105 

106 

107def get_parser_class(parser_name: str) -> type[Parser]: 

108 """Return the Parser class from the `parser_name` module.""" 

109 name = parser_name.lower() 

110 

111 try: 

112 module = importlib.import_module(PARSER_ALIASES.get(name, name)) 

113 except ImportError as err: 

114 raise ImportError(f'Parser "{parser_name}" not found. {err}') from err 

115 return module.Parser