2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
6 Simple internal document tree Writer, writes Docutils XML.
9 __docformat__
= 'reStructuredText'
13 from docutils
import frontend
, writers
16 class Writer(writers
.Writer
):
19 """Formats this writer supports."""
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.',
27 {'action': 'store_true', 'validator': frontend
.validate_boolean
}),
28 ('Generate XML with indents and newlines.',
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.',
37 {'dest': 'doctype_declaration', 'default': 1,
38 'action': 'store_false', 'validator': frontend
.validate_boolean
}),))
40 settings_defaults
= {'output_encoding_error_handler': 'xmlcharrefreplace'}
42 config_section
= 'docutils_xml writer'
43 config_section_dependencies
= ('writers',)
46 """Final translated form of `document`."""
48 xml_declaration
= '<?xml version="1.0" encoding="%s"?>\n'
49 #xml_stylesheet = '<?xml-stylesheet type="text/xsl" href="%s"?>\n'
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'
57 settings
= self
.document
.settings
65 if settings
.xml_declaration
:
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
))