Ticket #2860: sage-2860.patch

File sage-2860.patch, 2.4 KB (added by was, 12 years ago)
  • sage/misc/html.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1210492170 25200
    # Node ID 64bab8c54fd187e3860c10ba5629f6b3235cba98
    # Parent  19807feec3063d1c6193d858d64ad877611d033b
    trac #2860 -- easy-to-fix bug in html.py
    
    Nobody has a test case to reproduce the claimed problem.  So I read
    the code, vastly improved its documentation, and did make a change that
    logically must fix exactly the reported bug.
    
    diff -r 19807feec306 -r 64bab8c54fd1 sage/misc/html.py
    a b from sage.misc.sage_eval import sage_eva 
    1313
    1414def math_parse(s):
    1515    r"""
     16    Turn the HTML-ish string s that can have $$ and $'s in it into
     17    pure HTML.  See below for a precise definition of what this means.
     18   
     19    INPUT:
     20        s -- a string
     21    OUTPUT:
     22        a string.
     23       
    1624    Do the following:
    1725    \begin{verbatim}
    1826       * Replace all $ text $'s by
    def math_parse(s): 
    2331         the above two cases nothing is done if the $
    2432         is preceeded by a backslash.
    2533    \end{verbatim}
     34
     35    EXAMPLES:
     36        sage: sage.misc.html.math_parse('This is $2+2$.')
     37        'This is <span class="math">2+2</span>.'
     38        sage: sage.misc.html.math_parse('This is $$2+2$$.')
     39        'This is <div class="math">2+2</div>.'
     40
     41    TESTS:
     42        sage: sage.misc.html.math_parse(r'This \$\$is $2+2$.')
     43        'This $$is <span class="math">2+2</span>.'   
    2644    """
     45    # Below t always has the "parsed so far" version of s, and s is
     46    # just the part of the original input s that hasn't been parsed.
    2747    t = ''
    2848    while True:
    2949        i = s.find('$')
    3050        if i == -1:
     51            # No dollar signs -- definitely done.
    3152            return t + s
    3253        elif i > 0 and s[i-1] == '\\':
     54            # A dollar sign with a backslash right before it, so
     55            # we ignore it by sticking it in the parsed string t
     56            # and skip to the next iteration.
    3357            t += s[:i-1] + '$'
    3458            s = s[i+1:]
     59            continue
    3560        elif i+1 < len(s) and s[i+1] == '$':
     61            # Found a math environment. Double dollar sign so div mode.
    3662            typ = 'div'
    3763        else:
     64            # Found math environment. Single dollar sign so span mode.
    3865            typ = 'span'
     66
     67        # Now find the matching $ sign and form the span or div.
    3968        j = s[i+2:].find('$')
    4069        if j == -1:
    4170            j = len(s)