Needed files for examples.
[docutils/kirr.git] / sandbox / docbook / scripts / rstxml2xml.py
blobd4f7251c4fa447c5dbb50fdbb800f8e659f2aaac
1 #!/usr/bin/env python
2 # $Id: sax_complete_copy.py 54 2011-04-17 15:44:41Z cynthia $
4 import os, sys, argparse, io
5 import xml.sax.handler
6 from xml.sax.handler import feature_namespaces
7 from io import StringIO
10 import asciitomathml.asciitomathml
11 from xml.etree.ElementTree import Element, tostring
12 import xml.etree.cElementTree as etree
13 import tempfile, subprocess, os
14 from xml.sax import InputSource
16 class InvaidXml(Exception):
17 pass
20 class FixTree(xml.sax.ContentHandler):
22 def __init__(self, mathml=True, raw_xml=True):
23 self._characters = ''
24 self._mathml = mathml
25 self._raw_xml = raw_xml
26 self._write_raw = False
27 self._ns_dict = {'http://www.w3.org/XML/1998/namespace': "xml"}
30 def startDocument(self):
31 pass
34 def characters (self, characters):
35 self._characters += characters
38 def startElementNS(self, name, qname, attrs):
39 self._write_text()
40 ns = name[0]
41 el_name = name[1]
42 self._write_string('<')
43 if el_name == 'raw':
44 if attrs.get((None, 'format')) == 'xml' and self._raw_xml:
45 self._write_raw = True
46 if ns:
47 self._write_string('ns1:%s' % el_name)
48 else:
49 self._write_string(el_name)
50 if ns:
51 self._write_string(' xmlns:ns1="%s"' % ns)
53 the_keys = list(attrs.keys())
54 counter = 1
55 for the_key in the_keys:
56 counter +=1
57 ns_att = the_key[0]
58 att_name = the_key[1]
59 value = attrs[the_key]
60 ns_prefix = self._ns_dict.get(ns_att)
61 if ns_att and not ns_prefix:
62 raise InvaidXml('No namespace for "%s"\n' % (ns_att))
63 if ns_att and ns_prefix == 'xml':
64 self._write_string(' xml:%s="%s"' % (att_name, value))
65 elif ns_att:
66 raise InvaidXml('Sorry, but don\'t know what to do with ns "%s"\n' % (ns_prefix))
67 else:
68 self._write_string(' %s="%s"' % (att_name, value))
69 self._write_string('>')
73 def _write_text(self, raw = False):
74 if raw:
75 text = self._characters
76 else:
77 text = xml.sax.saxutils.escape(self._characters)
78 self._write_string(text)
79 self._characters = ''
81 def _write_string(self, the_string):
82 sys.stdout.write(the_string)
84 def endElementNS(self, name, qname):
85 ns = name[0]
86 el_name = name[1]
87 if (el_name == 'math_block' and self._mathml) or (el_name == 'math' and self._mathml) :
88 # math_obj = asciitomathml.asciitomathml.AsciiMathML()
89 if el_name == 'math_block':
90 math_obj = asciitomathml.asciitomathml.AsciiMathML(mstyle={'displaystyle':'true'})
91 else:
92 math_obj = asciitomathml.asciitomathml.AsciiMathML()
93 math_obj.parse_string(self._characters)
94 math_tree = math_obj.get_tree()
95 math_string = tostring(math_tree, encoding="us-ascii")
96 self._write_string(math_string)
97 self._characters = ''
98 elif el_name == 'raw' and self._write_raw:
99 self._write_text(raw = True)
100 self._write_raw = False
101 else:
102 self._write_text()
103 if ns:
104 raise InvaidXml('Should not be namespace "%s" here\n' % (ns))
105 else:
106 self._write_string('</%s>' % el_name)
110 class ConverttoMathml:
113 def _init_(self):
114 pass
116 def _parse_args(self):
117 desc = """Inserts Mathmx elements into an rst document.
118 In order to use the script, first run rs2txml.py on the RST file.
119 Then run this script on that resulting file
120 Or, in one pass: rst2xml.py <infile> | python3 rstxml2mathml.py
122 parser = argparse.ArgumentParser(description=desc)
123 parser.add_argument('--no-mathml', action="store_const", const=True, dest='no_mathml')
124 parser.add_argument('--no-rawxml', action="store_const", const=True, dest='no_rawxml')
125 parser.add_argument('--fix-sh', action="store_const", const=True, dest='fix_sh', default=False)
126 parser.add_argument('in_file', default = sys.stdin, nargs='?',
127 help = 'the file to input; default is standard in')
128 args = parser.parse_args()
129 return args
132 def convert_to_mathml(self):
133 args = self._parse_args()
134 standard_in = False
135 in_file = args.in_file
136 no_mathml = args.no_mathml
137 if no_mathml:
138 mathml = False
139 else:
140 mathml = True
141 no_rawxml = args.no_rawxml
142 if no_rawxml:
143 raw_xml = False
144 else:
145 raw_xml = True
146 fix_sh = args.fix_sh
147 if not isinstance(in_file, str):
148 standard_in = True
149 the_string = sys.stdin.read()
150 if standard_in:
151 read_obj = StringIO(the_string)
152 else:
153 read_obj = open(in_file, 'r')
154 the_handle=FixTree(mathml= mathml, raw_xml = raw_xml)
155 parser = xml.sax.make_parser()
156 parser.setFeature(feature_namespaces, 1)
157 parser.setContentHandler(the_handle)
158 parser.setFeature("http://xml.org/sax/features/external-general-entities", True)
159 try:
160 parser.parse(read_obj)
161 except xml.sax._exceptions.SAXParseException as error:
162 msg = error.args[0]
163 raise InvaidXml(msg)
164 except InvaidXml as error:
165 msg = error.args[0]
166 raise InvaidXml(msg)
167 read_obj.close()
169 if __name__ == '__main__':
170 main_obj = ConverttoMathml()
171 main_obj.convert_to_mathml()