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