# HG changeset patch
diff -r 73be77c19ca7 -r de5da281bc2e sagenb/notebook/mathjax.py
 - # -*- coding: utf-8 -* """ jsMath support for the notebook Provides code to parse HTML, changing \$'s and \$\$'s to tags to allow jsMath to process them. AUTHORS: - William Stein (?) -- initial revision - Tim Dumol (Oct 6, 2009) -- Added HTMLMathParser. Made math_parser skip * Replace all $$text$$'s by * Replace all \$'s by $'s. Note that in the above two cases nothing is done if the$ is preceeded by a backslash. * Replace all $text$'s by \end{verbatim} EXAMPLES: sage: sage.misc.html.math_parse('This is $2+2$.') 'This is .' sage: sage.misc.html.math_parse('This is $$2+2$$.') 'This is .' sage: sage.misc.html.math_parse('This is \$2+2\$.') 'This is .' sage: sage.misc.html.math_parse(r'This is $2+2$.') 'This is .' TESTS: sage: sage.misc.html.math_parse(r'This \$\$is $2+2$.') 'This $$is .' """ # first replace \$and \$ by and , respectively. while True: i = s.find('\\[') if i == -1: break else: s = s[:i] + '' else: s = s[:j] + '' + s[j+2:] # Below t always has the "parsed so far" version of s, and s is # just the part of the original input s that hasn't been parsed. t = '' while True: i = s.find('') if i == -1: # No dollar signs -- definitely done. return t + s elif i > 0 and s[i-1] == '\\': # A dollar sign with a backslash right before it, so # we ignore it by sticking it in the parsed string t # and skip to the next iteration. t += s[:i-1] + '' s = s[i+1:] continue elif i+1 < len(s) and s[i+1] == '': # Found a math environment. Double dollar sign so display mode. disp = '; mode=display' else: # Found math environment. Single dollar sign so default mode. disp = '' # Now find the matching  sign and form the html string. if len(disp) > 0: j = s[i+2:].find('$$') if j == -1: j = len(s) s += '' else: j += i + 2 txt = s[i+2:j] else: j = s[i+2:].find('$') if j == -1: j = len(s) s += '$' else: j += i + 2 txt = s[i+1:j] t += s[:i] + ''%(disp, ' '.join(txt.splitlines())) s = s[j+1:] if len(disp) > 0: s = s[1:] return t