On Fri, Sep 4, 2009 at 2:28 PM, John H Palmieri<jhpalmier...@gmail.com> wrote:
>
> A patch which does a basic version of this (changing $blah$ to `blah`,
> but allowing no custom delimiters, no parsing of $$blah$$ or \[ blah
> \]) is here:
>
>  <http://trac.sagemath.org/sage_trac/ticket/6892>

I tried your patch (I made a sphinx extension out of it), but it
didn't work for me --- the backsubstitution to docstringlines failed
in the extension (maybe sphinx is inconsistent here), but this can be
fixed easily by just doing

    docstringlines[:] = [s]

worse problem is that I want to change it to math:`sd`, not just `sdf`
(I have not figured it out how to do that using your indices
approach). In any case, I rewrote it so that it works for me,
attached. To install it, just add to your conf.py:

extensions = ['sphinx.ext.pngmath', "math_dollar"]

possibly adding the patch to math_dollar.py by:

sys.path.append(os.path.abspath('exts'))


So that's a very convenient solution, as I can ship it with my sphinx
notes and it will just work. Later on, after I gain more experience,
I'll ask on the sphinx list, if they want to ship this with sphinx
itself.

Ondrej

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

import re

def process_dollars(app, docname, source):
    r"""
    Replace dollar signs with backticks.

    More precisely, do a regular expression search.  Replace a plain
    dollar sign ($) by a backtick (`).  Replace an escaped dollar sign
    (\$) by a dollar sign ($).  Don't change a dollar sign preceded or
    followed by a backtick (`$ or $`), because of strings like
    "``$HOME``".  Don't make any changes on lines starting with
    spaces, because those are indented and hence part of a block of
    code or examples.

    This also doesn't replaces dollar signs enclosed in curly braces,
    to avoid nested math environments, such as ::

      $f(n) = 0 \text{ if $n$ is prime}$

    Thus the above line would get changed to

      `f(n) = 0 \text{ if $n$ is prime}`
    """
    s = "\n".join(source)
    if s.find("$") == -1:
        return
    # This searches for "$blah$" inside a pair of curly braces --
    # don't change these, since they're probably coming from a nested
    # math environment.  So for each match, we replace it with a temporary
    # string, and later on we substitute the original back.
    global _data
    _data = {}
    def repl(matchobj):
        global _data
        s = matchobj.group(0)
        t = "___XXX_REPL_%d___" % len(_data)
        _data[t] = s
        return t
    s = re.sub(r"({[^{}$]*\$[^{}$]*\$[^{}$]*})", repl, s)
    # matches $...$
    dollars = re.compile(r"(?<!\$)(?<!\\)\$([^\$]+?)\$")
    # regular expression for \$
    slashdollar = re.compile(r"\\\$")
    s = dollars.sub(r":math:`\1`", s)
    s = slashdollar.sub(r"$", s)
    # change the original {...} things in:
    for r in _data:
        s = s.replace(r, _data[r])
    # now save results in "source"
    source[:] = [s]

def setup(app):
    app.connect("source-read", process_dollars)

Reply via email to