-
Notifications
You must be signed in to change notification settings - Fork 18
/
converter.py
executable file
·88 lines (76 loc) · 3.07 KB
/
converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import click
import os
import requests
import sys
from lxml import etree
input_formats = ['asciimath', 'latex', 'mathml']
output_formats = ['latex', 'mathml']
def mathml2latex_yarosh(equation):
""" MathML to LaTeX conversion with XSLT from Vasil Yaroshevich """
script_base_path = os.path.dirname(os.path.realpath(__file__))
xslt_file = os.path.join(script_base_path, 'xsl_yarosh', 'mmltex.xsl')
dom = etree.fromstring(equation)
xslt = etree.parse(xslt_file)
transform = etree.XSLT(xslt)
newdom = transform(dom)
return unicode(newdom)
def _call_mathml_cloud(equation, mathtype):
""" the HTTP POST to MathMLCloud server """
try:
resp = requests.post('https://api.mathmlcloud.org/equation',
{'math': equation, 'mathType': mathtype, 'mml': 'true', 'description': 'true'})
data = resp.json()
try:
mathml = data['components'][0]['source']
return mathml
except IndexError:
# bug in MathML Cloud. It sometimes returns 200 but no result
# (bummer).
return 'null'
except Exception, err:
sys.stderr.write('MathML Cloud ERROR: %sn' % str(err))
sys.exit(2)
def asciilatex2mathml_cloud(equation, iformat):
""" Get MathML from MathMLCloud converter """
# resp = requests.post('https://api.mathmlcloud.org/equation',{'math': equation,'mathType':'TeX','mml':'true', 'svg':'true', 'png':'true', 'description':'true'})
if iformat == 'asciimath':
mathtype = 'AsciiMath'
else: # LaTeX
mathtype = 'TeX'
if iformat == 'mathml':
sys.stderr.write('ERROR: unexpected mathml usage. Should not happen')
sys.exit(3)
# try x times to get MathML cloud result:
for i in range(1, 5):
# print "Attempt calling MathML Cloud Server: {}".format(i)
mathml = _call_mathml_cloud(equation, mathtype)
if mathml != 'null':
break
if mathml == 'null':
sys.stderr.write('ERROR: MathML Cloud Server does not give a result.')
sys.exit(2)
return mathml
@click.command()
@click.option('--equation', prompt='Equation', help='Equation to convert')
@click.option('--iformat', type=click.Choice(input_formats), prompt='Input Format', help='Equation input format')
@click.option('--oformat', type=click.Choice(output_formats), prompt='Output Format', help='Equation output format')
def convert(equation, iformat, oformat):
print
if (iformat == oformat):
sys.stderr.write(
'Nothing to convert here. Input and output format are the same!')
sys.exit(1)
result = 'Error: No result. Should not happen.'
if (oformat == 'mathml'):
result = asciilatex2mathml_cloud(equation, iformat)
else: # LaTeX
if (iformat == 'mathml'):
result = mathml2latex_yarosh(equation)
else: # AsciiMath
mathml = asciilatex2mathml_cloud(equation, iformat)
result = mathml2latex_yarosh(mathml)
click.echo(result)
if __name__ == '__main__':
convert()