"""
ReplaceInlineAxiom.py v. 0.2.3
Replace Axiom command \\begin{axiom} \\end{axiom} blocks with LaTeX
"""
import re
from string import join,replace
n=1
def replaceInlineAxiom(body):
from axiomWrapper import renderAxiom
errorMessage = """\n
Some or all expressions may not have rendered properly,
because Axiom returned the following error:
%s
"""
reConsts = re.MULTILINE+re.DOTALL
axiomInPattern = re.compile(
r'[ \t]*(?\$\*_\'\\])',
# r'([&<>\$\*\\_])',
lambda x: { '&':'&',
'<':'<',
'>':'>',
'$':'$',
'*':'*',
'#':'#',
'\\':'\',
'[':'![',
'\'':''',
'_':'_'
}[x.group(1)],newcode)
def fixLoadingInTex(N):
if N.group(4): # Load WikiNames
requires[N.group(5)].append('['+N.group(6)+']')
return '' # We list them later
else:
return N.group(1) or N.group(2) or N.group(3) or N.group(7)
def fixEmbedded(x):
return axiomOutPattern.sub(fixLoadingInTex,x)
def linebreak(x):
# XXX can't build texbreaker yet
return x
#from texbreaker import texbreak, cvar # Robert Sutor's C program
#texbreak(replace(x,'\n',' '))
#return cvar.bufout
def translateOutput(N):
global n
if N.group(1): # Equation
return '\\begin{equation}%s\\end{equation}\n'% \
linebreak(re.sub(r'\\leqno\(.*?\)',r'',fixEmbedded(N.group(1))))
if N.group(2): # Type
return '%s
\n'%N.group(2)
if N.group(3): # Autoload messages
return '' # Ignore them
if N.group(4): # Load WikiNames
requires[N.group(5)].append({'category':'[Cat]:',
'domain':'[Dom]:',
'package':'[Pac]:'}[N.group(5)]+
N.group(6))
return '' # We list them later
if N.group(7):
if re.match(r'^\s*Compiling',N.group(7)): # compiler output
n=n+1
return '\n'%(n,htmlMarkup(N.group(7)))
else: # command
return '\n'%htmlMarkup(N.group(7))
return 'Pattern Error'
def formatOutput(x):
global n
requires['package'] = []
requires['domain'] = []
requires['category'] = []
if re.match(r'^\s*\n(.*?)\n(.*)$',x,reConsts):
m = re.match(r'^\s*\n(.*?)\n(.*)$',x,reConsts)
n=n+1
newCode = '''
'''%(htmlMarkup(m.group(1)),n,htmlMarkup(m.group(2)))
elif re.match(r'^\s*\n(.*?)\n(.*)$',x,reConsts):
m = re.match(r'^\s*\n(.*?)\n(.*)$',x,reConsts)
n=n+1
newCode = '''
'''%(htmlMarkup(m.group(1)),n,htmlMarkup(m.group(2)))
elif re.match(r'^\s*\n(.*?)\n(.*)$',x,reConsts):
m = re.match(r'^\s*\n(.*?)\n(.*)$',x,reConsts)
n=n+1
newCode = '''
'''%(htmlMarkup(m.group(1)),n,htmlMarkup(m.group(2)))
elif re.match(r'^\s*\n(.*?)\n(.*)$',x,reConsts):
m = re.match(r'^\s*\n(.*?)\n(.*)$',x,reConsts)
n=n+1
newCode = '''
'''%(htmlMarkup(m.group(1)),n,htmlMarkup(m.group(2)))
else:
newCode = axiomOutPattern.sub(translateOutput,x)
for module,names in requires.iteritems():
if names:
names.sort()
newCode = newCode + '\n%s: %s
'%(module,join(names))
return newCode
axiomCodeIn = map(lambda x: x[0] or x[1] or x[2] or x[3] or x[4] or x[5],
axiomInPattern.findall(body))
(axiomCodeOut,errors) = renderAxiom(axiomCodeIn)
if not errors:
newCodeList = map(formatOutput,axiomCodeOut)
body = axiomInPattern.sub(lambda x:len(newCodeList) and newCodeList.pop(0) or 'Axiom output parse error!\n',body)
else:
body = "" + body + "
" + errorMessage %(errors)
return body