-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcnxmlplus2latex
executable file
·150 lines (128 loc) · 5.32 KB
/
cnxmlplus2latex
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
import sys
import os
import argparse
import inspect
from XmlValidator import XmlValidator
from XmlValidator import utils
SPEC_PATH = os.path.join(os.path.dirname(inspect.getfile(XmlValidator)))
MY_PATH = os.path.realpath(os.path.dirname(__file__))
# Parse command line arguments
argumentParser = argparse.ArgumentParser(description='Convert a CNXML+ document to a LaTeX document.')
argumentParser.add_argument(
'--spec', dest='specFilename',
default="spec.xml",
help='Filename of the XML specification document.')
argumentParser.add_argument(
'--audience', dest='audience',
default="learner",
help='Target audience of the transform ( learner | teacher | correct ).')
argumentParser.add_argument(
'-o', dest='outputFilename',
help='Write output to given filename rather than stdout.')
argumentParser.add_argument(
'filename', nargs='+',
help='One or more filenames to process.')
commandlineArguments = argumentParser.parse_args()
if commandlineArguments.outputFilename is None:
outputFile = sys.stdout
else:
outputFile = open(commandlineArguments.outputFilename, 'wt')
validator = XmlValidator(open(os.path.join(SPEC_PATH, commandlineArguments.specFilename),'rt').read())
mathml_transform = utils.MmlTex()
conversionFunctions = {} # Cache
def cache_conversion_function(iSpec):
global conversionFunctions, validator, utils, commandlineArguments
if isinstance(iSpec, basestring):
# xpath given rather than node
specEntry = None
for child in validator.spec:
xpathNode = child.find('xpath')
if xpathNode is None:
continue
if xpathNode.text == iSpec:
specEntry = child
break
else:
specEntry = iSpec
conversionFunction = conversionFunctions.get(specEntry)
if conversionFunction is None:
# Cache conversion function
conversionFunctionNodes = specEntry.xpath('.//conversion-callback[contains(@name, "latex") and contains(@name, "%s")]'%commandlineArguments.audience)
if len(conversionFunctionNodes) == 0:
utils.warning_message('No conversion entry for ' + specEntry.find('xpath').text)
conversionFunctionSource = 'conversionFunction = lambda self: None'
else:
if len(conversionFunctionNodes) != 1:
utils.error_message('More than 1 conversion entry for ' + specEntry.find('xpath').text)
conversionFunctionSource = conversionFunctionNodes[0].text.strip()
if conversionFunctionSource == '':
conversionFunctionSource = 'conversionFunction = lambda self: None'
else:
conversionFunctionSource = 'def conversionFunction(self):\n' + '\n'.join(['\t' + line for line in conversionFunctionSource.split('\n')]) + '\n'
from lxml import etree
from XmlValidator import utils
localVars = {
'etree': etree,
'utils': utils,
'convert_using': convert_using,
'warning_message': utils.warning_message,
'error_message': utils.error_message,
'mathml_transform': mathml_transform,
'escape_latex': utils.escape_latex,
'latex_math_function_check': utils.latex_math_function_check,
'convert_image': convert_image,
}
exec(conversionFunctionSource, localVars)
conversionFunction = localVars['conversionFunction']
conversionFunctions[specEntry] = conversionFunction
return conversionFunction
def convert_using(iNode, iPath):
f = cache_conversion_function(iPath)
return f(iNode)
def convert_image(iSourceFilename, iDestinationFilename):
import subprocess
p = subprocess.Popen(['convert', iSourceFilename, iDestinationFilename])
p.wait()
def traverse(iNode, iValidator):
global conversionFunctions
children = iNode.getchildren()
for child in children:
traverse(child, iValidator)
# Get associated conversion function
specEntry = iValidator.documentSpecEntries.get(iNode)
if specEntry is None:
utils.error_message('Unhandled element at ' + utils.get_full_dom_path(iNode, iValidator.spec))
conversionFunction = cache_conversion_function(specEntry)
parent = iNode.getparent()
try:
converted = conversionFunction(iNode)
except TypeError:
from lxml import etree
print "Problem converting node %s on line %s "%(iNode, iNode.sourceline)
sys.exit(1)
if isinstance(converted, basestring):
if parent is None:
return converted
else:
from lxml import etree
dummyNode = etree.Element('dummy')
dummyNode.text = converted
utils.etree_replace_with_node_list(parent, iNode, dummyNode)
elif converted is not None:
if parent is None:
return converted
else:
utils.etree_replace_with_node_list(iNode.getparent(), iNode, converted)
for filename in commandlineArguments.filename:
if filename == '-':
fp = sys.stdin
else:
fp = open(filename,'rt')
validator.validate(
fp.read(),
iCleanUp=True)
document = validator.dom
#print etree.tostring(traverse(document, spec), encoding="utf-8", xml_declaration=True)
print traverse(document, validator).encode('utf-8')
outputFile.flush()