| 1 |
.. include:: ../header.rst |
| 2 |
|
| 3 |
========================== |
| 4 |
Docutils_ Hacker's Guide |
| 5 |
========================== |
| 6 |
|
| 7 |
:Author: Lea Wiemann |
| 8 |
:Contact: [email protected] |
| 9 |
:Revision: $Revision: 9906 $ |
| 10 |
:Date: $Date: 2024-08-15 10:43:38 +0200 (Do, 15. Aug 2024) $ |
| 11 |
:Copyright: This document has been placed in the public domain. |
| 12 |
|
| 13 |
:Abstract: This is the introduction to Docutils for all persons who |
| 14 |
want to extend Docutils in some way. |
| 15 |
:Prerequisites: You have used reStructuredText_ and played around with |
| 16 |
the `Docutils front-end tools`_ before. Some (basic) Python |
| 17 |
knowledge is certainly helpful (though not necessary, strictly |
| 18 |
speaking). |
| 19 |
|
| 20 |
.. _Docutils: https://docutils.sourceforge.io/ |
| 21 |
.. _reStructuredText: https://docutils.sourceforge.io/rst.html |
| 22 |
.. _Docutils front-end tools: ../user/tools.html |
| 23 |
|
| 24 |
.. contents:: |
| 25 |
|
| 26 |
|
| 27 |
Overview of the Docutils Architecture |
| 28 |
===================================== |
| 29 |
|
| 30 |
To give you an understanding of the Docutils architecture, we'll dive |
| 31 |
right into the internals using a practical example. |
| 32 |
|
| 33 |
Consider the following reStructuredText file:: |
| 34 |
|
| 35 |
My *favorite* language is Python_. |
| 36 |
|
| 37 |
.. _Python: https://www.python.org/ |
| 38 |
|
| 39 |
Using the ``rst2html`` front-end tool, you would get an HTML output |
| 40 |
which looks like this:: |
| 41 |
|
| 42 |
[uninteresting HTML code removed] |
| 43 |
<body> |
| 44 |
<div class="document"> |
| 45 |
<p>My <em>favorite</em> language is <a class="reference" href="https://www.python.org/">Python</a>.</p> |
| 46 |
</div> |
| 47 |
</body> |
| 48 |
</html> |
| 49 |
|
| 50 |
While this looks very simple, it's enough to illustrate all internal |
| 51 |
processing stages of Docutils. Let's see how this document is |
| 52 |
processed from the reStructuredText source to the final HTML output: |
| 53 |
|
| 54 |
|
| 55 |
Reading the Document |
| 56 |
-------------------- |
| 57 |
|
| 58 |
The **Reader** reads the document from the source file and passes it |
| 59 |
to the parser (see below). The default reader is the standalone |
| 60 |
reader (``docutils/readers/standalone.py``) which just reads the input |
| 61 |
data from a single text file. Unless you want to do really fancy |
| 62 |
things, there is no need to change that. |
| 63 |
|
| 64 |
Since you probably won't need to touch readers, we will just move on |
| 65 |
to the next stage: |
| 66 |
|
| 67 |
|
| 68 |
Parsing the Document |
| 69 |
-------------------- |
| 70 |
|
| 71 |
The **Parser** analyzes the the input document and creates a **node |
| 72 |
tree** representation. In this case we are using the |
| 73 |
**reStructuredText parser**: |
| 74 |
|
| 75 |
.. code:: python |
| 76 |
|
| 77 |
from docutils import frontend, utils |
| 78 |
from docutils.parsers.rst import Parser |
| 79 |
settings = frontend.get_default_settings(Parser) |
| 80 |
with open('test.rst', encoding='utf-8') as file: |
| 81 |
document = utils.new_document(file.name, settings) |
| 82 |
Parser().parse(file.read(), document) |
| 83 |
|
| 84 |
Let us now examine the node tree. |
| 85 |
With ``print(document.pformat())`` we get:: |
| 86 |
|
| 87 |
<document source="test.rst"> |
| 88 |
<paragraph> |
| 89 |
My |
| 90 |
<emphasis> |
| 91 |
favorite |
| 92 |
language is |
| 93 |
<reference name="Python" refname="python"> |
| 94 |
Python |
| 95 |
. |
| 96 |
<target ids="python" names="python" refuri="https://www.python.org/"> |
| 97 |
|
| 98 |
The top-level node is ``document``. It has a ``source`` attribute |
| 99 |
whose value is ``text.rst``. There are two children: A ``paragraph`` |
| 100 |
node and a ``target`` node. The ``paragraph`` in turn has children: A |
| 101 |
text node ("My "), an ``emphasis`` node, a text node (" language is "), |
| 102 |
a ``reference`` node, and again a ``Text`` node ("."). |
| 103 |
|
| 104 |
These node types (``document``, ``paragraph``, ``emphasis``, etc.) are |
| 105 |
all defined in ``docutils/nodes.py``. The node types are internally |
| 106 |
arranged as a class hierarchy (for example, both ``emphasis`` and |
| 107 |
``reference`` have the common superclass ``Inline``). To get an |
| 108 |
overview of the node class hierarchy, use epydoc (type ``epydoc |
| 109 |
nodes.py``) and look at the class hierarchy tree. |
| 110 |
|
| 111 |
.. tip:: While rst2pseudoxml_ shows the node tree after `transforming the |
| 112 |
document`_, the script quicktest.py_ (in the ``tools/dev/`` directory |
| 113 |
of the Docutils distribution) shows the direct result of parsing a |
| 114 |
reStructuredText sample. |
| 115 |
|
| 116 |
.. _quicktest.py: https://docutils.sourceforge.io/tools/dev/quicktest.py |
| 117 |
|
| 118 |
|
| 119 |
Transforming the Document |
| 120 |
------------------------- |
| 121 |
|
| 122 |
In the node tree above, the ``reference`` node does not contain the |
| 123 |
target URI (``https://www.python.org/``) yet. |
| 124 |
|
| 125 |
Assigning the target URI (from the ``target`` node) to the |
| 126 |
``reference`` node is *not* done by the parser (the parser only |
| 127 |
translates the input document into a node tree). |
| 128 |
|
| 129 |
Instead, it's done by a **Transform**. In this case (resolving a |
| 130 |
reference), it's done by the ``ExternalTargets`` transform in |
| 131 |
``docutils/transforms/references.py``. |
| 132 |
|
| 133 |
The Transforms are applied after parsing. To see how the node tree |
| 134 |
has changed after applying the Transforms, we use the |
| 135 |
rst2pseudoxml_ tool [#]_:: |
| 136 |
|
| 137 |
$ rst2pseudoxml test.rst |
| 138 |
<document source="test.rst"> |
| 139 |
<paragraph> |
| 140 |
My |
| 141 |
<emphasis> |
| 142 |
favorite |
| 143 |
language is |
| 144 |
<reference name="Python" **refuri="https://www.python.org/"**> |
| 145 |
Python |
| 146 |
. |
| 147 |
<target ids="python" names="python" ``refuri="https://www.python.org/"``> |
| 148 |
|
| 149 |
For our small test document, the only change is that the ``refname`` |
| 150 |
attribute of the reference has been replaced by a ``refuri`` |
| 151 |
attribute |---| the reference has been resolved. |
| 152 |
|
| 153 |
While this does not look very exciting, transforms are a powerful tool |
| 154 |
to apply any kind of transformation on the node tree. |
| 155 |
In fact, there are quite a lot of Transforms, which do various useful |
| 156 |
things like creating the table of contents, applying substitution |
| 157 |
references or resolving auto-numbered footnotes. For details, see |
| 158 |
`Docutils Transforms`_. |
| 159 |
|
| 160 |
.. [#] You can also get a standard XML representation of the |
| 161 |
node tree by using rst2xml_ instead of rst2pseudoxml_. |
| 162 |
|
| 163 |
.. _Docutils Transforms: ../api/transforms.html |
| 164 |
.. _rst2pseudoxml: ../user/tools.html#rst2pseudoxml |
| 165 |
.. _rst2xml: ../user/tools.html#rst2xml |
| 166 |
|
| 167 |
|
| 168 |
Writing the Document |
| 169 |
-------------------- |
| 170 |
|
| 171 |
To get an HTML document out of the node tree, we use a **Writer**, the |
| 172 |
HTML writer in this case (``docutils/writers/html4css1.py``). |
| 173 |
|
| 174 |
The writer receives the node tree and returns the output document. |
| 175 |
For HTML output, we can test this using the ``rst2html`` tool:: |
| 176 |
|
| 177 |
$ rst2html --link-stylesheet test.rst |
| 178 |
<?xml version="1.0" encoding="utf-8" ?> |
| 179 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 180 |
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 181 |
<head> |
| 182 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 183 |
<meta name="generator" content="Docutils 0.3.10: https://docutils.sourceforge.io/" /> |
| 184 |
<title></title> |
| 185 |
<link rel="stylesheet" href="../docutils/writers/html4css1/html4css1.css" type="text/css" /> |
| 186 |
</head> |
| 187 |
<body> |
| 188 |
<div class="document"> |
| 189 |
<p>My <em>favorite</em> language is <a class="reference" href="https://www.python.org/">Python</a>.</p> |
| 190 |
</div> |
| 191 |
</body> |
| 192 |
</html> |
| 193 |
|
| 194 |
So here we finally have our HTML output. The actual document contents |
| 195 |
are in the fourth-last line. Note, by the way, that the HTML writer |
| 196 |
did not render the (invisible) ``target`` node |---| only the |
| 197 |
``paragraph`` node and its children appear in the HTML output. |
| 198 |
|
| 199 |
|
| 200 |
Extending Docutils |
| 201 |
================== |
| 202 |
|
| 203 |
Now you'll ask, "how do I actually extend Docutils?" |
| 204 |
|
| 205 |
First of all, once you are clear about *what* you want to achieve, you |
| 206 |
have to decide *where* to implement it |---| in the Parser (e.g. by |
| 207 |
adding a directive or role to the reStructuredText parser), as a |
| 208 |
Transform, or in the Writer. There is often one obvious choice among |
| 209 |
those three (Parser, Transform, Writer). If you are unsure, ask on |
| 210 |
the Docutils-develop_ mailing list. |
| 211 |
|
| 212 |
In order to find out how to start, it is often helpful to look at |
| 213 |
similar features which are already implemented. For example, if you |
| 214 |
want to add a new directive to the reStructuredText parser, look at |
| 215 |
the implementation of a similar directive in |
| 216 |
``docutils/parsers/rst/directives/``. |
| 217 |
|
| 218 |
|
| 219 |
Modifying the Document Tree Before It Is Written |
| 220 |
------------------------------------------------ |
| 221 |
|
| 222 |
You can modify the document tree right before the writer is called. |
| 223 |
One possibility is to use the publish_doctree_ and |
| 224 |
publish_from_doctree_ functions. |
| 225 |
|
| 226 |
To retrieve the document tree, call:: |
| 227 |
|
| 228 |
document = docutils.core.publish_doctree(...) |
| 229 |
|
| 230 |
Please see the docstring of publish_doctree for a list of parameters. |
| 231 |
|
| 232 |
.. XXX Need to write a well-readable list of (commonly used) options |
| 233 |
of the publish_* functions. Probably in api/publisher.rst. |
| 234 |
|
| 235 |
``document`` is the root node of the document tree. You can now |
| 236 |
change the document by accessing the ``document`` node and its |
| 237 |
children |---| see `The Node Interface`_ below. |
| 238 |
|
| 239 |
When you're done with modifying the document tree, you can write it |
| 240 |
out by calling:: |
| 241 |
|
| 242 |
output = docutils.core.publish_from_doctree(document, ...) |
| 243 |
|
| 244 |
.. _publish_doctree: ../api/publisher.html#publish_doctree |
| 245 |
.. _publish_from_doctree: ../api/publisher.html#publish_from_doctree |
| 246 |
|
| 247 |
|
| 248 |
The Node Interface |
| 249 |
------------------ |
| 250 |
|
| 251 |
As described in the overview above, Docutils' internal representation |
| 252 |
of a document is a tree of nodes. We'll now have a look at the |
| 253 |
interface of these nodes. |
| 254 |
|
| 255 |
(To be completed.) |
| 256 |
|
| 257 |
|
| 258 |
What Now? |
| 259 |
========= |
| 260 |
|
| 261 |
This document is not complete. Many topics could (and should) be |
| 262 |
covered here. To find out with which topics we should write about |
| 263 |
first, we are awaiting *your* feedback. So please ask your questions |
| 264 |
on the Docutils-develop_ mailing list. |
| 265 |
|
| 266 |
.. _Docutils-develop: ../user/mailing-lists.html#docutils-develop |
| 267 |
|
| 268 |
.. |---| unicode:: 8212 .. em-dash |
| 269 |
:trim: |
| 270 |
|
| 271 |
.. Emacs settings |
| 272 |
|
| 273 |
Local Variables: |
| 274 |
mode: indented-text |
| 275 |
mode: rst |
| 276 |
indent-tabs-mode: nil |
| 277 |
sentence-end-double-space: t |
| 278 |
fill-column: 70 |
| 279 |
End: |