def convert_dollars(app, docname, source):
    print "-"
    print "so:"
    print app
    print docname
    print source
    print "-"

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

@@ -1,15 +1,26 @@
-def convert
+import re%0A%0Adef process
 _dol
@@ -55,96 +55,2294 @@
     
-print %22-%22%0A    print %22so:%22%0A    print app%0A    print docname%0A    print source%0A    print %22-%22
+r%22%22%22%0A    Replace dollar signs with backticks.%0A%0A    More precisely, do a regular expression search.  Replace a plain%0A    dollar sign ($) by a backtick (%60).  Replace an escaped dollar sign%0A    (%5C$) by a dollar sign ($).  Don't change a dollar sign preceded or%0A    followed by a backtick (%60$ or $%60), because of strings like%0A    %22%60%60$HOME%60%60%22.  Don't make any changes on lines starting with%0A    spaces, because those are indented and hence part of a block of%0A    code or examples.%0A%0A    This also doesn't replaces dollar signs enclosed in curly braces,%0A    to avoid nested math environments, such as ::%0A%0A      $f(n) = 0 %5Ctext%7B if $n$ is prime%7D$%0A%0A    Thus the above line would get changed to%0A%0A      %60f(n) = 0 %5Ctext%7B if $n$ is prime%7D%60%0A    %22%22%22%0A    s = %22%5Cn%22.join(source)%0A    if s.find(%22$%22) == -1:%0A        return%0A    # Indices will be a list of pairs of positions in s, to search between.%0A    # If the following search has no matches, then indices will be (0, len(s)).%0A    indices = %5B0%5D%0A    # This searches for %22$blah$%22 inside a pair of curly braces --%0A    # don't change these, since they're probably coming from a nested%0A    # math environment.  So for each match, search to the left of its%0A    # start and to the right of its end, but not in between.%0A    for m in re.finditer(r%22%7B%5B%5E%7B%7D$%5D*%5C$(%5B%5E%7B%7D$%5D*)%5C$%5B%5E%7B%7D$%5D*%7D%22, s):%0A        indices%5B-1%5D = (indices%5B-1%5D, m.start())%0A        indices.append(m.end())%0A    indices%5B-1%5D = (indices%5B-1%5D, len(s))%0A%0A    # regular expression for $ (not %5C$, %60$, $%60, and only on a line%0A    # with no leading whitespace).%0A    dollar = re.compile(r%22%22%22%5E # beginning of line%0A                            (%5B%5E%5Cs%5D # non whitespace%0A                            .*?)? # non-greedy match any non-newline characters%0A                            (?%3C!%60%7C%5C%5C)%5C$(?!%60) # $ with negative lookbehind and lookahead%0A                            %22%22%22, re.M %7C re.X)%0A    # regular expression for %5C$%0A    slashdollar = re.compile(r%22%5C%5C%5C$%22)%0A    for start, end in indices:%0A        while dollar.search(s, start, end):%0A            m = dollar.search(s, start, end)%0A            s = s%5B:m.end()-1%5D + %22%60%22 + s%5Bm.end():%5D%0A        while slashdollar.search(s, start, end):%0A            m = slashdollar.search(s, start, end)%0A            s = s%5B:m.start()%5D + %22$%22 + s%5Bm.end():%5D%0A    # now save results in %22source%22%0A    source%5B:%5D = %5Bs%5D
 %0A%0Ade
@@ -2390,15 +2390,15 @@
 d%22, 
-convert
+process
 _dol

Substitute dollars