""" ReplaceInlineMaxima.py v. 0.2.3 Replace Axiom command \\begin{maxima} \\end{maxima} blocks with LaTeX """ import re from string import join,replace n=1 def replaceInlineMaxima(body): from maximaWrapper import renderMaxima errorMessage = """\n
Some or all expressions may not have rendered properly, because Maxima returned the following error:
%s
""" reConsts = re.MULTILINE+re.DOTALL maximaInPattern = re.compile( r'[ \t]*(?.*?black\}(.*?)' #1 LaTeX r'\\mbox{\\tt\\red\(\\mathrm{\\%(i\d+)}\) \\black}(.*?)|' #1 #2 Input r'\\mbox{\\tt\\red\(\\mathrm{\\%(o\d+)}\) \\black}(.*?)|' #3 #4 Output r'stdin:((?:.(?!))*.)', #5 Other stuff reConsts) def htmlMarkup(code): newcode = code newcode = re.compile(r'\n\s*\n+',reConsts).sub(r'\n',newcode) return re.sub(r'([&<>\$\*_\'\\\"])', lambda x: { '&':'&', '<':'<', '>':'>', '$':'$', '*':'*', '#':'#', '\\':'\', '[':'![', '\'':''', '"':'"', '_':'_' }[x.group(1)],newcode) def linebreak(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): return '
maxima
(%%%s)\\begin{equation*}\n%s\n\\end{equation*}\n
\n'%\ (N.group(1),N.group(2).strip()) if N.group(3): return '
(%%%s)\\begin{equation}\n%s\n\\end{equation}\n
'%\ (N.group(3),N.group(4).strip()) if N.group(5): return '
maxima
%s
\n'%htmlMarkup(N.group(5)) return 'Pattern Error' def formatOutput(x): global n newCode = maximaOutPattern.sub(translateOutput,x) return newCode maximaCodeIn = maximaInPattern.findall(body) (maximaCodeOut,errors) = renderMaxima(maximaCodeIn) if not errors: newCodeList = map(formatOutput,maximaCodeOut) #newCodeList = maximaCodeOut body = maximaInPattern.sub(lambda x:len(newCodeList) and newCodeList.pop(0) or 'Maxima output parse error!\n',body) else: body = "
" + body + "
" + errorMessage %(errors) return body