4 if sys
.version_info
< (3,0):
5 sys
.stderr
.write('Need python 3.0 or greater\n')
6 sys
.stderr
.wrtie('Script quiting\n')
8 import xml
.etree
.cElementTree
as etree
9 import asciitomathml
.asciitomathml
10 from xml
.etree
.ElementTree
import Element
, tostring
12 class InvaidXml(Exception):
13 # not used at this point
16 class ConverttoMathml():
21 def _parse_args(self
):
22 desc
= """Inserts Mathml elements into an rst document.
23 In order to use the script, first run rs2txml.py on the RST file.
24 Then run this script on that resulting file
25 Or, in one pass: rst2xml.py <infile> | python3 rstxml2mathml.py
27 parser
= argparse
.ArgumentParser(description
=desc
)
28 parser
.add_argument('in_file', default
= sys
.stdin
, nargs
='?',
29 help = 'the file to input; default is standard in')
30 parser
.add_argument('-o,--out', nargs
=1, dest
="out_file" )
31 parser
.add_argument('--out-encoding', nargs
=1, dest
="out_encoding" )
32 parser
.add_argument('--in-encoding', nargs
=1, dest
="in_encoding" )
33 args
= parser
.parse_args()
36 def convert_to_mathml(self
):
37 args
= self
._parse
_args
()
38 out_encoding
= args
.out_encoding
42 out_encoding
= out_encoding
[0]
43 in_encoding
= args
.in_encoding
47 in_encoding
= in_encoding
[0]
49 in_file
= args
.in_file
50 out_file
= args
.out_file
52 out_file
= out_file
[0]
53 if not isinstance(in_file
, str):
54 orig_string
= sys
.stdin
.read()
56 read_obj
= open(in_file
, mode
='r', encoding
=in_encoding
)
57 lines
= read_obj
.readlines()
58 orig_string
= ''.join(lines
)
59 old_tree
= etree
.XML(orig_string
)
60 for ma
in ['math_block', 'math']:
61 for e
in old_tree
.iter(ma
):
64 if ma
== 'math_block':
65 math_obj
= asciitomathml
.asciitomathml
.AsciiMathML(mstyle
={'displaystyle':'true'})
67 math_obj
= asciitomathml
.asciitomathml
.AsciiMathML()
68 math_obj
.parse_string(math_text
)
69 math_tree
= math_obj
.get_tree()
70 math_string
= tostring(math_tree
, encoding
='ascii') # <encoding=utf8> cannot appear here
71 math_tree
= etree
.XML(math_string
)
72 e
.insert(0, math_tree
)
73 xml_string
= tostring(old_tree
, encoding
=out_encoding
)
75 write_obj
= open(out_file
, mode
='w', encoding
=out_encoding
)
76 write_obj
.write(xml_string
.decode(out_encoding
))
79 sys
.stdout
.write(xml_string
.decode(out_encoding
))
83 if __name__
== '__main__':
84 main_obj
= ConverttoMathml()
85 main_obj
.convert_to_mathml()