1 # $Id: docutils_xml.py 4564 2006-05-21 20:44:42Z wiemann $ |
|
2 # Author: David Goodger <goodger@python.org> |
|
3 # Copyright: This module has been placed in the public domain. |
|
4 |
|
5 """ |
|
6 Simple internal document tree Writer, writes Docutils XML. |
|
7 """ |
|
8 |
|
9 __docformat__ = 'reStructuredText' |
|
10 |
|
11 |
|
12 import docutils |
|
13 from docutils import frontend, writers |
|
14 |
|
15 |
|
16 class Writer(writers.Writer): |
|
17 |
|
18 supported = ('xml',) |
|
19 """Formats this writer supports.""" |
|
20 |
|
21 settings_spec = ( |
|
22 '"Docutils XML" Writer Options', |
|
23 'Warning: the --newlines and --indents options may adversely affect ' |
|
24 'whitespace; use them only for reading convenience.', |
|
25 (('Generate XML with newlines before and after tags.', |
|
26 ['--newlines'], |
|
27 {'action': 'store_true', 'validator': frontend.validate_boolean}), |
|
28 ('Generate XML with indents and newlines.', |
|
29 ['--indents'], |
|
30 {'action': 'store_true', 'validator': frontend.validate_boolean}), |
|
31 ('Omit the XML declaration. Use with caution.', |
|
32 ['--no-xml-declaration'], |
|
33 {'dest': 'xml_declaration', 'default': 1, 'action': 'store_false', |
|
34 'validator': frontend.validate_boolean}), |
|
35 ('Omit the DOCTYPE declaration.', |
|
36 ['--no-doctype'], |
|
37 {'dest': 'doctype_declaration', 'default': 1, |
|
38 'action': 'store_false', 'validator': frontend.validate_boolean}),)) |
|
39 |
|
40 settings_defaults = {'output_encoding_error_handler': 'xmlcharrefreplace'} |
|
41 |
|
42 config_section = 'docutils_xml writer' |
|
43 config_section_dependencies = ('writers',) |
|
44 |
|
45 output = None |
|
46 """Final translated form of `document`.""" |
|
47 |
|
48 xml_declaration = '<?xml version="1.0" encoding="%s"?>\n' |
|
49 #xml_stylesheet = '<?xml-stylesheet type="text/xsl" href="%s"?>\n' |
|
50 doctype = ( |
|
51 '<!DOCTYPE document PUBLIC' |
|
52 ' "+//IDN docutils.sourceforge.net//DTD Docutils Generic//EN//XML"' |
|
53 ' "http://docutils.sourceforge.net/docs/ref/docutils.dtd">\n') |
|
54 generator = '<!-- Generated by Docutils %s -->\n' |
|
55 |
|
56 def translate(self): |
|
57 settings = self.document.settings |
|
58 indent = newline = '' |
|
59 if settings.newlines: |
|
60 newline = '\n' |
|
61 if settings.indents: |
|
62 newline = '\n' |
|
63 indent = ' ' |
|
64 output_prefix = [] |
|
65 if settings.xml_declaration: |
|
66 output_prefix.append( |
|
67 self.xml_declaration % settings.output_encoding) |
|
68 if settings.doctype_declaration: |
|
69 output_prefix.append(self.doctype) |
|
70 output_prefix.append(self.generator % docutils.__version__) |
|
71 docnode = self.document.asdom().childNodes[0] |
|
72 self.output = (''.join(output_prefix) |
|
73 + docnode.toprettyxml(indent, newline)) |
|