Debian Codegraph @

docs/api/publisher.rst

raw
1 .. include:: ../header.rst
2
3 ========================
4 The Docutils Publisher
5 ========================
6
7 :Author: David Goodger
8 :Contact: [email protected]
9 :Date: $Date: 2025-12-01 23:43:32 +0100 (Mo, 01. Dez 2025) $
10 :Revision: $Revision: 10267 $
11 :Copyright: This document has been placed in the public domain.
12
13 .. contents::
14 :depth: 2
15
16 .. _Publisher:
17
18 The ``docutils.core.Publisher`` class is the core of Docutils,
19 managing all the processing and relationships between components. [#]_
20
21 The `Publisher convenience functions`_ are the normal entry points
22 for using Docutils as a library.
23 `Entry point functions`_ with pre-set components are provided
24 for use in `"console_scripts" entry points`_.
25 Configuration is done via `convenience function arguments`_ and
26 `runtime settings`_ assembled from several sources.
27
28 .. [#] See the `Docutils Project Model`_ in PEP 258 for an overview.
29 .. _Docutils Project Model: ../peps/pep-0258.html#docutils-project-model
30
31
32 Publisher Convenience Functions
33 ===============================
34
35 There are several convenience functions in the ``docutils.core`` module.
36 Each of these functions sets up a `docutils.core.Publisher` object,
37 then calls its `publish()` method which handles everything else.
38 For details, see the `argument reference`_, the function docstrings
39 and the source file `core.py`_.
40
41 .. TODO: generate API documentation with Sphinx and add links to it.
42
43
44 publish_cmdline()
45 -----------------
46
47 Function for custom `command-line applications`_.
48
49 .. parsed-literal::
50
51 def publish_cmdline(reader_\ =None, reader_name=None, [#component-names]_
52 parser_\ =None, parser_name=None, [#component-names]_
53 writer_\ =None, writer_name=None, [#component-names]_
54 settings_\ =None, settings_spec_\ =None,
55 settings_overrides_\ =None, config_section_\ =None,
56 enable_exit_status_\ =False,
57 argv=None, usage=default_usage,
58 description=default_description) -> str | bytes
59
60 The function reads input from `sys.stdin` or a file specified on the
61 command line, writes the output document to a file, and also returns it
62 as `str` instance. [#binary-output]_
63
64 See the `entry point functions`_ in `core.py`_, the scripts in the
65 `tools/`_ directory, and `Inside A Docutils Command-Line Front-End Tool`_
66 for usage examples.
67
68 .. [#binary-output] Documents in binary formats (currently only ODT_)
69 are returned as a `bytes` instance.
70
71
72 publish_file()
73 --------------
74
75 For programmatic use with `file I/O`_.
76
77 .. parsed-literal::
78
79 def publish_file(source__\ =None, source_path__\ =None,
80 destination_\ =None, destination_path__\ =None,
81 reader_\ =None, reader_name=None, [#component-names]_
82 parser_\ =None, parser_name=None, [#component-names]_
83 writer_\ =None, writer_name=None, [#component-names]_
84 settings_\ =None, settings_spec_\ =None,
85 settings_overrides_\ =None, config_section_\ =None,
86 enable_exit_status_\ =False) -> str | bytes
87
88 Read input from a file-like object.
89 Write the output document to a file-like object and also return it as
90 `str` instance. [#binary-output]_
91
92 __ `source (file I/O)`_
93 __ `source_path (file I/O)`_
94 __ `destination_path (file I/O)`_
95
96
97 publish_string()
98 ----------------
99
100 For programmatic use with `string I/O`_.
101
102 .. parsed-literal::
103
104 def publish_string(source__, source_path__\ =None,
105 destination_path__\ =None,
106 reader_\ =None, reader_name=None, [#component-names]_
107 parser_\ =None, parser_name=None, [#component-names]_
108 writer_\ =None, writer_name=None, [#component-names]_
109 settings_\ =None, settings_spec_\ =None,
110 settings_overrides_\ =None, config_section_\ =None,
111 enable_exit_status_\ =False) -> bytes | str
112
113 __ `source (string I/O)`_
114 __ `source_path (string I/O)`_
115 __ `destination_path (string I/O)`_
116
117 Get the input from the `source <source (string I/O)_>`__ argument
118 (a `str` or `bytes` instance).
119 Return the output document as a **bytes** instance unless the
120 output_encoding_ runtime setting has the special value ``"unicode"``. [#]_
121
122 Example:
123 Return a Docutils XML version of the source as `str` instance::
124
125 publish_string(source, writer='xml',
126 settings_overrides={'output_encoding': 'unicode'})
127
128
129 .. Caution::
130 The output_encoding_ and output_encoding_error_handler_ runtime
131 settings may affect the content of the output document:
132 Some document formats contain an *encoding declaration*,
133 some formats use substitutions for non-encodable characters.
134
135 Use `publish_parts()`_ to get a `str` instance of the output document
136 together with values of the "output_encoding" and
137 "output_encoding_error_handler" settings.
138
139 The `publish_str()` function is *provisional* because in Python 3 the
140 name and default return type no longer match.
141
142 .. [#] similar to `xml.etree.ElementTree.tostring()`__
143 __ https://docs.python.org/3/library/xml.etree.elementtree.html
144 #xml.etree.ElementTree.tostring
145
146
147 publish_doctree()
148 -----------------
149
150 For programmatic use with `string input`_.
151 Parse input into a `Docutils Document Tree`_ data structure.
152 Return a `nodes.document`_ instance.
153
154 .. parsed-literal::
155
156 publish_doctree(source__, source_path__\ =None,
157 source_class_\ =io.StringInput,
158 reader_\ =None, reader_name=None, [#component-names]_
159 parser_\ =None, parser_name=None, [#component-names]_
160 settings_\ =None, settings_spec_\ =None,
161 settings_overrides_\ =None, config_section_\ =None,
162 enable_exit_status_\ =False) -> nodes.document_
163
164 __ `source (string I/O)`_
165 __ `source_path (string I/O)`_
166
167 The Document Tree can be modified, pickled & unpickled,
168 etc., and then reprocessed with `publish_from_doctree()`_.
169
170
171 publish_from_doctree()
172 ----------------------
173
174 For programmatic use with `string output`_.
175 Render from an existing Document Tree data structure
176 (nodes.document_ instance).
177 Return the output document as a `bytes` or `str` instance
178 (cf. `publish_string()`_).
179
180 .. parsed-literal::
181
182 publish_from_doctree(document, destination_path__\ =None,
183 writer_\ =None, writer_name=None, [#component-names]_
184 settings_\ =None, settings_spec_\ =None,
185 settings_overrides_\ =None, config_section_\ =None,
186 enable_exit_status_\ =False) -> bytes | str
187
188 __ `destination_path (string I/O)`_
189
190 The `publish_from_doctree()` function is *provisional* because in
191 Python 3 the name and default return type of the `string I/O`_ interface
192 no longer match.
193
194
195 .. _publish-parts-details:
196
197 publish_parts()
198 ---------------
199
200 For programmatic use. With `string input`_ (default) or `file input`_
201 (depending on the value of the source_class_ argument).
202 Returns a dictionary of document parts.
203
204 .. parsed-literal::
205
206 def publish_parts(source__, source_path__\ =None,
207 source_class_\ =io.StringInput,
208 destination_path__\ =None,
209 reader_\ =None, reader_name=None, [#component-names]_
210 parser_\ =None, parser_name=None, [#component-names]_
211 writer_\ =None, writer_name=None, [#component-names]_
212 settings_\ =None, settings_spec_\ =None,
213 settings_overrides_\ =None, config_section_\ =None,
214 enable_exit_status_\ =False) -> dict
215
216 __ `source (string I/O)`_
217 __ `source_path (string I/O)`_
218 __ `destination_path (string I/O)`_
219
220
221 Dictionary keys are the part names.
222 Each Writer component may publish a different set of document parts:
223
224 .. contents::
225 :local:
226
227 Example:
228 post-process the output document with a custom function
229 ``post_process()`` before encoding with user-customizable
230 encoding and errors::
231
232 def publish_bytes_with_postprocessing(*args, **kwargs):
233 parts = publish_parts(*args, **kwargs)
234 out_str = post_process(parts['whole'])
235 return out_str.encode(parts['encoding'], parts['errors'])
236
237 There are more usage examples in the `docutils/examples.py`_ module.
238
239 .. _docutils/examples.py: ../../docutils/examples.py
240 .. _ODT: ../user/odt.html
241
242
243 Parts Provided by All Writers
244 `````````````````````````````
245
246 _`"encoding"` : str
247 The `output_encoding`_ setting.
248
249 _`"errors"` : str
250 The `output_encoding_error_handler`_ setting.
251
252 _`"version"` : str
253 The version of Docutils used.
254
255 _`"whole"` : str | bytes
256 The entire formatted document. [#binary-output]_
257
258
259 Parts Provided by the HTML Writers
260 ``````````````````````````````````
261
262 All parts returned by the HTML writers are of data type `str`.
263
264 HTML4 Writer
265 ^^^^^^^^^^^^
266
267 _`"body"`
268 Equivalent to `"fragment"`_. It is *not* equivalent to `"html_body"`_.
269
270 _`"body_prefix"`
271 Contains ::
272
273 </head>
274 <body>
275 <div class="document" ...>
276
277 and, if applicable ::
278
279 <div class="header">
280 ...
281 </div>
282
283 _`"body_pre_docinfo"`
284 Contains (as applicable)::
285
286 <h1 class="title">...</h1>
287 <h2 class="subtitle" id="...">...</h2>
288
289 _`"body_suffix"`
290 Contains ::
291
292 </div>
293
294 (the end-tag for ``<div class="document">``), the footer division
295 if applicable::
296
297 <div class="footer">
298 ...
299 </div>
300
301 and ::
302
303 </body>
304 </html>
305
306 _`"docinfo"`
307 Bibliographic data, i.e. the `\<docinfo>`_ element's content, [#]_
308 rendered as a table.
309
310 .. [#] Abstract and dedication are included in
311 the `"body"`_/`"fragment"`_ part.
312
313 _`"footer"`
314 The document footer content, meant to
315 appear at the bottom of a web page, or repeated at the bottom of
316 every printed page.
317
318 _`"fragment"`
319 The document body (*not* the HTML ``<body>``).
320 In other words, ``part['fragment']`` contains the entire document,
321 less the document `"title"`_, `"subtitle"`_, `"docinfo"`_,
322 `"header"`_, and `"footer"`_. Equivalent to part `"body"`_.
323
324 _`"head"`
325 Contains ``<meta ... />`` tags and the document
326 ``<title>...</title>``. See also `"html_head"`_.
327
328 _`"head_prefix"`
329 The XML declaration, the DOCTYPE declaration,
330 the ``<html ...>`` start tag, and the ``<head>`` start tag.
331
332 _`"header"`
333 The document header content, meant to appear at the top
334 of a web page, or repeated at the top of every printed page.
335
336 _`"html_body"`
337 The HTML ``<body>`` content,
338 less the ``<body>`` and ``</body>`` tags themselves.
339
340 _`"html_head"`
341 The HTML ``<head>`` content, less the `"stylesheet"`_
342 and the ``<head>`` and ``</head>`` tags themselves.
343
344 The "Content-Type" meta tag's "charset" value is left unresolved,
345 as ``%s``::
346
347 <meta http-equiv="Content-Type" content="text/html; charset=%s" />
348
349 The interpolation should be done by client code.
350 Alternatively, use part `"head"`_ which interpolates
351 the "charset" value with `"encoding"`_.
352
353 _`"html_prolog"`
354 The XML declaration and the doctype declaration.
355 The XML declaration's "encoding" attribute's value is left unresolved,
356 as "%s"::
357
358 <?xml version="1.0" encoding="%s" ?>
359
360 The interpolation should be done by client code.
361
362 _`"html_subtitle"`
363 The document subtitle, including the
364 enclosing ``<h2 class="subtitle">`` and ``</h2>`` tags.
365
366 _`"html_title"`
367 The document title, including the
368 enclosing ``<h1 class="title">`` and ``</h1>`` tags.
369
370 _`"meta"`
371 Contains all ``<meta ... />`` tags.
372
373 _`"stylesheet"`
374 The embedded stylesheet or stylesheet link.
375
376 _`"subtitle"`
377 The document subtitle text and any inline markup.
378 It does not include the enclosing ``<h2>`` and ``</h2>`` tags.
379
380 _`"title"`
381 The document title text and any inline markup.
382 It does not include the enclosing ``<h1>`` and ``</h1>`` tags.
383
384 The default template_ joins `"head_prefix"`_, `"head"`_, `"stylesheet"`_,
385 `"body_prefix"`_, `"body_pre_docinfo"`_, `"docinfo"`_, `"body"`_, and
386 `"body_suffix"`_ to get part `"whole"`_.
387
388
389 PEP/HTML Writer
390 ^^^^^^^^^^^^^^^
391
392 The PEP/HTML writer provides the same parts as the `HTML4 writer`_,
393 plus the following:
394
395 _`"pepnum"`
396 The PEP number (extracted from the `header preamble`__).
397
398 __ https://peps.python.org/pep-0001/#pep-header-preamble
399
400
401 S5/HTML Writer
402 ^^^^^^^^^^^^^^
403
404 The S5/HTML writer provides the same parts as the `HTML4 writer`_.
405
406
407 HTML5 Writer
408 ^^^^^^^^^^^^
409
410 The HTML5 writer provides the same parts as the `HTML4 writer`_.
411 However, it uses semantic HTML5 elements for the document, `"header"`_, and
412 `"footer"`_ and a description list for the `"docinfo"`_ part.
413
414
415 Parts Provided by the LaTeX Writers
416 ```````````````````````````````````
417
418 All parts returned by the LaTeX writers ("latex" writer and "xelatex"
419 writer) are of data type `str`.
420
421 _`"abstract"`
422 Formatted content of the "abstract" `bibliographic field`_.
423
424 "body"
425 The document's content. In other words, ``parts['body']`` contains
426 the entire document, except the document title, subtitle, and
427 docinfo.
428
429 This part can be included into another LaTeX document body using the
430 ``\input{}`` command.
431
432 "body_pre_docinfo"
433 The ``\maketitle`` command.
434
435 _`"dedication"`
436 Formatted content of the "dedication" `bibliographic field`_.
437
438 "docinfo"
439 `Bibliographic data`_ except `"abstract"`_ and `"dedication"`_
440 (i.e. the `\<docinfo>`_ element's content), rendered as a table.
441
442 If the use_latex_docinfo_ setting is True, <author>, <organization>,
443 <contact>, <address>, and <date> are moved to `"titledata"`_.
444
445 "fallbacks"
446 Fallback definitions for Docutils-specific LaTeX commands and environments.
447
448 "head_prefix"
449 The declaration of documentclass and document options.
450
451 "latex_preamble"
452 The argument of the latex_preamble_ setting.
453
454 "pdfsetup"
455 PDF properties ("hyperref" package setup).
456
457 "requirements"
458 Required packages and setup before the stylesheet inclusion.
459
460 "stylesheet"
461 The embedded stylesheet(s) or stylesheet loading command(s).
462
463 "subtitle"
464 Document subtitle text and any inline markup.
465
466 "template"
467 Content of the template__ file.
468 The output document (i.e. part `"whole"`_) is generated from the
469 template by substituting placeholders with the corresponding
470 parts using `string.Template.substitute()`_.
471 New in Docutils 0.22.
472
473 "title"
474 Document title text and any inline markup.
475
476 _`"titledata"`
477 The combined title data in ``\title``, ``\author``, and ``\date`` macros.
478
479 If use_latex_docinfo_ is True, "titledata" includes content from
480 the `\<docinfo>`_ elements <author>, <organization>, <contact>,
481 <address>, and <date>.
482
483 __ ../user/config.html#template-latex-writers
484 .. _string.Template.substitute():
485 https://docs.python.org/3/library/string.html#template-strings
486
487
488 publish_programmatically()
489 --------------------------
490
491 Auxiliary function used by `publish_file()`_, `publish_string()`_,
492 `publish_doctree()`_, and `publish_parts()`_.
493 Applications should not need to call this function directly.
494
495
496 Entry Point Functions
497 ---------------------
498
499 The functions rst2html(), rst2html4(), rst2html5(), rst2latex(),
500 rst2man(), rst2odt(), rst2pseudoxml(), rst2s5(), rst2xetex(),
501 and rst2xml() are wrappers around `publish_cmdline()`_ used in
502 `"console_scripts" entry points`_ for eponymous `command-line applications`_.
503
504 .. _"console_scripts" entry points:
505 https://packaging.python.org/en/latest/specifications/entry-points/
506
507
508 .. _convenience function arguments:
509
510 Argument Reference
511 ==================
512
513 .. _file input:
514
515 File I/O
516 --------
517
518 The function `publish_file()`_ uses the file I/O interface;
519 `publish_parts()`_ can be configured to use file input
520 by setting the `source_class`_ argument to `docutils.io.FileInput`.
521
522 .. _source (file I/O):
523
524 source : file-like
525 A file-like object holding the document source
526 (must have `read()` and `close()` methods).
527
528 Default: None (open `source_path`__ or use `sys.stdin`).
529
530 __
531 .. _source_path (file I/O):
532
533 source_path : str | pathlib.Path
534 Path to the source file,
535 opened if `source <source (file I/O)_>`__ is None.
536
537 Default: None (use `source <source (file I/O)_>`__ or `sys.stdin`).
538
539 _`destination` : file-like
540 A file-like object that will receive the output document
541 (must have `write()` and `close()` methods).
542
543 Default: None (open `destination_path <destination_path (file I/O)_>`__
544 or use `sys.stdout`).
545
546 .. _destination_path (file I/O):
547
548 destination_path : str | pathlib.Path
549 Path to the destination file, opened if destination_ is None.
550
551 Default: None (use destination_ or `sys.stdout`).
552
553
554 .. _string input:
555 .. _string output:
556
557 String I/O
558 ----------
559
560 The functions `publish_string()`_, `publish_doctree()`_,
561 `publish_from_doctree()`_, and `publish_parts()`_ use the
562 string I/O interface provided by the `docutils.io.StringInput`
563 and `docutils.io.StringOutput` classes.
564
565 .. _source (string I/O):
566
567 source : str | bytes
568 The document source. `bytes` are decoded with the encoding
569 specified in the input_encoding_ setting.
570
571 Required.
572
573 .. _source_path (string I/O):
574
575 source_path : str
576 Path to the file or name of the object that produced
577 `source <source (string I/O)_>`__.
578 Used as `"source" attribute`_ in diagnostic output.
579
580 Default: None.
581
582 _`source_class` : docutils.io.Input
583 Change the semantics of the "source" and "source_path" arguments.
584 The value `docutils.io.FileInput` lets "source" and "source_path"
585 behave as described in `file I/O`_.
586
587 Default: `docutils.io.StringInput`.
588
589 .. _destination_path (string I/O):
590
591 destination_path : str
592 Path to the file or name of the object which will receive the output.
593 Used for determining relative paths (stylesheets, source links, etc.)
594
595 Default: None.
596
597
598 Component Specification
599 -----------------------
600
601 _`reader` : str | docutils.readers.Reader
602 `Reader component name`_ or instance. [#component-names]_
603
604 Default: "standalone".
605
606 _`parser` : str | docutils.parsers.Parser
607 `Parser component name`_ or instance. [#component-names]_
608
609 Default: "restructuredtext".
610
611 _`writer` : str | docutils.writers.Writer
612 `Writer component name`_ or instance. [#component-names]_
613
614 Default: "pseudoxml".
615
616 .. [#component-names] Up to Docutils 0.21, component *names* were specified
617 with the "_`reader_name`", "_`parser_name`", and "_`writer_name`" arguments.
618 These arguments are deprecated and will be removed in Docutils 2.0.
619
620
621 Settings Specification
622 ----------------------
623
624 See also `Runtime Settings`_.
625
626 _`settings` : docutils.frontend.Values
627 Runtime settings object.
628 If `settings` is passed, it's assumed to be the end result of
629 `runtime settings processing`_ and no further setting/config/option
630 processing is done.
631
632 Default: None.
633
634 _`settings_spec` : docutils.SettingsSpec_
635 Application-specific settings definitions, independent of components.
636 In other words, the application becomes a component, and its settings
637 data is processed after the other components.
638 Used only if settings_ is None.
639
640 Default: None.
641
642 _`settings_overrides` : dict
643 Application-specific settings defaults that override the defaults
644 of other components. Used only if settings_ is None.
645
646 Default: None.
647
648 _`config_section` : str
649 Name of the configuration file section for this application.
650
651 Can be specified instead of settings_spec_ (a new
652 docutils.SettingsSpec_ will be created) or in addition to
653 settings_spec_ (overriding its `config_section` attribute).
654 Used only if settings_ is None.
655
656 Default: None.
657
658 _`enable_exit_status` : bool
659 Set "system exit status" at end of processing?
660
661 Default: False.
662
663
664 .. References
665 ----------
666
667 .. _runtime settings processing:
668 .. _Runtime Settings: runtime-settings.html
669 .. _docutils.SettingsSpec: runtime-settings.html#settingsspec
670
671 .. _Inside A Docutils Command-Line Front-End Tool: ../howto/cmdline-tool.html
672
673 .. _Document Tree:
674 .. _Docutils Document Tree: ../ref/doctree.html
675 .. _<docinfo>: ../ref/doctree.html#docinfo
676 .. _nodes.document: ../ref/doctree.html#document
677 .. _"source" attribute: ../ref/doctree.html#source
678
679 .. _bibliographic data:
680 .. _bibliographic field:
681 ../ref/rst/restructuredtext.html#bibliographic-fields
682
683 .. _Docutils Configuration: ../user/config.html
684 .. _configuration files: ../user/config.html#configuration-files
685 .. _input_encoding: ../user/config.html#input-encoding
686 .. _latex_preamble: ../user/config.html#latex-preamble
687 .. _output_encoding: ../user/config.html#output-encoding
688 .. _output_encoding_error_handler:
689 ../user/config.html#output-encoding-error-handler
690 .. _parser component name: ../user/config.html#parser
691 .. _reader component name: ../user/config.html#reader
692 .. _template: ../user/config.html#template
693 .. _use_latex_docinfo: ../user/config.html#use-latex-docinfo
694 .. _writer component name: ../user/config.html#writer-docutils-application
695
696 .. _command-line applications: ../user/tools.html
697
698 .. _core.py: ../../docutils/core.py
699 .. _tools/: ../../tools/