Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/docutils/writers/__init__.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 # $Id: __init__.py 8239 2018-11-21 21:46:00Z milde $ | |
2 # Author: David Goodger <goodger@python.org> | |
3 # Copyright: This module has been placed in the public domain. | |
4 | |
5 """ | |
6 This package contains Docutils Writer modules. | |
7 """ | |
8 | |
9 __docformat__ = 'reStructuredText' | |
10 | |
11 import os.path | |
12 import sys | |
13 | |
14 import docutils | |
15 from docutils import languages, Component | |
16 from docutils.transforms import universal | |
17 | |
18 | |
19 class Writer(Component): | |
20 | |
21 """ | |
22 Abstract base class for docutils Writers. | |
23 | |
24 Each writer module or package must export a subclass also called 'Writer'. | |
25 Each writer must support all standard node types listed in | |
26 `docutils.nodes.node_class_names`. | |
27 | |
28 The `write()` method is the main entry point. | |
29 """ | |
30 | |
31 component_type = 'writer' | |
32 config_section = 'writers' | |
33 | |
34 def get_transforms(self): | |
35 return Component.get_transforms(self) + [ | |
36 universal.Messages, | |
37 universal.FilterMessages, | |
38 universal.StripClassesAndElements,] | |
39 | |
40 document = None | |
41 """The document to write (Docutils doctree); set by `write`.""" | |
42 | |
43 output = None | |
44 """Final translated form of `document` (Unicode string for text, binary | |
45 string for other forms); set by `translate`.""" | |
46 | |
47 language = None | |
48 """Language module for the document; set by `write`.""" | |
49 | |
50 destination = None | |
51 """`docutils.io` Output object; where to write the document. | |
52 Set by `write`.""" | |
53 | |
54 def __init__(self): | |
55 | |
56 # Used by HTML and LaTeX writer for output fragments: | |
57 self.parts = {} | |
58 """Mapping of document part names to fragments of `self.output`. | |
59 Values are Unicode strings; encoding is up to the client. The 'whole' | |
60 key should contain the entire document output. | |
61 """ | |
62 | |
63 def write(self, document, destination): | |
64 """ | |
65 Process a document into its final form. | |
66 | |
67 Translate `document` (a Docutils document tree) into the Writer's | |
68 native format, and write it out to its `destination` (a | |
69 `docutils.io.Output` subclass object). | |
70 | |
71 Normally not overridden or extended in subclasses. | |
72 """ | |
73 self.document = document | |
74 self.language = languages.get_language( | |
75 document.settings.language_code, | |
76 document.reporter) | |
77 self.destination = destination | |
78 self.translate() | |
79 output = self.destination.write(self.output) | |
80 return output | |
81 | |
82 def translate(self): | |
83 """ | |
84 Do final translation of `self.document` into `self.output`. Called | |
85 from `write`. Override in subclasses. | |
86 | |
87 Usually done with a `docutils.nodes.NodeVisitor` subclass, in | |
88 combination with a call to `docutils.nodes.Node.walk()` or | |
89 `docutils.nodes.Node.walkabout()`. The ``NodeVisitor`` subclass must | |
90 support all standard elements (listed in | |
91 `docutils.nodes.node_class_names`) and possibly non-standard elements | |
92 used by the current Reader as well. | |
93 """ | |
94 raise NotImplementedError('subclass must override this method') | |
95 | |
96 def assemble_parts(self): | |
97 """Assemble the `self.parts` dictionary. Extend in subclasses.""" | |
98 self.parts['whole'] = self.output | |
99 self.parts['encoding'] = self.document.settings.output_encoding | |
100 self.parts['version'] = docutils.__version__ | |
101 | |
102 | |
103 class UnfilteredWriter(Writer): | |
104 | |
105 """ | |
106 A writer that passes the document tree on unchanged (e.g. a | |
107 serializer.) | |
108 | |
109 Documents written by UnfilteredWriters are typically reused at a | |
110 later date using a subclass of `readers.ReReader`. | |
111 """ | |
112 | |
113 def get_transforms(self): | |
114 # Do not add any transforms. When the document is reused | |
115 # later, the then-used writer will add the appropriate | |
116 # transforms. | |
117 return Component.get_transforms(self) | |
118 | |
119 | |
120 _writer_aliases = { | |
121 'html': 'html4css1', # may change to html5 some day | |
122 'html4': 'html4css1', | |
123 'html5': 'html5_polyglot', | |
124 'latex': 'latex2e', | |
125 'pprint': 'pseudoxml', | |
126 'pformat': 'pseudoxml', | |
127 'pdf': 'rlpdf', | |
128 's5': 's5_html', | |
129 'xelatex': 'xetex', | |
130 'xhtml': 'html5_polyglot', | |
131 'xhtml10': 'html4css1', | |
132 'xml': 'docutils_xml'} | |
133 | |
134 def get_writer_class(writer_name): | |
135 """Return the Writer class from the `writer_name` module.""" | |
136 writer_name = writer_name.lower() | |
137 if writer_name in _writer_aliases: | |
138 writer_name = _writer_aliases[writer_name] | |
139 try: | |
140 module = __import__(writer_name, globals(), locals(), level=1) | |
141 except ImportError: | |
142 module = __import__(writer_name, globals(), locals(), level=0) | |
143 return module.Writer |