Author: | David Goodger |
---|---|
Contact: | docutils-develop@lists.sourceforge.net |
Date: | 2024-12-18 |
Revision: | 10004 |
Copyright: | This document has been placed in the public domain. |
The docutils.core.Publisher class is the core of Docutils, managing all the processing and relationships between components. [1]
The Publisher convenience functions are the normal entry points for using Docutils as a library. Entry point functions with pre-set components are provided for use in "console_scripts" entry points. Configuration is done via convenience function arguments and runtime settings assembled from several sources.
[1] | See the Docutils Project Model in PEP 258 for an overview. |
There are several convenience functions in the docutils.core module. Each of these functions sets up a docutils.core.Publisher object, then calls its publish() method which handles everything else. For details, see the argument reference, the function docstrings and the source file core.py.
Function for custom command-line applications.
def publish_cmdline(reader=None, reader_name=None, [5] parser=None, parser_name=None, [5] writer=None, writer_name=None, [5] settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=False, argv=None, usage=default_usage, description=default_description) -> str | bytes
The function reads input from sys.stdin or a file specified on the command line, writes the output document to a file, and also returns it as str instance. [2]
See the entry point functions in core.py, the scripts in the tools/ directory, and Inside A Docutils Command-Line Front-End Tool for usage examples.
[2] | (1, 2, 3) Documents in binary formats (currently only ODT) are returned as a bytes instance. |
For programmatic use with file I/O.
def publish_file(source=None, source_path=None, destination=None, destination_path=None, reader=None, reader_name=None, [5] parser=None, parser_name=None, [5] writer=None, writer_name=None, [5] settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=False) -> str | bytes
Read input from a file-like object. Write the output document to a file-like object and also return it as str instance. [2]
For programmatic use with string I/O.
def publish_string(source, source_path=None, destination_path=None, reader=None, reader_name=None, [5] parser=None, parser_name=None, [5] writer=None, writer_name=None, [5] settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=False) -> bytes | str
Get the input from the source argument (a str or bytes instance). Return the output document as a bytes instance unless the output_encoding runtime setting has the special value "unicode". [3]
Return a Docutils XML version of the source as str instance:
publish_string(source, writer='xml', settings_overrides={'output_encoding': 'unicode'})
Caution!
The output_encoding and output_encoding_error_handler runtime settings may affect the content of the output document: Some document formats contain an encoding declaration, some formats use substitutions for non-encodable characters.
Use publish_parts() to get a str instance of the output document together with values of the "output_encoding" and "output_encoding_error_handler" settings.
The publish_str() function is provisional because in Python 3 the name and default return type no longer match.
[3] | similar to xml.etree.ElementTree.tostring() |
For programmatic use with string input. Parse input into a Docutils Document Tree data structure. Return a nodes.document instance.
publish_doctree(source, source_path=None, source_class=io.StringInput, reader=None, reader_name=None, [5] parser=None, parser_name=None, [5] settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=False) -> nodes.document
The Document Tree can be modified, pickled & unpickled, etc., and then reprocessed with publish_from_doctree().
For programmatic use with string output. Render from an existing Document Tree data structure (nodes.document instance). Return the output document as a bytes or str instance (cf. publish_string()).
publish_from_doctree(document, destination_path=None, writer=None, writer_name=None, [5] settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=False) -> bytes | str
The publish_from_doctree() function is provisional because in Python 3 the name and default return type of the string I/O interface no longer match.
For programmatic use. With string input (default) or file input (depending on the value of the source_class argument). Returns a dictionary of document parts.
def publish_parts(source, source_path=None, source_class=io.StringInput, destination_path=None, reader=None, reader_name=None, [5] parser=None, parser_name=None, [5] writer=None, writer_name=None, [5] settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=False) -> dict
Dictionary keys are the part names. Each Writer component may publish a different set of document parts:
post-process the output document with a custom function post_process() before encoding with user-customizable encoding and errors:
def publish_bytes_with_postprocessing(*args, **kwargs): parts = publish_parts(*args, **kwargs) out_str = post_process(parts['whole']) return out_str.encode(parts['encoding'], parts['errors'])
There are more usage examples in the docutils/examples.py module.
All parts returned by the HTML writers are of data type str.
Contains
</head> <body> <div class="document" ...>
and, if applicable
<div class="header"> ... </div>
Contains (as applicable):
<h1 class="title">...</h1> <h2 class="subtitle" id="...">...</h2>
Contains
</div>
(the end-tag for <div class="document">), the footer division if applicable:
<div class="footer"> ... </div>
and
</body> </html>
Bibliographic data, i.e. the <docinfo> element's content, [4] rendered as a table.
[4] | Abstract and dedication are included in the "body"/"fragment" part. |
The HTML <head> content, less the "stylesheet" and the <head> and </head> tags themselves.
The "Content-Type" meta tag's "charset" value is left unresolved, as %s:
<meta http-equiv="Content-Type" content="text/html; charset=%s" />
The interpolation should be done by client code. Alternatively, use part "head" which interpolates the "charset" value with "encoding".
The XML declaration and the doctype declaration. The XML declaration's "encoding" attribute's value is left unresolved, as "%s":
<?xml version="1.0" encoding="%s" ?>
The interpolation should be done by client code.
The default template joins "head_prefix", "head", "stylesheet", "body_prefix", "body_pre_docinfo", "docinfo", "body", and "body_suffix" to get part "whole".
The PEP/HTML writer provides the same parts as the HTML4 writer, plus the following:
The S5/HTML writer provides the same parts as the HTML4 writer.
The HTML5 writer provides the same parts as the HTML4 writer. However, it uses semantic HTML5 elements for the document, "header", and "footer" and a description list for the "docinfo" part.
All parts returned by the LaTeX writers ("latex" writer and "xelatex" writer) are of data type str.
The document's content. In other words, parts['body'] contains the entire document, except the document title, subtitle, and docinfo.
This part can be included into another LaTeX document body using the \input{} command.
Bibliographic data except "abstract" and "dedication" (i.e. the <docinfo> element's content), rendered as a table.
If the use_latex_docinfo setting is True, <author>, <organization>, <contact>, <address>, and <date> are moved to "titledata".
The combined title data in \title, \author, and \date macros.
If use_latex_docinfo is True, "titledata" includes content from the <docinfo> elements <author>, <organization>, <contact>, <address>, and <date>.
Auxiliary function used by publish_file(), publish_string(), publish_doctree(), and publish_parts(). Applications should not need to call this function directly.
The functions rst2html(), rst2html4(), rst2html5(), rst2latex(), rst2man(), rst2odt(), rst2pseudoxml(), rst2s5(), rst2xetex(), and rst2xml() are wrappers around publish_cmdline() used in "console_scripts" entry points for eponymous command-line applications.
The function publish_file() uses the file I/O interface; publish_parts() can be configured to use file input by setting the source_class argument to docutils.io.FileInput.
A file-like object holding the document source (must have read() and close() methods).
Default: None (open source_path or use sys.stdin).
Path to the source file, opened if source is None.
Default: None (use source).
A file-like object that will receive the output document (must have write() and close() methods).
Default: None (open destination_path or use sys.stdout).
Path to the destination file, opened if destination is None.
Default: None (use destination).
The functions publish_string(), publish_doctree(), publish_from_doctree(), and publish_parts() use the string I/O interface provided by the docutils.io.StringInput and docutils.io.StringOutput classes.
The document source. bytes are decoded with the encoding specified in the input_encoding setting.
Required.
Path to the file or name of the object that produced source. Used as "source" attribute in diagnostic output.
Default: None.
Change the semantics of the "source" and "source_path" arguments. The value docutils.io.FileInput lets "source" and "source_path" behave as described in file I/O.
Default: docutils.io.StringInput.
Path to the file or name of the object which will receive the output. Used for determining relative paths (stylesheets, source links, etc.)
Default: None.
Reader component name or instance. [5]
Default: "standalone".
Parser component name or instance. [5]
Default: "restructuredtext".
Writer component name or instance. [5]
Default: "pseudoxml".
[5] | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) Up to Docutils 0.21, component names were specified with the "reader_name", "parser_name", and "writer_name" arguments. These arguments are deprecated and will be removed in Docutils 2.0. |
See also Runtime Settings.
Runtime settings object. If settings is passed, it's assumed to be the end result of runtime settings processing and no further setting/config/option processing is done.
Default: None.
Application-specific settings definitions, independent of components. In other words, the application becomes a component, and its settings data is processed after the other components. Used only if settings is None.
Default: None.
Application-specific settings defaults that override the defaults of other components. Used only if settings is None.
Default: None.
Name of the configuration file section for this application.
Can be specified instead of settings_spec (a new docutils.SettingsSpec will be created) or in addition to settings_spec (overriding its config_section attribute). Used only if settings is None.
Default: None.
Set "system exit status" at end of processing?
Default: False.