docutils.writers._html_base module

common definitions for Docutils HTML writers

class Writer[source]

Bases: Writer

supported = ('html', 'xhtml')

Formats this writer supports.

settings_spec = ('HTML Writer Options', None, (('Specify the template file (UTF-8 encoded). (default: writer dependent)', ['--template'], {'metavar': '<file>'}), ('Comma separated list of stylesheet URLs. Overrides previous --stylesheet and --stylesheet-path settings.', ['--stylesheet'], {'metavar': '<URL[,URL,...]>', 'overrides': 'stylesheet_path', 'validator': <function validate_comma_separated_list>}), ('Comma separated list of stylesheet paths. Relative paths are expanded if a matching file is found in the --stylesheet-dirs. With --link-stylesheet, the path is rewritten relative to the output HTML file. (default: writer dependent)', ['--stylesheet-path'], {'metavar': '<file[,file,...]>', 'overrides': 'stylesheet', 'validator': <function validate_comma_separated_list>}), ('Comma-separated list of directories where stylesheets are found. Used by --stylesheet-path when expanding relative path arguments. (default: writer dependent)', ['--stylesheet-dirs'], {'metavar': '<dir[,dir,...]>', 'validator': <function validate_comma_separated_list>}), ('Embed the stylesheet(s) in the output HTML file.  The stylesheet files must be accessible during processing. (default)', ['--embed-stylesheet'], {'action': 'store_true', 'default': 1, 'validator': <function validate_boolean>}), ('Link to the stylesheet(s) in the output HTML file. ', ['--link-stylesheet'], {'action': 'store_false', 'dest': 'embed_stylesheet'}), ('Specify the initial header level. Does not affect document title & subtitle (see --no-doc-title).(default: writer dependent).', ['--initial-header-level'], {'choices': ['1', '2', '3', '4', '5', '6'], 'default': '2', 'metavar': '<level>'}), ('Format for footnote references: one of "superscript" or "brackets". (default: "brackets")', ['--footnote-references'], {'choices': ['superscript', 'brackets'], 'default': 'brackets', 'metavar': '<format>', 'overrides': 'trim_footnote_reference_space'}), ('Format for block quote attributions: one of "dash" (em-dash prefix), "parentheses"/"parens", or "none". (default: "dash")', ['--attribution'], {'choices': ['dash', 'parentheses', 'parens', 'none'], 'default': 'dash', 'metavar': '<format>'}), ('Remove extra vertical whitespace between items of "simple" bullet lists and enumerated lists. (default)', ['--compact-lists'], {'action': 'store_true', 'default': True, 'validator': <function validate_boolean>}), ('Disable compact simple bullet and enumerated lists.', ['--no-compact-lists'], {'action': 'store_false', 'dest': 'compact_lists'}), ('Remove extra vertical whitespace between items of simple field lists. (default)', ['--compact-field-lists'], {'action': 'store_true', 'default': True, 'validator': <function validate_boolean>}), ('Disable compact simple field lists.', ['--no-compact-field-lists'], {'action': 'store_false', 'dest': 'compact_field_lists'}), ('Added to standard table classes. Defined styles: borderless, booktabs, align-left, align-center, align-right, colwidths-auto, colwidths-grid.', ['--table-style'], {'default': ''}), ('Math output format (one of "MathML", "HTML", "MathJax", or "LaTeX") and option(s). (default: "HTML math.css")', ['--math-output'], {'default': 'HTML math.css'}), ('Prepend an XML declaration. ', ['--xml-declaration'], {'action': 'store_true', 'default': False, 'validator': <function validate_boolean>}), ('Omit the XML declaration.', ['--no-xml-declaration'], {'action': 'store_false', 'dest': 'xml_declaration'}), ('Obfuscate email addresses to confuse harvesters while still keeping email links usable with standards-compliant browsers.', ['--cloak-email-addresses'], {'action': 'store_true', 'validator': <function validate_boolean>})))

Runtime settings specification. Override in subclasses.

Defines runtime settings and associated command-line options, as used by docutils.frontend.OptionParser. This is a tuple of:

  • Option group title (string or None which implies no group, just a list of single options).

  • Description (string or None).

  • A sequence of option tuples. Each consists of:

    • Help text (string)

    • List of option strings (e.g. ['-Q', '--quux']).

    • Dictionary of keyword arguments sent to the OptionParser/OptionGroup add_option method.

      Runtime setting names are derived implicitly from long option names (’–a-setting’ becomes settings.a_setting) or explicitly from the ‘dest’ keyword argument.

      Most settings will also have a ‘validator’ keyword & function. The validator function validates setting values (from configuration files and command-line option arguments) and converts them to appropriate types. For example, the docutils.frontend.validate_boolean function, required by all boolean settings, converts true values (‘1’, ‘on’, ‘yes’, and ‘true’) to 1 and false values (‘0’, ‘off’, ‘no’, ‘false’, and ‘’) to 0. Validators need only be set once per setting. See the docutils.frontend.validate_* functions.

      See the optparse docs for more details.

  • More triples of group title, description, options, as many times as needed. Thus, settings_spec tuples can be simply concatenated.

settings_defaults = {'output_encoding_error_handler': 'xmlcharrefreplace'}

A dictionary of defaults for settings not in settings_spec (internal settings, intended to be inaccessible by command-line and config file). Override in subclasses.

config_section = 'html base writer'

The name of the config file section specific to this component (lowercase, no brackets). Override in subclasses.

config_section_dependencies = ('writers', 'html writers')

A list of names of config file sections that are to be applied before config_section, in order (from general to specific). In other words, the settings in config_section are to be overlaid on top of the settings from these sections. The “general” section is assumed implicitly. Override in subclasses.

visitor_attributes = ('head_prefix', 'head', 'stylesheet', 'body_prefix', 'body_pre_docinfo', 'docinfo', 'body', 'body_suffix', 'title', 'subtitle', 'header', 'footer', 'meta', 'fragment', 'html_prolog', 'html_head', 'html_title', 'html_subtitle', 'html_body')
get_transforms()[source]

Transforms required by this class. Override in subclasses.

translate()[source]

Do final translation of self.document into self.output. Called from write. Override in subclasses.

Usually done with a docutils.nodes.NodeVisitor subclass, in combination with a call to docutils.nodes.Node.walk() or docutils.nodes.Node.walkabout(). The NodeVisitor subclass must support all standard elements (listed in docutils.nodes.node_class_names) and possibly non-standard elements used by the current Reader as well.

apply_template()[source]
interpolation_dict()[source]
assemble_parts()[source]

Assemble the self.parts dictionary. Extend in subclasses.

See <https://docutils.sourceforge.io/docs/api/publisher.html>.

class HTMLTranslator(document)[source]

Bases: NodeVisitor

Generic Docutils to HTML translator.

See the html4css1 and html5_polyglot writers for full featured HTML writers.

Important

The visit_* and depart_* methods use a heterogeneous stack, self.context. When subclassing, make sure to be consistent in its use!

Examples for robust coding:

  1. Override both visit_* and depart_* methods, don’t call the parent functions.

  2. Extend both and unconditionally call the parent functions:

    def visit_example(self, node):
        if foo:
            self.body.append('<div class="foo">')
        html4css1.HTMLTranslator.visit_example(self, node)
    
    def depart_example(self, node):
        html4css1.HTMLTranslator.depart_example(self, node)
        if foo:
            self.body.append('</div>')
    
  3. Extend both, calling the parent functions under the same conditions:

    def visit_example(self, node):
        if foo:
            self.body.append('<div class="foo">
    
‘)
else: # call the parent method

_html_base.HTMLTranslator.visit_example(self, node)

def depart_example(self, node):
if foo:

self.body.append(‘</div>

‘)
else: # call the parent method

_html_base.HTMLTranslator.depart_example(self, node)

  1. Extend one method (call the parent), but don’t otherwise use the self.context stack:

    def depart_example(self, node):
        _html_base.HTMLTranslator.depart_example(self, node)
        if foo:
            # implementation-specific code
            # that does not use `self.context`
            self.body.append('</div>
    

‘)

This way, changes in stack use will not bite you.

doctype = '<!DOCTYPE html>\n'
doctype_mathml = '<!DOCTYPE html>\n'
head_prefix_template = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="%(lang)s" lang="%(lang)s">\n<head>\n'
content_type = '<meta charset="%s" />\n'
generator = '<meta name="generator" content="Docutils 0.20.1: https://docutils.sourceforge.io/" />\n'
documenttag_args = {'CLASS': 'document', 'tagname': 'div'}
mathjax_script = '<script type="text/javascript" src="%s"></script>\n'
mathjax_url = 'file:/usr/share/javascript/mathjax/MathJax.js'

URL of the MathJax javascript library.

The MathJax library ought to be installed on the same server as the rest of the deployed site files and specified in the math-output setting appended to “mathjax”. See Docutils Configuration.

The fallback tries a local MathJax installation at /usr/share/javascript/mathjax/MathJax.js.

embedded_stylesheet = '<style type="text/css">\n\n%s\n</style>\n'
words_and_spaces = re.compile('[^ \\n]+| +|\\n')
in_word_wrap_point = re.compile('.+\\W\\W.+|[-?].+')
lang_attribute = 'lang'
special_characters = {34: '&quot;', 38: '&amp;', 60: '&lt;', 62: '&gt;', 64: '&#64;'}

Character references for characters with a special meaning in HTML.

context

Heterogeneous stack.

Used by visit_* and depart_* functions in conjunction with the tree traversal. Make sure that the pops correspond to the pushes.

astext()[source]
encode(text)[source]

Encode special characters in text & return.

cloak_mailto(uri)[source]

Try to hide a mailto: URL from harvesters.

cloak_email(addr)[source]

Try to hide the link text of a email link from harversters.

attval(text, whitespace=re.compile('[\n\r\t\x0b\x0c]'))[source]

Cleanse, HTML encode, and return attribute value text.

stylesheet_call(path, adjust_path=None)[source]

Return code to reference or embed stylesheet file path

starttag(node, tagname, suffix='\n', empty=False, **attributes)[source]

Construct and return a start tag given a node (id & class attributes are extracted), tag name, and optional attributes.

emptytag(node, tagname, suffix='\n', **attributes)[source]

Construct and return an XML-compatible empty tag.

set_class_on_child(node, class_, index=0)[source]

Set class class_ on the visible child no. index of node. Do nothing if node has fewer children than index.

visit_Text(node)[source]
depart_Text(node)[source]
visit_abbreviation(node)[source]
depart_abbreviation(node)[source]
visit_acronym(node)[source]
depart_acronym(node)[source]
visit_address(node)[source]
depart_address(node)[source]
visit_admonition(node)[source]
depart_admonition(node=None)[source]
attribution_formats = {'dash': ('—', ''), 'none': ('', ''), 'parens': ('(', ')'), 'parentheses': ('(', ')')}
visit_attribution(node)[source]
depart_attribution(node)[source]
visit_author(node)[source]
depart_author(node)[source]
visit_authors(node)[source]
depart_authors(node)[source]
visit_block_quote(node)[source]
depart_block_quote(node)[source]
check_simple_list(node)[source]

Check for a simple list that can be rendered compactly.

is_compactable(node)[source]
visit_bullet_list(node)[source]
depart_bullet_list(node)[source]
visit_caption(node)[source]
depart_caption(node)[source]
visit_citation(node)[source]
depart_citation(node)[source]
visit_citation_reference(node)[source]
depart_citation_reference(node)[source]
visit_classifier(node)[source]
depart_classifier(node)[source]
visit_colspec(node)[source]
depart_colspec(node)[source]
visit_comment(node, sub=<built-in method sub of re.Pattern object>)[source]

Escape double-dashes in comment text.

visit_compound(node)[source]
depart_compound(node)[source]
visit_container(node)[source]
depart_container(node)[source]
visit_contact(node)[source]
depart_contact(node)[source]
visit_date(node)[source]
depart_date(node)[source]
visit_decoration(node)[source]
depart_decoration(node)[source]
visit_definition(node)[source]
depart_definition(node)[source]
visit_definition_list(node)[source]
depart_definition_list(node)[source]
visit_definition_list_item(node)[source]
depart_definition_list_item(node)[source]
visit_description(node)[source]
depart_description(node)[source]
visit_docinfo(node)[source]
depart_docinfo(node)[source]
visit_docinfo_item(node, name, meta=True)[source]
depart_docinfo_item()[source]
visit_doctest_block(node)[source]
depart_doctest_block(node)[source]
visit_document(node)[source]
depart_document(node)[source]
visit_emphasis(node)[source]
depart_emphasis(node)[source]
visit_entry(node)[source]
depart_entry(node)[source]
visit_enumerated_list(node)[source]
depart_enumerated_list(node)[source]
visit_field_list(node)[source]
depart_field_list(node)[source]
visit_field(node)[source]
depart_field(node)[source]
visit_field_name(node)[source]
depart_field_name(node)[source]
visit_field_body(node)[source]
depart_field_body(node)[source]
visit_figure(node)[source]
depart_figure(node)[source]
visit_footnote(node)[source]
depart_footnote(node)[source]
visit_footnote_reference(node)[source]
depart_footnote_reference(node)[source]
visit_generated(node)[source]
depart_generated(node)[source]
visit_header(node)[source]
depart_header(node)[source]
visit_image(node)[source]
depart_image(node)[source]
visit_inline(node)[source]
depart_inline(node)[source]
visit_label(node)[source]
depart_label(node)[source]
visit_legend(node)[source]
depart_legend(node)[source]
visit_line(node)[source]
depart_line(node)[source]
visit_line_block(node)[source]
depart_line_block(node)[source]
visit_list_item(node)[source]
depart_list_item(node)[source]
visit_literal(node)[source]
depart_literal(node)[source]
visit_literal_block(node)[source]
depart_literal_block(node)[source]
math_tags = {'html': ('div', 'span', 'formula'), 'latex': ('pre', 'tt', 'math'), 'mathjax': ('div', 'span', 'math'), 'mathml': ('div', '', '')}
visit_math(node, math_env='')[source]
depart_math(node)[source]
visit_math_block(node)[source]
depart_math_block(node)[source]
visit_meta(node)[source]
depart_meta(node)[source]
visit_option(node)[source]
depart_option(node)[source]
visit_option_argument(node)[source]
depart_option_argument(node)[source]
visit_option_group(node)[source]
depart_option_group(node)[source]
visit_option_list(node)[source]
depart_option_list(node)[source]
visit_option_list_item(node)[source]
depart_option_list_item(node)[source]
visit_option_string(node)[source]
depart_option_string(node)[source]
visit_organization(node)[source]
depart_organization(node)[source]
visit_paragraph(node)[source]
depart_paragraph(node)[source]
visit_problematic(node)[source]
depart_problematic(node)[source]
visit_raw(node)[source]
visit_reference(node)[source]
depart_reference(node)[source]
visit_revision(node)[source]
depart_revision(node)[source]
visit_row(node)[source]
depart_row(node)[source]
visit_rubric(node)[source]
depart_rubric(node)[source]
visit_section(node)[source]
depart_section(node)[source]
visit_sidebar(node)[source]
depart_sidebar(node)[source]
visit_status(node)[source]
depart_status(node)[source]
visit_strong(node)[source]
depart_strong(node)[source]
visit_subscript(node)[source]
depart_subscript(node)[source]
visit_substitution_definition(node)[source]

Internal only.

visit_substitution_reference(node)[source]
visit_subtitle(node)[source]
depart_subtitle(node)[source]
visit_superscript(node)[source]
depart_superscript(node)[source]
visit_system_message(node)[source]
depart_system_message(node)[source]
visit_table(node)[source]
depart_table(node)[source]
visit_target(node)[source]
depart_target(node)[source]
visit_tbody(node)[source]
depart_tbody(node)[source]
visit_term(node)[source]
depart_term(node)[source]
visit_tgroup(node)[source]
depart_tgroup(node)[source]
visit_thead(node)[source]
depart_thead(node)[source]
section_title_tags(node)[source]
visit_title(node)[source]
depart_title(node)[source]
visit_title_reference(node)[source]
depart_title_reference(node)[source]
visit_topic(node)[source]
depart_topic(node)[source]
visit_transition(node)[source]
depart_transition(node)[source]
visit_version(node)[source]
depart_version(node)[source]
unimplemented_visit(node)[source]
class SimpleListChecker(document)[source]

Bases: GenericNodeVisitor

Raise nodes.NodeFound if non-simple list item is encountered.

Here “simple” means a list item containing nothing other than a single paragraph, a simple list, or a paragraph followed by a simple list.

This version also checks for simple field lists and docinfo.

default_visit(node)[source]

Override for generic, uniform traversals.

visit_list_item(node)[source]
pass_node(node)[source]
ignore_node(node)[source]
visit_Text(node)
visit_paragraph(node)
visit_bullet_list(node)
visit_enumerated_list(node)
visit_docinfo(node)
visit_author(node)
visit_authors(node)
visit_address(node)
visit_contact(node)
visit_date(node)
visit_organization(node)
visit_status(node)
visit_version(node)
visit_definition_list(node)
visit_definition_list_item(node)
visit_term(node)
visit_classifier(node)
visit_definition(node)
visit_field_list(node)
visit_field(node)
visit_field_body(node)
visit_field_name(node)
visit_comment(node)
visit_substitution_definition(node)
visit_target(node)
visit_pending(node)