Use custom TypeAlias `StrPath` for `str`-based file system path vars.
[docutils.git] / sandbox / docbook / scripts / rstxml2xml.py
blobb0a5c282eca007c87d804861f825858a61e3443c
1 #!/usr/bin/env python
2 import sys
3 import argparse
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')
7 sys.exit(1)
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
14 pass
16 class ConverttoMathml():
18 def __init__(self):
19 pass
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
26 """
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()
34 return args
36 def convert_to_mathml(self):
37 args = self._parse_args()
38 out_encoding = args.out_encoding
39 if not out_encoding:
40 out_encoding = 'utf8'
41 else:
42 out_encoding = out_encoding[0]
43 in_encoding = args.in_encoding
44 if not in_encoding:
45 in_encoding = 'utf8'
46 else:
47 in_encoding = in_encoding[0]
48 standard_in = False
49 in_file = args.in_file
50 out_file = args.out_file
51 if out_file:
52 out_file = out_file[0]
53 if not isinstance(in_file, str):
54 orig_string = sys.stdin.read()
55 else:
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):
62 math_text = e.text
63 e.text = ''
64 if ma == 'math_block':
65 math_obj = asciitomathml.asciitomathml.AsciiMathML(mstyle={'displaystyle':'true'})
66 else:
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)
74 if out_file:
75 write_obj = open(out_file, mode='w', encoding=out_encoding)
76 write_obj.write(xml_string.decode(out_encoding))
77 write_obj.close()
78 else:
79 sys.stdout.write(xml_string.decode(out_encoding))
83 if __name__ == '__main__':
84 main_obj = ConverttoMathml()
85 main_obj.convert_to_mathml()