2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
6 This package contains Docutils Writer modules.
9 __docformat__
= 'reStructuredText'
15 from docutils
import languages
, Component
16 from docutils
.transforms
import universal
17 if sys
.version_info
< (2,5):
18 from docutils
._compat
import __import__
21 class Writer(Component
):
24 Abstract base class for docutils Writers.
26 Each writer module or package must export a subclass also called 'Writer'.
27 Each writer must support all standard node types listed in
28 `docutils.nodes.node_class_names`.
30 The `write()` method is the main entry point.
33 component_type
= 'writer'
34 config_section
= 'writers'
36 def get_transforms(self
):
37 return Component
.get_transforms(self
) + [
39 universal
.FilterMessages
,
40 universal
.StripClassesAndElements
,]
43 """The document to write (Docutils doctree); set by `write`."""
46 """Final translated form of `document` (Unicode string for text, binary
47 string for other forms); set by `translate`."""
50 """Language module for the document; set by `write`."""
53 """`docutils.io` Output object; where to write the document.
58 # Used by HTML and LaTeX writer for output fragments:
60 """Mapping of document part names to fragments of `self.output`.
61 Values are Unicode strings; encoding is up to the client. The 'whole'
62 key should contain the entire document output.
65 def write(self
, document
, destination
):
67 Process a document into its final form.
69 Translate `document` (a Docutils document tree) into the Writer's
70 native format, and write it out to its `destination` (a
71 `docutils.io.Output` subclass object).
73 Normally not overridden or extended in subclasses.
75 self
.document
= document
76 self
.language
= languages
.get_language(
77 document
.settings
.language_code
,
79 self
.destination
= destination
81 output
= self
.destination
.write(self
.output
)
86 Do final translation of `self.document` into `self.output`. Called
87 from `write`. Override in subclasses.
89 Usually done with a `docutils.nodes.NodeVisitor` subclass, in
90 combination with a call to `docutils.nodes.Node.walk()` or
91 `docutils.nodes.Node.walkabout()`. The ``NodeVisitor`` subclass must
92 support all standard elements (listed in
93 `docutils.nodes.node_class_names`) and possibly non-standard elements
94 used by the current Reader as well.
96 raise NotImplementedError('subclass must override this method')
98 def assemble_parts(self
):
99 """Assemble the `self.parts` dictionary. Extend in subclasses."""
100 self
.parts
['whole'] = self
.output
101 self
.parts
['encoding'] = self
.document
.settings
.output_encoding
102 self
.parts
['version'] = docutils
.__version
__
105 class UnfilteredWriter(Writer
):
108 A writer that passes the document tree on unchanged (e.g. a
111 Documents written by UnfilteredWriters are typically reused at a
112 later date using a subclass of `readers.ReReader`.
115 def get_transforms(self
):
116 # Do not add any transforms. When the document is reused
117 # later, the then-used writer will add the appropriate
119 return Component
.get_transforms(self
)
125 'pprint': 'pseudoxml',
126 'pformat': 'pseudoxml',
128 'xml': 'docutils_xml',
131 def get_writer_class(writer_name
):
132 """Return the Writer class from the `writer_name` module."""
133 writer_name
= writer_name
.lower()
134 if writer_name
in _writer_aliases
:
135 writer_name
= _writer_aliases
[writer_name
]
136 module
= __import__(writer_name
, globals(), locals(), level
=1)