diff options
| author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2023-04-28 21:26:36 +0000 |
|---|---|---|
| committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2023-04-28 21:26:36 +0000 |
| commit | c9c4e3598dd04853fc82c9c66bc993d8741a5710 (patch) | |
| tree | dcefc414af37c4c23ddd2a805512770528b0464a | |
| parent | de132e1969e107acc8c2ddfa49007343b2e7f1c0 (diff) | |
| download | docutils-c9c4e3598dd04853fc82c9c66bc993d8741a5710.tar.gz | |
Add output encoding error handler to the parts provided by all writers.
The new generic part "errors" returned by default
contains the `output_encoding_error_handler` setting,
which may make-or-break encoding ``parts['whole']``.
Allows use of `core.publish_parts()` to get the document as `str`,
post-process it and encode with user configurated encoding and
encoding-error handler.
git-svn-id: https://svn.code.sf.net/p/docutils/code/trunk/docutils@9368 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
| -rw-r--r-- | docs/api/publisher.txt | 5 | ||||
| -rw-r--r-- | docutils/core.py | 2 | ||||
| -rw-r--r-- | docutils/writers/__init__.py | 13 | ||||
| -rwxr-xr-x | test/test_writers/test_html4css1_parts.py | 1 | ||||
| -rw-r--r-- | test/test_writers/test_html5_polyglot_parts.py | 1 | ||||
| -rw-r--r-- | test/test_writers/test_latex2e_misc.py | 1 |
6 files changed, 17 insertions, 6 deletions
diff --git a/docs/api/publisher.txt b/docs/api/publisher.txt index 4d725e62c..3ff5c4e6c 100644 --- a/docs/api/publisher.txt +++ b/docs/api/publisher.txt @@ -140,7 +140,10 @@ Parts Provided By All Writers ````````````````````````````` _`encoding` - The output encoding setting. + The `output_encoding`_ setting. + +_`errors` + The `output_encoding_error_handler`_ setting. _`version` The version of Docutils used. diff --git a/docutils/core.py b/docutils/core.py index 0d3e0cb95..3f99526c9 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -494,7 +494,7 @@ def publish_parts(source, source_path=None, source_class=io.StringInput, e.g.:: parts = publish_parts(...) - body = parts['body'].encode(parts['encoding']) + body = parts['body'].encode(parts['encoding'], parts['errors']) See the `API documentation`__ for details on the provided parts. diff --git a/docutils/writers/__init__.py b/docutils/writers/__init__.py index d15ccd29f..05a445aca 100644 --- a/docutils/writers/__init__.py +++ b/docutils/writers/__init__.py @@ -55,11 +55,11 @@ class Writer(Component): def __init__(self): - # Used by HTML and LaTeX writer for output fragments: self.parts = {} """Mapping of document part names to fragments of `self.output`. - Values are Unicode strings; encoding is up to the client. The 'whole' - key should contain the entire document output. + + See `Writer.assemble_parts()` below and + <https://docutils.sourceforge.io/docs/api/publisher.html>. """ def write(self, document, destination): @@ -95,9 +95,14 @@ class Writer(Component): raise NotImplementedError('subclass must override this method') def assemble_parts(self): - """Assemble the `self.parts` dictionary. Extend in subclasses.""" + """Assemble the `self.parts` dictionary. Extend in subclasses. + + See <https://docutils.sourceforge.io/docs/api/publisher.html>. + """ self.parts['whole'] = self.output self.parts['encoding'] = self.document.settings.output_encoding + self.parts['errors'] = ( + self.document.settings.output_encoding_error_handler) self.parts['version'] = docutils.__version__ diff --git a/test/test_writers/test_html4css1_parts.py b/test/test_writers/test_html4css1_parts.py index 8bd0deea3..6dcfbd78c 100755 --- a/test/test_writers/test_html4css1_parts.py +++ b/test/test_writers/test_html4css1_parts.py @@ -75,6 +75,7 @@ class Html4WriterPublishPartsTestCase(unittest.TestCase): del parts['head'] del parts['head_prefix'] del parts['encoding'] + del parts['errors'] del parts['version'] # remove standard portions: parts['meta'] = parts['meta'].replace(self.standard_meta_value, '') diff --git a/test/test_writers/test_html5_polyglot_parts.py b/test/test_writers/test_html5_polyglot_parts.py index acd959dfb..799ee983a 100644 --- a/test/test_writers/test_html5_polyglot_parts.py +++ b/test/test_writers/test_html5_polyglot_parts.py @@ -73,6 +73,7 @@ class Html5WriterPublishPartsTestCase(unittest.TestCase): del parts['head'] del parts['head_prefix'] del parts['encoding'] + del parts['errors'] del parts['version'] # remove standard portions: parts['meta'] = parts['meta'].replace(self.standard_meta_value, '') diff --git a/test/test_writers/test_latex2e_misc.py b/test/test_writers/test_latex2e_misc.py index 32b1edc29..74b281327 100644 --- a/test/test_writers/test_latex2e_misc.py +++ b/test/test_writers/test_latex2e_misc.py @@ -68,6 +68,7 @@ class PublishTestCase(unittest.TestCase): 'dedication', 'docinfo', 'encoding', + 'errors', 'fallbacks', 'head_prefix', 'latex_preamble', |
