| 1 |
.. include:: ../header.rst |
| 2 |
|
| 3 |
================================================== |
| 4 |
Creating reStructuredText Interpreted Text Roles |
| 5 |
================================================== |
| 6 |
|
| 7 |
:Authors: David Goodger |
| 8 |
:Contact: [email protected] |
| 9 |
:Date: $Date: 2024-08-15 10:43:38 +0200 (Do, 15. Aug 2024) $ |
| 10 |
:Revision: $Revision: 9906 $ |
| 11 |
:Copyright: This document has been placed in the public domain. |
| 12 |
|
| 13 |
Interpreted text roles are an extension mechanism for inline markup in |
| 14 |
reStructuredText. This document aims to make the creation of new |
| 15 |
roles as easy and understandable as possible. |
| 16 |
|
| 17 |
Standard roles are described in `reStructuredText Interpreted Text |
| 18 |
Roles`_. See the `Interpreted Text`_ section in the `reStructuredText |
| 19 |
Markup Specification`_ for syntax details. |
| 20 |
|
| 21 |
.. _reStructuredText Interpreted Text Roles: ../ref/rst/roles.html |
| 22 |
.. _Interpreted Text: |
| 23 |
../ref/rst/restructuredtext.html#interpreted-text |
| 24 |
.. _reStructuredText Markup Specification: |
| 25 |
../ref/rst/restructuredtext.html |
| 26 |
|
| 27 |
|
| 28 |
.. contents:: |
| 29 |
|
| 30 |
|
| 31 |
Define the Role Function |
| 32 |
======================== |
| 33 |
|
| 34 |
The role function creates and returns inline elements (nodes) and does |
| 35 |
any additional processing required. Its signature is as follows:: |
| 36 |
|
| 37 |
def role_fn(name, rawtext, text, lineno, inliner, |
| 38 |
options=None, content=None): |
| 39 |
code... |
| 40 |
|
| 41 |
# Optional function attributes for customization: |
| 42 |
role_fn.options = ... |
| 43 |
role_fn.content = ... |
| 44 |
|
| 45 |
Function attributes are described below (see `Specify Role Function |
| 46 |
Options and Content`_). The role function parameters are as follows: |
| 47 |
|
| 48 |
* ``name``: The local name of the interpreted role, the role name |
| 49 |
actually used in the document. |
| 50 |
|
| 51 |
* ``rawtext``: A string containing the entire interpreted text input, |
| 52 |
including the role and markup. Return it as a ``problematic`` node |
| 53 |
linked to a system message if a problem is encountered. |
| 54 |
|
| 55 |
* ``text``: The interpreted text content. |
| 56 |
|
| 57 |
* ``lineno``: The line number where the text block containing the |
| 58 |
interpreted text begins. |
| 59 |
|
| 60 |
* ``inliner``: The ``docutils.parsers.rst.states.Inliner`` object that |
| 61 |
called role_fn. It contains the several attributes useful for error |
| 62 |
reporting and document tree access. |
| 63 |
|
| 64 |
* ``options``: A dictionary of directive options for customization |
| 65 |
(from the `"role" directive`_), to be interpreted by the role |
| 66 |
function. Used for additional attributes for the generated elements |
| 67 |
and other functionality. |
| 68 |
|
| 69 |
* ``content``: A list of strings, the directive content for |
| 70 |
customization (from the `"role" directive`_). To be interpreted by |
| 71 |
the role function. |
| 72 |
|
| 73 |
Role functions return a tuple of two values: |
| 74 |
|
| 75 |
* A list of nodes which will be inserted into the document tree at the |
| 76 |
point where the interpreted role was encountered (can be an empty |
| 77 |
list). |
| 78 |
|
| 79 |
* A list of system messages, which will be inserted into the document tree |
| 80 |
immediately after the end of the current block (can also be empty). |
| 81 |
|
| 82 |
|
| 83 |
Specify Role Function Options and Content |
| 84 |
========================================= |
| 85 |
|
| 86 |
Function attributes are for customization, and are interpreted by the |
| 87 |
`"role" directive`_. If unspecified, role function attributes are |
| 88 |
assumed to have the value ``None``. Two function attributes are |
| 89 |
recognized: |
| 90 |
|
| 91 |
- ``options``: The option specification. All role functions |
| 92 |
implicitly support the "class" option, unless disabled with an |
| 93 |
explicit ``{'class': None}``. |
| 94 |
|
| 95 |
An option specification must be defined detailing the options |
| 96 |
available to the "role" directive. An option spec is a mapping of |
| 97 |
option name to conversion function; conversion functions are applied |
| 98 |
to each option value to check validity and convert them to the |
| 99 |
expected type. Python's built-in conversion functions are often |
| 100 |
usable for this, such as ``int``, ``float``, and ``bool`` (included |
| 101 |
in Python from version 2.2.1). Other useful conversion functions |
| 102 |
are included in the ``docutils.parsers.rst.directives`` package. |
| 103 |
For further details, see `Creating reStructuredText Directives`_. |
| 104 |
|
| 105 |
- ``content``: A boolean; true if "role" directive content is allowed. |
| 106 |
Role functions must handle the case where content is required but |
| 107 |
not supplied (an empty content list will be supplied). |
| 108 |
|
| 109 |
As of this writing, no roles accept directive content. |
| 110 |
|
| 111 |
Note that unlike directives, the "arguments" function attribute is not |
| 112 |
supported for role customization. Directive arguments are handled by |
| 113 |
the "role" directive itself. |
| 114 |
|
| 115 |
.. _"role" directive: ../ref/rst/directives.html#role |
| 116 |
.. _Creating reStructuredText Directives: |
| 117 |
rst-directives.html#specify-directive-arguments-options-and-content |
| 118 |
|
| 119 |
|
| 120 |
Register the Role |
| 121 |
================= |
| 122 |
|
| 123 |
If the role is a general-use addition to the Docutils core, it must be |
| 124 |
registered with the parser and language mappings added: |
| 125 |
|
| 126 |
1. Register the new role using the canonical name:: |
| 127 |
|
| 128 |
from docutils.parsers.rst import roles |
| 129 |
roles.register_canonical_role(name, role_function) |
| 130 |
|
| 131 |
This code is normally placed immediately after the definition of |
| 132 |
the role function. |
| 133 |
|
| 134 |
2. Add an entry to the ``roles`` dictionary in |
| 135 |
``docutils/parsers/rst/languages/en.py`` for the role, mapping the |
| 136 |
English name to the canonical name (both lowercase). Usually the |
| 137 |
English name and the canonical name are the same. Abbreviations |
| 138 |
and other aliases may also be added here. |
| 139 |
|
| 140 |
3. Update all the other language modules as well. For languages in |
| 141 |
which you are proficient, please add translations. For other |
| 142 |
languages, add the English role name plus "(translation required)". |
| 143 |
|
| 144 |
If the role is application-specific, use the ``register_local_role`` |
| 145 |
function:: |
| 146 |
|
| 147 |
from docutils.parsers.rst import roles |
| 148 |
roles.register_local_role(name, role_function) |
| 149 |
|
| 150 |
|
| 151 |
Examples |
| 152 |
======== |
| 153 |
|
| 154 |
For the most direct and accurate information, "Use the Source, Luke!". |
| 155 |
All standard roles are documented in `reStructuredText Interpreted |
| 156 |
Text Roles`_, and the source code implementing them is located in the |
| 157 |
``docutils/parsers/rst/roles.py`` module. Several representative |
| 158 |
roles are described below. |
| 159 |
|
| 160 |
|
| 161 |
Generic Roles |
| 162 |
------------- |
| 163 |
|
| 164 |
Many roles simply wrap a given element around the text. There's a |
| 165 |
special helper function, ``register_generic_role``, which generates a |
| 166 |
role function from the canonical role name and node class:: |
| 167 |
|
| 168 |
register_generic_role('emphasis', nodes.emphasis) |
| 169 |
|
| 170 |
For the implementation of ``register_generic_role``, see the |
| 171 |
``docutils.parsers.rst.roles`` module. |
| 172 |
|
| 173 |
|
| 174 |
RFC Reference Role |
| 175 |
------------------ |
| 176 |
|
| 177 |
This role allows easy references to RFCs_ (Request For Comments |
| 178 |
documents) by automatically providing the base URL, |
| 179 |
http://www.faqs.org/rfcs/, and appending the RFC document itself |
| 180 |
(rfcXXXX.html, where XXXX is the RFC number). For example:: |
| 181 |
|
| 182 |
See :RFC:`2822` for information about email headers. |
| 183 |
|
| 184 |
This is equivalent to:: |
| 185 |
|
| 186 |
See `RFC 2822`__ for information about email headers. |
| 187 |
|
| 188 |
__ http://www.faqs.org/rfcs/rfc2822.html |
| 189 |
|
| 190 |
Here is the implementation of the role:: |
| 191 |
|
| 192 |
def rfc_reference_role(role, rawtext, text, lineno, inliner, |
| 193 |
options=None, content=None): |
| 194 |
if "#" in text: |
| 195 |
rfcnum, section = utils.unescape(text).split("#", 1) |
| 196 |
else: |
| 197 |
rfcnum, section = utils.unescape(text), None |
| 198 |
try: |
| 199 |
rfcnum = int(rfcnum) |
| 200 |
if rfcnum < 1: |
| 201 |
raise ValueError |
| 202 |
except ValueError: |
| 203 |
msg = inliner.reporter.error( |
| 204 |
'RFC number must be a number greater than or equal to 1; ' |
| 205 |
'"%s" is invalid.' % text, line=lineno) |
| 206 |
prb = inliner.problematic(rawtext, rawtext, msg) |
| 207 |
return [prb], [msg] |
| 208 |
# Base URL mainly used by inliner.rfc_reference, so this is correct: |
| 209 |
ref = inliner.document.settings.rfc_base_url + inliner.rfc_url % rfcnum |
| 210 |
if section is not None: |
| 211 |
ref += "#"+section |
| 212 |
options = normalize_role_options(options) |
| 213 |
node = nodes.reference(rawtext, 'RFC ' + str(rfcnum), refuri=ref, |
| 214 |
**options) |
| 215 |
return [node], [] |
| 216 |
|
| 217 |
register_canonical_role('rfc-reference', rfc_reference_role) |
| 218 |
|
| 219 |
Noteworthy in the code above are: |
| 220 |
|
| 221 |
1. The interpreted text itself should contain the RFC number. The |
| 222 |
``try`` clause verifies by converting it to an integer. If the |
| 223 |
conversion fails, the ``except`` clause is executed: a system |
| 224 |
message is generated, the entire interpreted text construct (in |
| 225 |
``rawtext``) is wrapped in a ``problematic`` node (linked to the |
| 226 |
system message), and the two are returned. |
| 227 |
|
| 228 |
2. The RFC reference itself is constructed from a stock URI, set as |
| 229 |
the "refuri" attribute of a "reference" element. |
| 230 |
|
| 231 |
3. The ``options`` function parameter, a dictionary, may contain a |
| 232 |
"class" customization attribute; it is interpreted and replaced |
| 233 |
with a "classes" attribute by the ``set_classes()`` function. The |
| 234 |
resulting "classes" attribute is passed through to the "reference" |
| 235 |
element node constructor. |
| 236 |
|
| 237 |
.. _RFCs: http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=rfc&action=Search&sourceid=Mozilla-search |