Debian Codegraph @

docs/api/transforms.rst

raw
1 .. include:: ../header.rst
2
3 =====================
4 Docutils Transforms
5 =====================
6
7 :Author: David Goodger
8 :Contact: [email protected]
9 :Revision: $Revision: 10307 $
10 :Date: $Date: 2026-03-28 19:54:14 +0100 (Sa, 28. Mär 2026) $
11 :Copyright: This document has been placed in the public domain.
12
13
14 .. contents::
15 :depth: 2
16
17 Introduction
18 ============
19
20 Transforms change the `document tree`_ in-place, add to the tree, or prune it.
21 Transforms resolve references and footnote numbers, process
22 `interpreted text`_, and do other context-sensitive processing.
23 Each transform is a subclass of `docutils.transforms.Transform`.
24
25 Most transforms are added by components, others (e.g. `parts.Contents`) are
26 added by the parser if a corresponding directive_ is found in the document.
27
28 To add a transform, components (objects inheriting from
29 `docutils.Component` like Readers, Parsers, Writers, Input, Output) overwrite
30 the `get_transforms()` method of their base class. After the Reader has
31 finished processing, the Publisher calls
32 `Transformer.populate_from_components()` with a list of components and all
33 transforms returned by the component's `get_transforms()` method are
34 stored in a `Transformer` object attached to the document tree.
35
36
37 For more info about transforms and the `Transformer` object,
38 see also `PEP 258`_. [#]_
39
40 .. [#] The `default_transforms()` attribute of component classes mentioned
41 in PEP 258 is deprecated. Use the `get_transforms()` method instead.
42
43 .. _priority:
44
45 Transforms Listed in Priority Order
46 ===================================
47
48 Transform classes each have a `default_priority` attribute which is used by
49 the Transformer to apply transforms in order (low to high). The default
50 priority can be overridden when adding transforms to the Transformer object.
51
52
53 ================================== ============================ ========
54 Transform: module.Class Added By Priority
55 ================================== ============================ ========
56 misc_.ClassAttribute `"class"`_ (d/p) _`210`
57
58 references_.Substitutions standalone_ (r), pep_ (r) _`220`
59
60 references_.SectionIDs standalone_ (r), pep_ (r) _`240`
61
62 references_.PropagateTargets standalone_ (r), pep_ (r) _`260`
63
64 frontmatter.\ DocTitle_ standalone_ (r) _`320`
65
66 frontmatter.\ DocInfo_ standalone_ (r) _`340`
67
68 frontmatter.\ SectSubTitle_ standalone_ (r) _`350`
69
70 peps_.Headers pep_ (r) _`360`
71
72 peps_.Contents pep_ (r) _`380`
73
74 universal_.StripClassesAndElements writers_ (w) _`420`
75
76 references_.AnonymousHyperlinks standalone_ (r), pep_ (r) _`440`
77
78 references_.IndirectHyperlinks standalone_ (r), pep_ (r) _`460`
79
80 peps_.TargetNotes pep_ (r) _`520`
81
82 references_.TargetNotes peps.TargetNotes (t/p) 0
83
84 misc_.CallBack peps.TargetNotes (t/p) 1
85
86 references_.TargetNotes `"target-notes"`_ (d/p) _`540`
87
88 references_.Footnotes standalone_ (r), pep_ (r) _`620`
89
90 references_.ExternalTargets standalone_ (r), pep_ (r) _`640`
91
92 references_.InternalTargets standalone_ (r), pep_ (r) _`660`
93
94 parts_.SectNum `"sectnum"`_ (d/p) _`710`
95
96 parts_.Contents `"contents"`_ (d/p), _`720`
97 peps.Contents (t/p)
98
99 universal_.StripComments readers_ (r) _`740`
100
101 peps_.PEPZero peps.Headers (t/p) _`760`
102
103 references_.CitationReferences latex2e_ (w) _`770`
104
105 components.Filter *not used* _`780`
106
107 universal_.Decorations readers_ (r) _`820`
108
109 misc_.Transitions standalone_ (r), pep_ (r) _`830`
110
111 universal_.Validate parsers_ (p) _`835`
112
113 universal_.ExposeInternals readers_ (r) _`840`
114
115 references_.DanglingReferences standalone_ (r), pep_ (r), _`850`
116 *deprecated* [#dangling]_
117
118 universal_.SmartQuotes rst_ (p) _`855`
119
120 universal_.Messages writers_ (w) _`860`
121
122 universal_.FilterMessages writers_ (w) _`870`
123
124 universal_.TestMessages DocutilsTestSupport _`880`
125
126 writer_aux_.Compound *not used, to be removed* _`910`
127
128 writer_aux_.Admonitions `_html_base`_ (w), _`920`
129 latex2e_ (w)
130
131 misc_.CallBack n/a _`990`
132 ================================== ============================ ========
133
134 Key:
135
136 * (r): Reader
137 * (w): Writer
138 * (d): Directive
139 * (t): Transform
140 * (/p): Via a `\<pending>`_ element
141
142 .. [#dangling] `references.DanglingReferences` will be replaced by
143 separate transforms for resolving refnames__ to refids__
144 and for reporting unresolved references to make space for
145 reference-resolving transforms added by extensions or applications
146 (cf. `Release Notes`__).
147
148 __ ../ref/doctree.html#refname
149 __ ../ref/doctree.html#refid
150 __ ../../RELEASE-NOTES.html#removals
151
152
153 Transform Priority Range Categories
154 -----------------------------------
155
156 ==== ==== ================================================
157 Priority
158 ---------- ------------------------------------------------
159 From To Category
160 ==== ==== ================================================
161 0 99 immediate execution (added by another transform)
162 100 199 very early (non-standard)
163 200 299 very early
164 300 399 early
165 400 699 main
166 700 799 late
167 800 899 very late
168 900 999 very late (non-standard)
169 ==== ==== ================================================
170
171
172 Transforms Added by Components
173 ==============================
174
175 .. _readers:
176
177 readers.Reader:
178 | universal.StripComments (740_)
179 | universal.Decorations (820_)
180 | universal.ExposeInternals (840_)
181
182 readers.ReReader:
183 None
184
185 .. _standalone:
186
187 readers.standalone.Reader:
188 | references.Substitutions (220_)
189 | references.SectionIDs (240_)
190 | references.PropagateTargets (260_)
191 | frontmatter.\ DocTitle_ (320_)
192 | frontmatter.\ DocInfo_ (340_)
193 | frontmatter.\ SectSubTitle_ (350_)
194 | references.AnonymousHyperlinks (440_)
195 | references.IndirectHyperlinks (460_)
196 | references.Footnotes (620_)
197 | references.ExternalTargets (640_)
198 | references.InternalTargets (660_)
199 | misc.Transitions (830_)
200 | references.DanglingReferences (850_)
201
202 .. _pep:
203
204 readers.pep.Reader:
205 | references.Substitutions (220_)
206 | references.SectionIDs (240_)
207 | references.PropagateTargets (260_)
208 | peps.Headers (360_)
209 | peps.Contents (380_)
210 | references.AnonymousHyperlinks (440_)
211 | references.IndirectHyperlinks (460_)
212 | peps.TargetNotes (520_)
213 | references.Footnotes (620_)
214 | references.ExternalTargets (640_)
215 | references.InternalTargets (660_)
216 | misc.Transitions (830_)
217 | references.DanglingReferences (850_)
218
219 .. _parsers:
220
221 parsers.Parser
222 universal_.Validate (835_)
223
224 .. _rst:
225
226 parsers.rst.Parser
227 | universal.SmartQuotes (855_)
228
229 .. _writers:
230
231 writers.Writer:
232 | universal.StripClassesAndElements (420_)
233 | universal.Messages (860_)
234 | universal.FilterMessages (870_)
235
236 writers.UnfilteredWriter
237 None
238
239 .. _latex2e:
240
241 writers.latex2e.Writer
242 | writer_aux.Admonitions (920_)
243 | references.CitationReferences (770_)
244
245 .. _`_html_base`:
246
247 writers._html_base.Writer:
248 writer_aux.Admonitions (920_)
249
250 writers.odf_odt.Writer:
251 removes references.DanglingReferences
252
253
254 Transforms Reference
255 ====================
256
257 .. Note:: This section is still incomplete.
258
259 See also `Transforms Listed in Priority Order`_
260 and the sources in `docutils/transforms`__.
261
262 __ https://docutils.sourceforge.io/docutils/transforms/
263
264
265 DocInfo
266 -------
267
268 .. class:: field-indent-12em
269
270 :Module: frontmatter_
271 :Added by: standalone_ Reader
272 :Default priority_: 340_
273 :Configuration_ setting: docinfo_xform_ (default: True)
274
275 Given a document starting [#pre-docinfo]_ with a field list, the DocInfo
276 transform converts fields with registered `bibliographic field`_ names to
277 the corresponding document tree elements becoming child elements of the
278 `\<docinfo>`_ element (except for "dedication" and "abstract", which
279 become `\<topic>`_ elements after <docinfo>).
280
281 .. [#pre-docinfo] A document title and subtitle, header and footer,
282 as well as body elements that do not show up in the output
283 before the field list don't prevent the DocInfo transformation.
284 See the `DocTitle examples`_ and `PreBibliographic Elements`_
285 in the Appendix for details.
286
287 For example, this document fragment::
288
289 <document>
290 <title>
291 Document Title
292 <field_list>
293 <field>
294 <field_name>
295 Author
296 <field_body>
297 <paragraph>
298 A. Name
299 <field>
300 <field_name>
301 Status
302 <field_body>
303 <paragraph>
304 $RCSfile$
305 ...
306
307 will be transformed to::
308
309 <document>
310 <title>
311 Document Title
312 <docinfo>
313 <author>
314 A. Name
315 <status>
316 frontmatter.py
317 ...
318
319
320 DocTitle
321 --------
322
323 .. class:: field-indent-12em
324
325 :Module: frontmatter_
326 :Added by: standalone_ Reader
327 :Default priority_: 320_
328 :Configuration_ setting: doctitle_xform_ (default: True)
329
330 Under the conditions explained below, the DocTitle transform converts
331 the document's first section title(s) to a document title and
332 subtitle.
333
334 1. If the document contains a single top-level section as its first body
335 element, [#pre-doctitle]_ the top-level section's title is used
336 as `document title`_ and default `metadata title`_. The top-level
337 section's contents become the document's immediate contents.
338
339 .. _step 2:
340
341 2. If step 1 successfully determines the document title, the transform
342 checks for a subtitle:
343 If the lone top-level section itself contains a single second-level
344 section as its first element, [#pre-doctitle]_ that section's
345 title is promoted to the document's subtitle, and that section's
346 contents become the document's immediate contents.
347
348 The transform can be disabled with the doctitle_xform_ configuration
349 setting or the corresponding ``--no-doc-title`` command line option.
350
351 .. [#pre-doctitle] Header and footer as well as body elements that
352 do not show up in the output before the section don't stop the
353 transformation of the section title to document title or subtitle.
354 See `PreBibliographic Elements`_ in the Appendix for details.
355
356 .. _DocTitle examples:
357
358 Examples
359 ~~~~~~~~
360
361 The input text ::
362
363 Top-Level Title
364 ===============
365 A paragraph.
366
367 is parsed to the following document tree::
368
369 <document>
370 <section names="top-level\ title">
371 <title>
372 Top-Level Title
373 <paragraph>
374 A paragraph.
375
376 The DocTitle transform converts it to::
377
378 <document names="top-level\ title" title="Top-Level Title">
379 <title>
380 Top-Level Title
381 <paragraph>
382 A paragraph.
383
384 Given this input with a lone section and sub-section::
385
386 Top-Level Title
387 ===============
388 Second-Level Title
389 ~~~~~~~~~~~~~~~~~~
390
391 A paragraph.
392
393 the result after parsing and running the DocTitle transform is::
394
395 <document names="top-level\ title">
396 <title>
397 Top-Level Title
398 <subtitle names="second-level\ title">
399 Second-Level Title
400 <paragraph>
401 A paragraph.
402
403 (Note that the implicit hyperlink target generated by the
404 "Second-Level Title" is preserved on the "subtitle" element
405 itself.)
406
407 --------------
408
409 The following examples do *not* comply with the conditions:
410
411 i. More than one top-level section::
412
413 Top-Level Title
414 ===============
415 A paragraph.
416
417 Another Top-Level Title
418 =======================
419 Another paragraph.
420
421 The DocTitle transform will leave the document tree as-is.
422 The document has no title. It is recommended to set the
423 `metadata title`_ with the `"title"`_ directive.
424
425 #. More than one second-level section::
426
427 Top-Level Title
428 ===============
429 Second-Level Title
430 ~~~~~~~~~~~~~~~~~~
431 A paragraph.
432
433 Another Second-Level Title
434 ~~~~~~~~~~~~~~~~~~~~~~~~~~
435 Another paragraph.
436
437 Step 2 is skipped. "Top-Level Title" becomes the document title;
438 "Second-Level Title" and "Another Second-Level Title" become titles of
439 top-level sections.
440
441 This is what you normally want in a document with title but no
442 subtitle.
443
444 #. Body elements before the first section: [#pre-doctitle]_ ::
445
446 .. note:: This element would not stop the transform, if it were
447 nested in a header.
448
449 Top-Level Title
450 ===============
451 A paragraph.
452
453 The DocTitle transform will leave the document tree as-is.
454 The document has no title.
455
456
457 #. A `configuration file`_ entry "``doctitle_xform: False``"
458 or converting with the CLI command ::
459
460 docutils --no-doc-title example.rst > example.html
461
462 The DocTitle transform is skipped. The document has no title.
463
464
465 SectSubTitle
466 ------------
467
468 .. class:: field-indent-12em
469
470 :Module: frontmatter_
471 :Added by: standalone_ Reader
472 :Default priority_: 350_
473 :Configuration_ setting: sectsubtitle_xform_ (default: False)
474
475 The SectSubTitle transform works like `step 2`_ of the DocTitle_
476 transform, but for sections.
477
478 For example, ::
479
480 <section>
481 <title>
482 Title
483 <section>
484 <title>
485 Subtitle
486 ...
487
488 is transformed into ::
489
490 <section>
491 <title>
492 Title
493 <subtitle>
494 Subtitle
495 ...
496
497 This transform is disabled by default.
498
499
500 Appendix
501 ========
502
503 PreBibliographic Elements
504 -------------------------
505
506 The document tree elements `\<comment>`_, `\<decoration>`_,
507 `\<footer>`_, `\<header>`_, `\<meta>`_, `\<pending>`_, `\<raw>`_,
508 `\<substitution_definition>`_, `\<subtitle>`_, `\<system_message>`_,
509 `\<target>`_, and `\<title>`_ are ignored when the DocTitle_ and DocInfo_
510 transforms check for elements before frontmatter candidates.
511
512 This means that in the reStructuredText source, comments_,
513 `hyperlink targets`_, `substitution definitions`_, and the directives_
514 `"class"`_, `"default-role"`_, `"footer"`_, `"header"`_, `"meta"`_,
515 `"raw"`_, `"sectnum"`_, `"sectnum"`_, `"target-notes"`_, and `"title"`_
516 may be placed before the `document title`_ or `bibliographic fields`_.
517
518 The DocTitle_ transform inserts document title and subtitle before these
519 elements. DocInfo_ inserts the bibliographic fields before
520 `\<comment>`_, `\<raw>`_, `\<substitution_definition>`_, and `\<target>`_.
521
522 For example, the document source::
523
524 .. a comment
525 .. |today| date::
526 .. _hypertarget: http://example.org
527 .. class:: spam
528 .. role:: ham
529 .. default-role:: ham
530 .. footer:: footer text
531 .. header:: header text
532 .. meta:: :test: value
533 .. raw:: html
534
535 raw html text
536 .. sectnum::
537 .. target-notes::
538 .. title:: metadata document title
539
540 Top-Level Title
541 ===============
542 Second-Level Title
543 ~~~~~~~~~~~~~~~~~~
544
545 :date: |today|
546
547 is parsed and transformed to::
548
549 <document ids="top-level-title" names="top-level\ title"
550 title="metadata document title">
551 <title>
552 Top-Level Title
553 <subtitle ids="second-level-title" names="second-level\ title">
554 Second-Level Title
555 <meta content="value" name="test">
556 <decoration>
557 <header>
558 <paragraph>
559 header text
560 <footer>
561 <paragraph>
562 footer text
563 <docinfo>
564 <date>
565 2024-03-01
566 <comment xml:space="preserve">
567 a comment
568 <substitution_definition names="today">
569 2024-03-01
570 <target ids="hypertarget" names="hypertarget" refuri="http://example.org">
571 <raw classes="spam" format="html" xml:space="preserve">
572 raw html text
573
574
575
576 .. References
577 ==========
578
579 .. _PEP 258: ../peps/pep-0258.html#transformer
580
581 .. _configuration:
582 .. _configuration file:
583 .. _configuration setting: ../user/config.html
584 .. _docinfo_xform: ../user/config.html#docinfo-xform
585 .. _doctitle_xform: ../user/config.html#doctitle-xform
586 .. _sectsubtitle_xform: ../user/config.html#sectsubtitle-xform
587
588 .. _"class": ../ref/rst/directives.html#class
589 .. _"contents": ../ref/rst/directives.html#table-of-contents
590 .. _"default-role": ../ref/rst/directives.html#default-role
591 .. _"footer": ../ref/rst/directives.html#footer
592 .. _"header": ../ref/rst/directives.html#header
593 .. _"meta": ../ref/rst/directives.html#meta
594 .. _"raw": ../ref/rst/directives.html#raw
595 .. _"sectnum": ../ref/rst/directives.html#sectnum
596 .. _"target-notes": ../ref/rst/directives.html#target-notes
597 .. _"title": ../ref/rst/directives.html#title
598
599 .. _document tree: ../ref/doctree.html
600 .. _`<comment>`: ../ref/doctree.html#comment
601 .. _`<docinfo>`: ../ref/doctree.html#docinfo
602 .. _`<decoration>`: ../ref/doctree.html#decoration
603 .. _`<footer>`: ../ref/doctree.html#footer
604 .. _`<header>`: ../ref/doctree.html#header
605 .. _`<meta>`: ../ref/doctree.html#meta
606 .. _`<pending>`: ../ref/doctree.html#pending
607 .. _`<raw>`: ../ref/doctree.html#raw
608 .. _`<substitution_definition>`: ../ref/doctree.html#substitution-definition
609 .. _`<subtitle>`: ../ref/doctree.html#subtitle
610 .. _`<system_message>`: ../ref/doctree.html#system-message
611 .. _`<target>`: ../ref/doctree.html#target
612 .. _`<title>`: ../ref/doctree.html#title
613 .. _`<topic>`: ../ref/doctree.html#topic
614 .. _metadata title: ../ref/doctree.html#title-attribute
615
616 .. reStructuredText Markup Specification
617 .. _bibliographic field:
618 .. _bibliographic fields: ../ref/rst/restructuredtext.html#bibliographic-fields
619 .. _comments: ../ref/rst/restructuredtext.html#comments
620 .. _directive:
621 .. _directives: ../ref/rst/restructuredtext.html#directives
622 .. _document title: ../ref/rst/restructuredtext.html#document-title
623 .. _hyperlink targets: ../ref/rst/restructuredtext.html#hyperlink-targets
624 .. _interpreted text: ../ref/rst/restructuredtext.html#interpreted-text
625 .. _registered bibliographic field names:
626 ../ref/rst/restructuredtext.html#bibliographic-field-names
627 .. _substitution definitions:
628 ../ref/rst/restructuredtext.html#substitution-definitions
629
630 .. transform class sources
631 .. _frontmatter: ../../docutils/transforms/frontmatter.py
632 .. _misc: ../../docutils/transforms/misc.py
633 .. _parts: ../../docutils/transforms/parts.py
634 .. _peps: ../../docutils/transforms/peps.py
635 .. _references: ../../docutils/transforms/references.py
636 .. _universal: ../../docutils/transforms/universal.py
637 .. _writer_aux: ../../docutils/transforms/writer_aux.py