57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
# Authors: David Goodger
|
|
# Contact: goodger@users.sourceforge.net
|
|
# Revision: $Revision$
|
|
# Date: $Date$
|
|
# Copyright: This module has been placed in the public domain.
|
|
|
|
"""
|
|
Simple internal document tree Writer, writes Docutils XML.
|
|
"""
|
|
|
|
__docformat__ = 'reStructuredText'
|
|
|
|
|
|
import docutils
|
|
from docutils import writers
|
|
|
|
|
|
class Writer(writers.Writer):
|
|
|
|
supported = ('xml',)
|
|
"""Formats this writer supports."""
|
|
|
|
settings_spec = (
|
|
'"Docutils XML" Writer Options',
|
|
'Warning: these options may adversely affect whitespace; use them '
|
|
'only for reading convenience.',
|
|
(('Generate XML with newlines before and after tags.',
|
|
['--newlines'], {'action': 'store_true'}),
|
|
('Generate XML with indents and newlines.',
|
|
['--indents'], {'action': 'store_true'}),),)
|
|
|
|
output = None
|
|
"""Final translated form of `document`."""
|
|
|
|
xml_declaration = '<?xml version="1.0" encoding="%s"?>\n'
|
|
#xml_stylesheet = '<?xml-stylesheet type="text/xsl" href="%s"?>\n'
|
|
doctype = (
|
|
'<!DOCTYPE document PUBLIC'
|
|
' "+//IDN docutils.sourceforge.net//DTD Docutils Generic//EN//XML"'
|
|
' "http://docutils.sourceforge.net/spec/docutils.dtd">\n')
|
|
generator = '<!-- Generated by Docutils %s -->\n'
|
|
|
|
def translate(self):
|
|
settings = self.document.settings
|
|
indent = newline = ''
|
|
if settings.newlines:
|
|
newline = '\n'
|
|
if settings.indents:
|
|
newline = '\n'
|
|
indent = ' '
|
|
output_prefix = [self.xml_declaration % settings.output_encoding,
|
|
self.doctype,
|
|
self.generator % docutils.__version__]
|
|
docnode = self.document.asdom().childNodes[0]
|
|
self.output = (''.join(output_prefix)
|
|
+ docnode.toprettyxml(indent, newline))
|