| Download
Process Maxima output using Markdown.
Project: Many Interacting Worlds
Path: maxima_md.py
Views: 113# Markdown mode for Maxima1def maxima_md(s):2import re3#print s4# insert calls to tex around expressions separated by $ or ; or as a single expression per line5s=re.compile(r'((?:(?![\n\$;]).)+)([\$;\n]?)',re.M+re.S).sub(r'tex(\1,false)\2',s,count=0)6#print s7t = maxima.eval(s,split_lines=true, reformat=false)8# cleanup generated LaTeX for MathJax9t=re.compile(r'\$\$').sub('\n$$\n',t,count=0)10t=re.compile(r'\\\\').sub(r'\\',t,count=0)11t=re.compile(r'\\cos').sub(r'\\mathrm{cos}',t,count=0)12t=re.compile(r'\\sin').sub(r'\\mathrm{sin}',t,count=0)13t=re.compile(r'\\_').sub(r'_',t,count=0)14t=re.compile(r'\\begin{verbatim}').sub(r'$$',t,count=0)15t=re.compile(r'\\end{verbatim}').sub(r'$$',t,count=0)16# strip non-escaped string quotes17t=re.compile(r'[^\\]"([^"]*?)').sub(r'\1',t,count=0)18# remove escape chars before quotes19t=re.compile(r'\\"').sub('"',t,count=0)20# Maybe this just text output? Indent as a preformatted code block21t=re.compile(r'\n\s*([^\$].*)').sub(r' \1',t,count=0)22# hide input and output numbers (Sage makes it impossible to use them anyway.)23t=re.compile(r'\(\%o[0-9]+\)').sub(r'',t)24t=re.compile(r'\(\%i[0-9]+\) ').sub(r'',t)25#print "'",t,"'"26md(t, hide=False)2728