1
0

roles.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. # $Id: roles.py 9037 2022-03-05 23:31:10Z milde $
  2. # Author: Edward Loper <edloper@gradient.cis.upenn.edu>
  3. # Copyright: This module has been placed in the public domain.
  4. """
  5. This module defines standard interpreted text role functions, a registry for
  6. interpreted text roles, and an API for adding to and retrieving from the
  7. registry. See also `Creating reStructuredText Interpreted Text Roles`__.
  8. __ https://docutils.sourceforge.io/docs/ref/rst/roles.html
  9. The interface for interpreted role functions is as follows::
  10. def role_fn(name, rawtext, text, lineno, inliner,
  11. options=None, content=None):
  12. code...
  13. # Set function attributes for customization:
  14. role_fn.options = ...
  15. role_fn.content = ...
  16. Parameters:
  17. - ``name`` is the local name of the interpreted text role, the role name
  18. actually used in the document.
  19. - ``rawtext`` is a string containing the entire interpreted text construct.
  20. Return it as a ``problematic`` node linked to a system message if there is a
  21. problem.
  22. - ``text`` is the interpreted text content, with backslash escapes converted
  23. to nulls (``\x00``).
  24. - ``lineno`` is the line number where the text block containing the
  25. interpreted text begins.
  26. - ``inliner`` is the Inliner object that called the role function.
  27. It defines the following useful attributes: ``reporter``,
  28. ``problematic``, ``memo``, ``parent``, ``document``.
  29. - ``options``: A dictionary of directive options for customization, to be
  30. interpreted by the role function. Used for additional attributes for the
  31. generated elements and other functionality.
  32. - ``content``: A list of strings, the directive content for customization
  33. ("role" directive). To be interpreted by the role function.
  34. Function attributes for customization, interpreted by the "role" directive:
  35. - ``options``: A dictionary, mapping known option names to conversion
  36. functions such as `int` or `float`. ``None`` or an empty dict implies no
  37. options to parse. Several directive option conversion functions are defined
  38. in the `directives` module.
  39. All role functions implicitly support the "class" option, unless disabled
  40. with an explicit ``{'class': None}``.
  41. - ``content``: A boolean; true if content is allowed. Client code must handle
  42. the case where content is required but not supplied (an empty content list
  43. will be supplied).
  44. Note that unlike directives, the "arguments" function attribute is not
  45. supported for role customization. Directive arguments are handled by the
  46. "role" directive itself.
  47. Interpreted role functions return a tuple of two values:
  48. - A list of nodes which will be inserted into the document tree at the
  49. point where the interpreted role was encountered (can be an empty
  50. list).
  51. - A list of system messages, which will be inserted into the document tree
  52. immediately after the end of the current inline block (can also be empty).
  53. """
  54. __docformat__ = 'reStructuredText'
  55. from docutils import nodes
  56. from docutils.parsers.rst import directives
  57. from docutils.parsers.rst.languages import en as _fallback_language_module
  58. from docutils.utils.code_analyzer import Lexer, LexerError
  59. DEFAULT_INTERPRETED_ROLE = 'title-reference'
  60. """The canonical name of the default interpreted role.
  61. This role is used when no role is specified for a piece of interpreted text.
  62. """
  63. _role_registry = {}
  64. """Mapping of canonical role names to role functions.
  65. Language-dependent role names are defined in the ``language`` subpackage.
  66. """
  67. _roles = {}
  68. """Mapping of local or language-dependent interpreted text role names to role
  69. functions."""
  70. def role(role_name, language_module, lineno, reporter):
  71. """
  72. Locate and return a role function from its language-dependent name, along
  73. with a list of system messages.
  74. If the role is not found in the current language, check English. Return a
  75. 2-tuple: role function (``None`` if the named role cannot be found) and a
  76. list of system messages.
  77. """
  78. normname = role_name.lower()
  79. messages = []
  80. msg_text = []
  81. if normname in _roles:
  82. return _roles[normname], messages
  83. if role_name:
  84. canonicalname = None
  85. try:
  86. canonicalname = language_module.roles[normname]
  87. except AttributeError as error:
  88. msg_text.append('Problem retrieving role entry from language '
  89. 'module %r: %s.' % (language_module, error))
  90. except KeyError:
  91. msg_text.append('No role entry for "%s" in module "%s".'
  92. % (role_name, language_module.__name__))
  93. else:
  94. canonicalname = DEFAULT_INTERPRETED_ROLE
  95. # If we didn't find it, try English as a fallback.
  96. if not canonicalname:
  97. try:
  98. canonicalname = _fallback_language_module.roles[normname]
  99. msg_text.append('Using English fallback for role "%s".'
  100. % role_name)
  101. except KeyError:
  102. msg_text.append('Trying "%s" as canonical role name.'
  103. % role_name)
  104. # The canonical name should be an English name, but just in case:
  105. canonicalname = normname
  106. # Collect any messages that we generated.
  107. if msg_text:
  108. message = reporter.info('\n'.join(msg_text), line=lineno)
  109. messages.append(message)
  110. # Look the role up in the registry, and return it.
  111. if canonicalname in _role_registry:
  112. role_fn = _role_registry[canonicalname]
  113. register_local_role(normname, role_fn)
  114. return role_fn, messages
  115. return None, messages # Error message will be generated by caller.
  116. def register_canonical_role(name, role_fn):
  117. """
  118. Register an interpreted text role by its canonical name.
  119. :Parameters:
  120. - `name`: The canonical name of the interpreted role.
  121. - `role_fn`: The role function. See the module docstring.
  122. """
  123. set_implicit_options(role_fn)
  124. _role_registry[name.lower()] = role_fn
  125. def register_local_role(name, role_fn):
  126. """
  127. Register an interpreted text role by its local or language-dependent name.
  128. :Parameters:
  129. - `name`: The local or language-dependent name of the interpreted role.
  130. - `role_fn`: The role function. See the module docstring.
  131. """
  132. set_implicit_options(role_fn)
  133. _roles[name.lower()] = role_fn
  134. def set_implicit_options(role_fn):
  135. """
  136. Add customization options to role functions, unless explicitly set or
  137. disabled.
  138. """
  139. if not hasattr(role_fn, 'options') or role_fn.options is None:
  140. role_fn.options = {'class': directives.class_option}
  141. elif 'class' not in role_fn.options:
  142. role_fn.options['class'] = directives.class_option
  143. def register_generic_role(canonical_name, node_class):
  144. """For roles which simply wrap a given `node_class` around the text."""
  145. role = GenericRole(canonical_name, node_class)
  146. register_canonical_role(canonical_name, role)
  147. class GenericRole:
  148. """
  149. Generic interpreted text role.
  150. The interpreted text is simply wrapped with the provided node class.
  151. """
  152. def __init__(self, role_name, node_class):
  153. self.name = role_name
  154. self.node_class = node_class
  155. def __call__(self, role, rawtext, text, lineno, inliner,
  156. options=None, content=None):
  157. options = normalized_role_options(options)
  158. return [self.node_class(rawtext, text, **options)], []
  159. class CustomRole:
  160. """Wrapper for custom interpreted text roles."""
  161. def __init__(self, role_name, base_role, options=None, content=None):
  162. self.name = role_name
  163. self.base_role = base_role
  164. self.options = getattr(base_role, 'options', None)
  165. self.content = getattr(base_role, 'content', None)
  166. self.supplied_options = options
  167. self.supplied_content = content
  168. def __call__(self, role, rawtext, text, lineno, inliner,
  169. options=None, content=None):
  170. opts = normalized_role_options(self.supplied_options)
  171. try:
  172. opts.update(options)
  173. except TypeError: # options may be ``None``
  174. pass
  175. # pass concatenation of content from instance and call argument:
  176. supplied_content = self.supplied_content or []
  177. content = content or []
  178. delimiter = ['\n'] if supplied_content and content else []
  179. return self.base_role(role, rawtext, text, lineno, inliner,
  180. options=opts,
  181. content=supplied_content+delimiter+content)
  182. def generic_custom_role(role, rawtext, text, lineno, inliner,
  183. options=None, content=None):
  184. """Base for custom roles if no other base role is specified."""
  185. # Once nested inline markup is implemented, this and other methods should
  186. # recursively call inliner.nested_parse().
  187. options = normalized_role_options(options)
  188. return [nodes.inline(rawtext, text, **options)], []
  189. generic_custom_role.options = {'class': directives.class_option}
  190. ######################################################################
  191. # Define and register the standard roles:
  192. ######################################################################
  193. register_generic_role('abbreviation', nodes.abbreviation)
  194. register_generic_role('acronym', nodes.acronym)
  195. register_generic_role('emphasis', nodes.emphasis)
  196. register_generic_role('literal', nodes.literal)
  197. register_generic_role('strong', nodes.strong)
  198. register_generic_role('subscript', nodes.subscript)
  199. register_generic_role('superscript', nodes.superscript)
  200. register_generic_role('title-reference', nodes.title_reference)
  201. def pep_reference_role(role, rawtext, text, lineno, inliner,
  202. options=None, content=None):
  203. options = normalized_role_options(options)
  204. try:
  205. pepnum = int(nodes.unescape(text))
  206. if pepnum < 0 or pepnum > 9999:
  207. raise ValueError
  208. except ValueError:
  209. msg = inliner.reporter.error(
  210. 'PEP number must be a number from 0 to 9999; "%s" is invalid.'
  211. % text, line=lineno)
  212. prb = inliner.problematic(rawtext, rawtext, msg)
  213. return [prb], [msg]
  214. # Base URL mainly used by inliner.pep_reference; so this is correct:
  215. ref = (inliner.document.settings.pep_base_url
  216. + inliner.document.settings.pep_file_url_template % pepnum)
  217. return [nodes.reference(rawtext, 'PEP ' + text, refuri=ref, **options)], []
  218. register_canonical_role('pep-reference', pep_reference_role)
  219. def rfc_reference_role(role, rawtext, text, lineno, inliner,
  220. options=None, content=None):
  221. options = normalized_role_options(options)
  222. if "#" in text:
  223. rfcnum, section = nodes.unescape(text).split("#", 1)
  224. else:
  225. rfcnum, section = nodes.unescape(text), None
  226. try:
  227. rfcnum = int(rfcnum)
  228. if rfcnum < 1:
  229. raise ValueError
  230. except ValueError:
  231. msg = inliner.reporter.error(
  232. 'RFC number must be a number greater than or equal to 1; '
  233. '"%s" is invalid.' % text, line=lineno)
  234. prb = inliner.problematic(rawtext, rawtext, msg)
  235. return [prb], [msg]
  236. # Base URL mainly used by inliner.rfc_reference, so this is correct:
  237. ref = inliner.document.settings.rfc_base_url + inliner.rfc_url % rfcnum
  238. if section is not None:
  239. ref += "#" + section
  240. node = nodes.reference(rawtext, 'RFC '+str(rfcnum), refuri=ref, **options)
  241. return [node], []
  242. register_canonical_role('rfc-reference', rfc_reference_role)
  243. def raw_role(role, rawtext, text, lineno, inliner, options=None, content=None):
  244. options = normalized_role_options(options)
  245. if not inliner.document.settings.raw_enabled:
  246. msg = inliner.reporter.warning('raw (and derived) roles disabled')
  247. prb = inliner.problematic(rawtext, rawtext, msg)
  248. return [prb], [msg]
  249. if 'format' not in options:
  250. msg = inliner.reporter.error(
  251. 'No format (Writer name) is associated with this role: "%s".\n'
  252. 'The "raw" role cannot be used directly.\n'
  253. 'Instead, use the "role" directive to create a new role with '
  254. 'an associated format.' % role, line=lineno)
  255. prb = inliner.problematic(rawtext, rawtext, msg)
  256. return [prb], [msg]
  257. node = nodes.raw(rawtext, nodes.unescape(text, True), **options)
  258. node.source, node.line = inliner.reporter.get_source_and_line(lineno)
  259. return [node], []
  260. raw_role.options = {'format': directives.unchanged}
  261. register_canonical_role('raw', raw_role)
  262. def code_role(role, rawtext, text, lineno, inliner,
  263. options=None, content=None):
  264. options = normalized_role_options(options)
  265. language = options.get('language', '')
  266. classes = ['code']
  267. if 'classes' in options:
  268. classes.extend(options['classes'])
  269. if language and language not in classes:
  270. classes.append(language)
  271. try:
  272. tokens = Lexer(nodes.unescape(text, True), language,
  273. inliner.document.settings.syntax_highlight)
  274. except LexerError as error:
  275. msg = inliner.reporter.warning(error)
  276. prb = inliner.problematic(rawtext, rawtext, msg)
  277. return [prb], [msg]
  278. node = nodes.literal(rawtext, '', classes=classes)
  279. # analyse content and add nodes for every token
  280. for classes, value in tokens:
  281. if classes:
  282. node += nodes.inline(value, value, classes=classes)
  283. else:
  284. # insert as Text to decrease the verbosity of the output
  285. node += nodes.Text(value)
  286. return [node], []
  287. code_role.options = {'language': directives.unchanged}
  288. register_canonical_role('code', code_role)
  289. def math_role(role, rawtext, text, lineno, inliner,
  290. options=None, content=None):
  291. options = normalized_role_options(options)
  292. text = nodes.unescape(text, True) # raw text without inline role markup
  293. node = nodes.math(rawtext, text, **options)
  294. return [node], []
  295. register_canonical_role('math', math_role)
  296. ######################################################################
  297. # Register roles that are currently unimplemented.
  298. ######################################################################
  299. def unimplemented_role(role, rawtext, text, lineno, inliner,
  300. options=None, content=None):
  301. msg = inliner.reporter.error(
  302. 'Interpreted text role "%s" not implemented.' % role, line=lineno)
  303. prb = inliner.problematic(rawtext, rawtext, msg)
  304. return [prb], [msg]
  305. register_canonical_role('index', unimplemented_role)
  306. register_canonical_role('named-reference', unimplemented_role)
  307. register_canonical_role('anonymous-reference', unimplemented_role)
  308. register_canonical_role('uri-reference', unimplemented_role)
  309. register_canonical_role('footnote-reference', unimplemented_role)
  310. register_canonical_role('citation-reference', unimplemented_role)
  311. register_canonical_role('substitution-reference', unimplemented_role)
  312. register_canonical_role('target', unimplemented_role)
  313. # This should remain unimplemented, for testing purposes:
  314. register_canonical_role('restructuredtext-unimplemented-role',
  315. unimplemented_role)
  316. def set_classes(options):
  317. """Deprecated. Obsoleted by ``normalized_role_options()``."""
  318. # TODO: Change use in directives.py and uncomment.
  319. # warnings.warn('The auxiliary function roles.set_classes() is obsoleted'
  320. # ' by roles.normalized_role_options() and will be removed'
  321. # ' in Docutils 0.21 or later', DeprecationWarning, stacklevel=2)
  322. if options and 'class' in options:
  323. assert 'classes' not in options
  324. options['classes'] = options['class']
  325. del options['class']
  326. def normalized_role_options(options):
  327. """
  328. Return normalized dictionary of role options.
  329. * ``None`` is replaced by an empty dictionary.
  330. * The key 'class' is renamed to 'classes'.
  331. """
  332. if options is None:
  333. return {}
  334. result = options.copy()
  335. if 'class' in result:
  336. assert 'classes' not in result
  337. result['classes'] = result['class']
  338. del result['class']
  339. return result