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