1 | # Authors: David Goodger |
---|
2 | # Contact: goodger@users.sourceforge.net |
---|
3 | # Revision: $Revision: 4163 $ |
---|
4 | # Date: $Date: 2005-12-09 05:21:34 +0100 (Fri, 09 Dec 2005) $ |
---|
5 | # Copyright: This module has been placed in the public domain. |
---|
6 | |
---|
7 | """ |
---|
8 | This package contains Docutils Writer modules. |
---|
9 | """ |
---|
10 | |
---|
11 | __docformat__ = 'reStructuredText' |
---|
12 | |
---|
13 | |
---|
14 | import os.path |
---|
15 | import docutils |
---|
16 | from docutils import languages, Component |
---|
17 | from docutils.transforms import universal |
---|
18 | |
---|
19 | |
---|
20 | class Writer(Component): |
---|
21 | |
---|
22 | """ |
---|
23 | Abstract base class for docutils Writers. |
---|
24 | |
---|
25 | Each writer module or package must export a subclass also called 'Writer'. |
---|
26 | Each writer must support all standard node types listed in |
---|
27 | `docutils.nodes.node_class_names`. |
---|
28 | |
---|
29 | The `write()` method is the main entry point. |
---|
30 | """ |
---|
31 | |
---|
32 | component_type = 'writer' |
---|
33 | config_section = 'writers' |
---|
34 | |
---|
35 | def get_transforms(self): |
---|
36 | return Component.get_transforms(self) + [ |
---|
37 | universal.Messages, |
---|
38 | universal.FilterMessages, |
---|
39 | ] |
---|
40 | |
---|
41 | document = None |
---|
42 | """The document to write (Docutils doctree); set by `write`.""" |
---|
43 | |
---|
44 | output = None |
---|
45 | """Final translated form of `document` (Unicode string for text, binary |
---|
46 | string for other forms); set by `translate`.""" |
---|
47 | |
---|
48 | language = None |
---|
49 | """Language module for the document; set by `write`.""" |
---|
50 | |
---|
51 | destination = None |
---|
52 | """`docutils.io` Output object; where to write the document. |
---|
53 | Set by `write`.""" |
---|
54 | |
---|
55 | def __init__(self): |
---|
56 | |
---|
57 | # Currently only used by HTML writer for output fragments: |
---|
58 | self.parts = {} |
---|
59 | """Mapping of document part names to fragments of `self.output`. |
---|
60 | Values are Unicode strings; encoding is up to the client. The 'whole' |
---|
61 | key should contain the entire document output. |
---|
62 | """ |
---|
63 | |
---|
64 | def write(self, document, destination): |
---|
65 | """ |
---|
66 | Process a document into its final form. |
---|
67 | |
---|
68 | Translate `document` (a Docutils document tree) into the Writer's |
---|
69 | native format, and write it out to its `destination` (a |
---|
70 | `docutils.io.Output` subclass object). |
---|
71 | |
---|
72 | Normally not overridden or extended in subclasses. |
---|
73 | """ |
---|
74 | self.document = document |
---|
75 | self.language = languages.get_language( |
---|
76 | document.settings.language_code) |
---|
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 | |
---|
100 | |
---|
101 | class UnfilteredWriter(Writer): |
---|
102 | |
---|
103 | """ |
---|
104 | A writer that passes the document tree on unchanged (e.g. a |
---|
105 | serializer.) |
---|
106 | |
---|
107 | Documents written by UnfilteredWriters are typically reused at a |
---|
108 | later date using a subclass of `readers.ReReader`. |
---|
109 | """ |
---|
110 | |
---|
111 | def get_transforms(self): |
---|
112 | # Do not add any transforms. When the document is reused |
---|
113 | # later, the then-used writer will add the appropriate |
---|
114 | # transforms. |
---|
115 | return Component.get_transforms(self) |
---|
116 | |
---|
117 | |
---|
118 | _writer_aliases = { |
---|
119 | 'html': 'html4css1', |
---|
120 | 'latex': 'latex2e', |
---|
121 | 'pprint': 'pseudoxml', |
---|
122 | 'pformat': 'pseudoxml', |
---|
123 | 'pdf': 'rlpdf', |
---|
124 | 'xml': 'docutils_xml', |
---|
125 | 's5': 's5_html'} |
---|
126 | |
---|
127 | def get_writer_class(writer_name): |
---|
128 | """Return the Writer class from the `writer_name` module.""" |
---|
129 | writer_name = writer_name.lower() |
---|
130 | if _writer_aliases.has_key(writer_name): |
---|
131 | writer_name = _writer_aliases[writer_name] |
---|
132 | module = __import__(writer_name, globals(), locals()) |
---|
133 | return module.Writer |
---|