#!/usr/bin/env python3
# :Author: David Goodger, Günter Milde
# Based on the html4css1 writer by David Goodger.
# :Maintainer: docutils-develop@lists.sourceforge.net
# :Revision: $Revision: 10272 $
# :Date: $Date: 2005-06-28$
# :Copyright: © 2016 David Goodger, Günter Milde
# :License: Released under the terms of the `2-Clause BSD license`_, in short:
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.
# This file is offered as-is, without any warranty.
#
# .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
"""Common definitions for Docutils HTML writers."""
from __future__ import annotations
__docformat__ = 'reStructuredText'
import base64
import mimetypes
import os
import os.path
import re
import warnings
import xml.etree.ElementTree as ET
from pathlib import Path
import docutils
from docutils import frontend, languages, nodes, utils, writers
from docutils.parsers.rst.directives import length_or_percentage_or_unitless
from docutils.parsers.rst.directives.images import PIL
from docutils.transforms import writer_aux
from docutils.utils.math import (latex2mathml, math2html, tex2mathml_extern,
unichar2tex, wrap_math_code, MathError)
TYPE_CHECKING = False
if TYPE_CHECKING:
from docutils.transforms import Transform
class Writer(writers.Writer):
supported = ('html', 'xhtml') # update in subclass
"""Formats this writer supports."""
settings_spec = (
'HTML Writer Options',
None,
(('Specify the template file (UTF-8 encoded). '
'(default: writer dependent)',
['--template'],
{'metavar': ''}),
('Comma separated list of stylesheet URLs. '
'Overrides previous --stylesheet and --stylesheet-path settings.',
['--stylesheet'],
{'metavar': '', 'overrides': 'stylesheet_path',
'validator': frontend.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': '', 'overrides': 'stylesheet',
'validator': frontend.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': '',
'validator': frontend.validate_comma_separated_list}),
('Embed the stylesheet(s) in the output HTML file. The stylesheet '
'files must be accessible during processing. (default)',
['--embed-stylesheet'],
{'default': True, 'action': 'store_true',
'validator': frontend.validate_boolean}),
('Link to the stylesheet(s) in the output HTML file. ',
['--link-stylesheet'],
{'dest': 'embed_stylesheet', 'action': 'store_false'}),
('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 auto'.split(), 'default': '2',
'metavar': ''}),
('Format for footnote references: one of "superscript" or '
'"brackets". (default: "brackets")',
['--footnote-references'],
{'choices': ['superscript', 'brackets'], 'default': 'brackets',
'metavar': '',
'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': ''}),
('Remove extra vertical whitespace between items of "simple" bullet '
'lists and enumerated lists. (default)',
['--compact-lists'],
{'default': True, 'action': 'store_true',
'validator': frontend.validate_boolean}),
('Disable compact simple bullet and enumerated lists.',
['--no-compact-lists'],
{'dest': 'compact_lists', 'action': 'store_false'}),
('Remove extra vertical whitespace between items of simple field '
'lists. (default)',
['--compact-field-lists'],
{'default': True, 'action': 'store_true',
'validator': frontend.validate_boolean}),
('Disable compact simple field lists.',
['--no-compact-field-lists'],
{'dest': 'compact_field_lists', 'action': 'store_false'}),
('Class value(s) assigned to all tables. '
'Defined styles: borderless, booktabs, '
'align-left, align-center, align-right, '
'colwidths-auto, colwidths-grid.',
['--table-style'],
{'metavar': '\n'
words_and_spaces = re.compile(r'[^ \n]+| +|\n')
# wrap point inside word:
in_word_wrap_point = re.compile(r'.+\W\W.+|[-?].+')
lang_attribute = 'lang' # name changes to 'xml:lang' in XHTML 1.1
special_characters = {ord('&'): '&',
ord('<'): '<',
ord('"'): '"',
ord('>'): '>',
ord('@'): '@', # may thwart address harvesters
}
"""Character references for characters with a special meaning in HTML."""
videotypes = ('video/mp4', 'video/webm', 'video/ogg')
"""MIME types supported by the HTML5