1 import io
, argparse
, sys
2 import xml
.etree
.cElementTree
as etree
7 desc
= """Inserts Mathmx elements into an rst document.
8 In order to use the script, first run rs2txml.py on the RST file.
9 Then run this script on that resulting file
10 Or, in one pass: rst2xml.py <infile> | python3 rstxml2mathml.py
12 parser
= argparse
.ArgumentParser(description
=desc
)
13 parser
.add_argument('in_file', default
= sys
.stdin
, nargs
='?',
14 help = 'the file to input; default is standard in')
15 args
= parser
.parse_args()
18 def convert_to_mathml():
21 in_file
= args
.in_file
22 if isinstance(in_file
, io
.TextIOWrapper
):
23 the_string
= sys
.stdin
.read()
24 xml_tree
= etree
.fromstring(the_string
)
26 xml_tree
= etree
.ElementTree().parse(in_file
)
27 for element
in xml_tree
.iter():
28 if element
.tag
== 'math' or element
.tag
== 'math_block':
29 mathml_tree
= asciimathml
.parse(element
.text
)
30 mathml_tree
.set("title", element
.text
)
31 mathml_tree
.set("xmlns", "http://www.w3.org/1998/Math/MathML")
32 element
.append(etree
.XML(etree
.tostring(mathml_tree
)))
34 string_tree
= etree
.tostring(xml_tree
, encoding
="utf-8")
35 if sys
.version_info
< (3,):
36 sys
.stdout
.write(string_tree
)
38 sys
.stdout
.write(string_tree
.decode())
40 if __name__
== '__main__':