File: dollarmath.py

package info (click to toggle)
python-periodictable 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,068 kB
  • sloc: python: 13,338; makefile: 103; sh: 92; javascript: 7
file content (70 lines) | stat: -rw-r--r-- 2,698 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# This program is public domain
# Author: Paul Kienzle
r"""
Allow $math$ markup in text and docstrings, ignoring \$.

The $math$ markup should be separated from the surrounding text by spaces.
To embed markup within a word, place backslash-space before and after. For
convenience, the final $ can be followed by punctuation (period, comma or
semicolon).
"""

import re

_DOLLAR = re.compile(
    r"(?:^|(?<=\s|[-(]))[$]([^\n]*?)(?<![\\])[$](?:$|(?=\s|[-.,;:?\\)]))")
_NOTDOLLAR = re.compile(r"\\[$]")

def replace_dollar(content):
    #original = content
    content = _DOLLAR.sub(r":math:`\1`", content)
    content = _NOTDOLLAR.sub("$", content)
    #if '$' in content:
    #    import sys
    #    sys.stdout.write("\n========> not converted\n")
    #    sys.stdout.write(content)
    #    sys.stdout.write("\n")
    #elif '$' in original:
    #    import sys
    #    sys.stdout.write("\n========> converted\n")
    #    sys.stdout.write(content)
    #    sys.stdout.write("\n")
    return content

def rewrite_rst(app, docname, source):
    source[0] = replace_dollar(source[0])

def rewrite_autodoc(app, what, name, obj, options, lines):
    lines[:] = [replace_dollar(L) for L in lines]

def setup(app):
    app.connect('source-read', rewrite_rst)
    app.connect('autodoc-process-docstring', rewrite_autodoc)


def test_dollar():
    assert replace_dollar(u"no dollar") == u"no dollar"
    assert replace_dollar(u"$only$") == u":math:`only`"
    assert replace_dollar(u"$first$ is good") == u":math:`first` is good"
    assert replace_dollar(u"so is $last$") == u"so is :math:`last`"
    assert replace_dollar(u"and $mid$ too") == u"and :math:`mid` too"
    assert (replace_dollar(u"$first$, $mid$, $last$")
            == u":math:`first`, :math:`mid`, :math:`last`")
    assert replace_dollar(u"dollar\\$ escape") == u"dollar$ escape"
    assert replace_dollar(u"dollar \\$escape\\$ too") == u"dollar $escape$ too"
    assert (replace_dollar(u"spaces $in the$ math")
            == u"spaces :math:`in the` math")
    assert replace_dollar(u"emb\\ $ed$\\ ed") == u"emb\\ :math:`ed`\\ ed"
    assert replace_dollar(u"$first$a") == u"$first$a"
    assert replace_dollar(u"a$last$") == u"a$last$"
    assert replace_dollar(u"$37") == u"$37"
    assert replace_dollar(u"($37)") == u"($37)"
    assert replace_dollar(u"$37 - $43") == u"$37 - $43"
    assert replace_dollar(u"($37, $38)") == u"($37, $38)"
    assert replace_dollar(u"a $mid$dle a") == u"a $mid$dle a"
    assert replace_dollar(u"a ($in parens$) a") == u"a (:math:`in parens`) a"
    assert (replace_dollar(u"a (again $in parens$) a")
            == u"a (again :math:`in parens`) a")

if __name__ == "__main__":
    test_dollar()