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_optionmethod.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_booleanfunction, 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')
- 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
NodeVisitorsubclass must support all standard elements (listed in docutils.nodes.node_class_names) and possibly non-standard elements used by the current Reader as well.
- 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:
NodeVisitorGeneric 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:
Override both visit_* and depart_* methods, don’t call the parent functions.
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>')
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)
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.
- stylesheet_link = '<link rel="stylesheet" href="%s" type="text/css" />\n'
- 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: '"', 38: '&', 60: '<', 62: '>', 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.
- 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.
- attribution_formats = {'dash': ('—', ''), 'none': ('', ''), 'parens': ('(', ')'), 'parentheses': ('(', ')')}
- visit_comment(node, sub=<built-in method sub of re.Pattern object>)[source]
Escape double-dashes in comment text.
- math_tags = {'html': ('div', 'span', 'formula'), 'latex': ('pre', 'tt', 'math'), 'mathjax': ('div', 'span', 'math'), 'mathml': ('div', '', '')}
- class SimpleListChecker(document)[source]
Bases:
GenericNodeVisitorRaise 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.
- 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_copyright(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)