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: __init__.py 10382 2026-07-14 13:32:05Z milde $ 

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 {'dest': 'file_insertion_enabled', 'action': 'store_false', 

33 'default': True, 'validator': frontend.validate_boolean}), 

34 ('Enable directives that insert the contents ' 

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

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

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

38 ('Disable the "raw" directive; ' 

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

40 ['--no-raw'], 

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

42 'validator': frontend.validate_boolean}), 

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

44 ['--raw-enabled'], 

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

46 ('Maximal number of characters in an input line. (default 10 000)', 

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

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

49 'validator': frontend.validate_nonnegative_int}), 

50 ('Keep IDs backwards compatible. (default)', 

51 ['--legacy-ids'], 

52 {'action': 'store_true', 'default': True, 

53 'validator': frontend.validate_boolean}), 

54 ('Generate IDs for implicit targets only if required; ' 

55 'no IDs for external and indirect targets.', 

56 ['--lazy-ids'], 

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

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

59 ['--validate'], 

60 {'action': 'store_true', 'validator': frontend.validate_boolean}), 

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

62 ['--no-validation'], 

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

64 ) 

65 ) 

66 component_type: Final = 'parser' 

67 config_section: Final = 'parsers' 

68 

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

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

71 

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

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

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

75 

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

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

78 self.inputstring = inputstring 

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

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

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

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

83 self.document = document 

84 document.reporter.attach_observer(document.note_parse_message) 

85 

86 def finish_parse(self) -> None: 

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

88 self.document.reporter.detach_observer( 

89 self.document.note_parse_message) 

90 

91 

92PARSER_ALIASES = { # short names for known parsers 

93 'null': 'docutils.parsers.null', 

94 # reStructuredText 

95 'rst': 'docutils.parsers.rst', 

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

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

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

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

100 # Docutils XML 

101 'docutils_xml': 'docutils.parsers.docutils_xml', 

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

103 # 3rd-party Markdown parsers 

104 'myst': 'myst_parser.docutils_', 

105 # 'pycmark': works out of the box (with `legacy_ids`) 

106 # dispatcher for 3rd-party Markdown parsers 

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

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

109 } 

110 

111 

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

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

114 name = parser_name.lower() 

115 

116 try: 

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

118 except ImportError as err: 

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

120 return module.Parser