Release 0.12: set version number to 0.12
[docutils.git] / sandbox / paultremblay / scripts / rsttoxml.py
blobec689015bc5fa1974cfbd1eaf1c343f066b24387
1 import docutils.core
2 from docutils import writers
4 import docutils
5 from docutils import frontend, writers
6 from docutils import languages, Component
7 from docutils.transforms import universal
8 from docutils.transforms import math
10 __docformat__ = 'reStructuredText'
12 class MWriter(writers.Writer, Component):
14 supported = ('xml',)
15 """Formats this writer supports."""
17 settings_spec = (
18 '"Docutils XML" Writer Options',
19 'Warning: the --newlines and --indents options may adversely affect '
20 'whitespace; use them only for reading convenience.',
21 (('Generate XML with newlines before and after tags.',
22 ['--newlines'],
23 {'action': 'store_true', 'validator': frontend.validate_boolean}),
24 ('Generate XML with indents and newlines.',
25 ['--indents'],
26 {'action': 'store_true', 'validator': frontend.validate_boolean}),
27 ('Omit the XML declaration. Use with caution.',
28 ['--no-xml-declaration'],
29 {'dest': 'xml_declaration', 'default': 1, 'action': 'store_false',
30 'validator': frontend.validate_boolean}),
31 ('Omit the DOCTYPE declaration.',
32 ['--no-doctype'],
33 {'dest': 'doctype_declaration', 'default': 1,
34 'action': 'store_false', 'validator': frontend.validate_boolean}),
35 ('Convert LaTeX math in math_block and math to MathML',
36 ['--latex-mathml'],
37 {'dest': 'latex_mathml', 'default':False,
38 'action': 'store_true', 'validator': frontend.validate_boolean}),
39 ('Convert ASCII math in math_block and math to MathML',
40 ['--ascii-mathml'],
41 {'dest': 'ascii_mathml', 'default':False,
42 'action': 'store_true', 'validator': frontend.validate_boolean}),
46 settings_defaults = {'output_encoding_error_handler': 'xmlcharrefreplace'}
48 config_section = 'docutils_xml writer'
49 config_section_dependencies = ('writers',)
51 output = None
52 """Final translated form of `document`."""
54 xml_declaration = '<?xml version="1.0" encoding="%s"?>\n'
55 #xml_stylesheet = '<?xml-stylesheet type="text/xsl" href="%s"?>\n'
56 doctype = (
57 '<!DOCTYPE document PUBLIC'
58 ' "+//IDN docutils.sourceforge.net//DTD Docutils Generic//EN//XML"'
59 ' "http://docutils.sourceforge.net/docs/ref/docutils.dtd">\n')
60 generator = '<!-- Generated by Docutils %s -->\n'
62 def get_transforms(self):
63 return Component.get_transforms(self) + [
64 universal.Messages,
65 universal.FilterMessages,
66 universal.StripClassesAndElements,
67 math.LaTeXmath2MathML,
68 math.Asciimath2MathML,
71 def translate(self):
72 settings = self.document.settings
73 indent = newline = ''
74 if settings.newlines:
75 newline = '\n'
76 if settings.indents:
77 newline = '\n'
78 indent = ' '
79 output_prefix = []
80 if settings.xml_declaration:
81 output_prefix.append(
82 self.xml_declaration % settings.output_encoding)
83 if settings.doctype_declaration:
84 output_prefix.append(self.doctype)
85 output_prefix.append(self.generator % docutils.__version__)
86 docnode = self.document.asdom().childNodes[0]
87 self.output = (''.join(output_prefix)
88 + docnode.toprettyxml(indent, newline))
91 class MPublisher(docutils.core.Publisher):
92 pass
96 def set_writer(self, writer_name):
97 """Set `self.writer` by name."""
98 writer_class = writers.get_writer_class(writer_name)
99 # self.writer = writer_class()
100 self.writer = MWriter()
103 A minimal front end to the Docutils Publisher, producing Docutils XML.
106 try:
107 import locale
108 locale.setlocale(locale.LC_ALL, '')
109 except:
110 pass
112 # from docutils.core import publish_cmdline, default_description
113 from docutils.core import default_description
116 description = ('Generates Docutils-native XML from standalone '
117 'reStructuredText sources. ' + default_description)
118 default_usage = '%prog [options] [<source> [<destination>]]'
119 default_description = ('Reads from <source> (default is stdin) and writes to '
120 '<destination> (default is stdout). See '
121 '<http://docutils.sf.net/docs/user/config.html> for '
122 'the full reference.')
125 def publish_cmdline(reader=None, reader_name='standalone',
126 parser=None, parser_name='restructuredtext',
127 writer=None, writer_name='pseudoxml',
128 settings=None, settings_spec=None,
129 settings_overrides=None, config_section=None,
130 enable_exit_status=1, argv=None,
131 usage=default_usage, description=default_description):
133 Set up & run a `Publisher` for command-line-based file I/O (input and
134 output file paths taken automatically from the command line). Return the
135 encoded string output also.
137 Parameters: see `publish_programmatically` for the remainder.
139 - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``.
140 - `usage`: Usage string, output if there's a problem parsing the command
141 line.
142 - `description`: Program description, output for the "--help" option
143 (along with command-line option descriptions).
145 pub = MPublisher(reader, parser, writer, settings=settings)
146 pub.set_components(reader_name, parser_name, writer_name)
147 output = pub.publish(
148 argv, usage, description, settings_spec, settings_overrides,
149 config_section=config_section, enable_exit_status=enable_exit_status)
150 return output
152 publish_cmdline(writer_name='xml', description=description)