2 # $Id: sax_complete_copy.py 54 2011-04-17 15:44:41Z cynthia $
4 import os
, sys
, argparse
, io
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):
20 class FixTree(xml
.sax
.ContentHandler
):
22 def __init__(self
, mathml
=True, raw_xml
=True):
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
):
34 def characters (self
, characters
):
35 self
._characters
+= characters
38 def startElementNS(self
, name
, qname
, attrs
):
42 self
._write
_string
('<')
44 if attrs
.get((None, 'format')) == 'xml' and self
._raw
_xml
:
45 self
._write
_raw
= True
47 self
._write
_string
('ns1:%s' % el_name
)
49 self
._write
_string
(el_name
)
51 self
._write
_string
(' xmlns:ns1="%s"' % ns
)
53 the_keys
= list(attrs
.keys())
55 for the_key
in the_keys
:
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
))
66 raise InvaidXml('Sorry, but don\'t know what to do with ns "%s"\n' % (ns_prefix
))
68 self
._write
_string
(' %s="%s"' % (att_name
, value
))
69 self
._write
_string
('>')
73 def _write_text(self
, raw
= False):
75 text
= self
._characters
77 text
= xml
.sax
.saxutils
.escape(self
._characters
)
78 self
._write
_string
(text
)
81 def _write_string(self
, the_string
):
82 sys
.stdout
.write(the_string
)
84 def endElementNS(self
, name
, qname
):
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'})
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
)
98 elif el_name
== 'raw' and self
._write
_raw
:
99 self
._write
_text
(raw
= True)
100 self
._write
_raw
= False
104 raise InvaidXml('Should not be namespace "%s" here\n' % (ns
))
106 self
._write
_string
('</%s>' % el_name
)
110 class ConverttoMathml
:
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()
132 def convert_to_mathml(self
):
133 args
= self
._parse
_args
()
135 in_file
= args
.in_file
136 no_mathml
= args
.no_mathml
141 no_rawxml
= args
.no_rawxml
147 if not isinstance(in_file
, str):
149 the_string
= sys
.stdin
.read()
151 read_obj
= StringIO(the_string
)
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)
160 parser
.parse(read_obj
)
161 except xml
.sax
._exceptions
.SAXParseException
as error
:
164 except InvaidXml
as error
:
169 if __name__
== '__main__':
170 main_obj
= ConverttoMathml()
171 main_obj
.convert_to_mathml()