[19dcb933] | 1 | # This program is public domain |
---|
| 2 | # Author: Paul Kienzle |
---|
| 3 | r""" |
---|
| 4 | Allow $math$ markup in text and docstrings, ignoring \$. |
---|
| 5 | |
---|
| 6 | The $math$ markup should be separated from the surrounding text by spaces. To |
---|
| 7 | embed markup within a word, place backslash-space before and after. For |
---|
| 8 | convenience, the final $ can be followed by punctuation (period, comma or |
---|
| 9 | semicolon). |
---|
| 10 | """ |
---|
| 11 | |
---|
| 12 | import re |
---|
| 13 | |
---|
[dd4d95d] | 14 | _dollar = re.compile(r"(?:^|(?<=\s|[-(]))[$]([^\n]*?)(?<![\\])[$](?:$|(?=\s|[-.,;:?\\)]))") |
---|
[19dcb933] | 15 | _notdollar = re.compile(r"\\[$]") |
---|
| 16 | |
---|
| 17 | def replace_dollar(content): |
---|
| 18 | original = content |
---|
| 19 | content = _dollar.sub(r":math:`\1`",content) |
---|
| 20 | content = _notdollar.sub("$", content) |
---|
| 21 | #if '$' in content: |
---|
| 22 | # import sys |
---|
| 23 | # sys.stdout.write("\n========> not converted\n") |
---|
| 24 | # sys.stdout.write(content) |
---|
| 25 | # sys.stdout.write("\n") |
---|
| 26 | #elif '$' in original: |
---|
| 27 | # import sys |
---|
| 28 | # sys.stdout.write("\n========> converted\n") |
---|
| 29 | # sys.stdout.write(content) |
---|
| 30 | # sys.stdout.write("\n") |
---|
| 31 | return content |
---|
| 32 | |
---|
| 33 | def rewrite_rst(app, docname, source): |
---|
| 34 | source[0] = replace_dollar(source[0]) |
---|
| 35 | |
---|
| 36 | def rewrite_autodoc(app, what, name, obj, options, lines): |
---|
| 37 | lines[:] = [replace_dollar(L) for L in lines] |
---|
| 38 | |
---|
| 39 | def setup(app): |
---|
| 40 | app.connect('source-read', rewrite_rst) |
---|
| 41 | app.connect('autodoc-process-docstring', rewrite_autodoc) |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | def test_dollar(): |
---|
| 45 | assert replace_dollar(u"no dollar")==u"no dollar" |
---|
| 46 | assert replace_dollar(u"$only$")==u":math:`only`" |
---|
| 47 | assert replace_dollar(u"$first$ is good")==u":math:`first` is good" |
---|
| 48 | assert replace_dollar(u"so is $last$")==u"so is :math:`last`" |
---|
| 49 | assert replace_dollar(u"and $mid$ too")==u"and :math:`mid` too" |
---|
| 50 | assert replace_dollar(u"$first$, $mid$, $last$")==u":math:`first`, :math:`mid`, :math:`last`" |
---|
| 51 | assert replace_dollar(u"dollar\$ escape")==u"dollar$ escape" |
---|
| 52 | assert replace_dollar(u"dollar \$escape\$ too")==u"dollar $escape$ too" |
---|
[103ea45] | 53 | assert replace_dollar(u"spaces $in the$ math")==u"spaces :math:`in the` math" |
---|
[19dcb933] | 54 | assert replace_dollar(u"emb\ $ed$\ ed")==u"emb\ :math:`ed`\ ed" |
---|
| 55 | assert replace_dollar(u"$first$a")==u"$first$a" |
---|
| 56 | assert replace_dollar(u"a$last$")==u"a$last$" |
---|
[103ea45] | 57 | assert replace_dollar(u"$37")==u"$37" |
---|
| 58 | assert replace_dollar(u"($37)")==u"($37)" |
---|
| 59 | assert replace_dollar(u"$37 - $43")==u"$37 - $43" |
---|
| 60 | assert replace_dollar(u"($37, $38)")==u"($37, $38)" |
---|
[19dcb933] | 61 | assert replace_dollar(u"a $mid$dle a")==u"a $mid$dle a" |
---|
[103ea45] | 62 | assert replace_dollar(u"a ($in parens$) a")==u"a (:math:`in parens`) a" |
---|
| 63 | assert replace_dollar(u"a (again $in parens$) a")==u"a (again :math:`in parens`) a" |
---|
[19dcb933] | 64 | |
---|
| 65 | if __name__ == "__main__": |
---|
| 66 | test_dollar() |
---|