Show More
@@ -0,0 +1,88 b'' | |||||
|
1 | # dollarmath.py by Akihiro Uchida *public domain* | |||
|
2 | # the original one is written by Paul Kienzle | |||
|
3 | # and published as public domain in [sphinx-dev]: $math$ extension | |||
|
4 | r""" | |||
|
5 | Allow $math$ markup in text and docstrings, ignoring \$. | |||
|
6 | ||||
|
7 | The $math$ markup should be separated from the surrounding text by spaces. | |||
|
8 | To embed markup within a word, place backslash-space before and after. | |||
|
9 | For convenience, the final $ can be followed by punctuation | |||
|
10 | (period, comma or semicolon). | |||
|
11 | """ | |||
|
12 | ||||
|
13 | import re | |||
|
14 | ||||
|
15 | dollar_pat = r"(?:^|(?<=\s))[$]([^\n]*?)(?<![\\])[$](?:$|(?=\s|[.,;\\]))" | |||
|
16 | _dollar = re.compile(dollar_pat) | |||
|
17 | _notdollar = re.compile(r"\\[$]") | |||
|
18 | ||||
|
19 | def replace_dollar(content): | |||
|
20 | content = _dollar.sub(r":math:`\1`", content) | |||
|
21 | content = _notdollar.sub("$", content) | |||
|
22 | return content | |||
|
23 | ||||
|
24 | def rewrite_rst(app, docname, source): | |||
|
25 | source[0] = replace_dollar(source[0]) | |||
|
26 | ||||
|
27 | def rewrite_autodoc(app, what, name, obj, options, lines): | |||
|
28 | lines[:] = [replace_dollar(L) for L in lines] | |||
|
29 | ||||
|
30 | def setup(app): | |||
|
31 | app.connect('source-read', rewrite_rst) | |||
|
32 | if 'autodoc-process-docstring' in app._events: | |||
|
33 | app.connect('autodoc-process-docstring', rewrite_autodoc) | |||
|
34 | ||||
|
35 | def test_expr(expr, expect): | |||
|
36 | result = replace_dollar(expr) | |||
|
37 | print 'A math expression: %s' % expr | |||
|
38 | print 'A expected output: %s' % expect | |||
|
39 | if result == expect: | |||
|
40 | print 'OK: A result match expected one' | |||
|
41 | else: | |||
|
42 | print 'NG: A result %s does not match expected one!' % result | |||
|
43 | ||||
|
44 | def test_dollar(): | |||
|
45 | samples = { | |||
|
46 | u"no dollar": u"no dollar", | |||
|
47 | u"$only$": u":math:`only`", | |||
|
48 | u"$first$ is good": u":math:`first` is good", | |||
|
49 | u"so is $last$": u"so is :math:`last`", | |||
|
50 | u"and $mid$ too": u"and :math:`mid` too", | |||
|
51 | u"$first$, $mid$, $last$": u":math:`first`, :math:`mid`, :math:`last`", | |||
|
52 | u"dollar\$ escape": u"dollar$ escape", | |||
|
53 | u"dollar \$escape\$ too": u"dollar $escape$ too", | |||
|
54 | u"emb\ $ed$\ ed": u"emb\ :math:`ed`\ ed", | |||
|
55 | u"$first$a": u"$first$a", | |||
|
56 | u"a$last$": u"a$last$", | |||
|
57 | u"a $mid$dle a": u"a $mid$dle a", | |||
|
58 | } | |||
|
59 | for expr, expect in samples.items(): | |||
|
60 | test_expr(expr, expect) | |||
|
61 | ||||
|
62 | if __name__ == "__main__": | |||
|
63 | import sys, locale, codecs | |||
|
64 | encoding = locale.getpreferredencoding() | |||
|
65 | sys.stdout = codecs.getwriter(encoding)(sys.stdout) | |||
|
66 | sys.stdin = codecs.getreader(encoding)(sys.stdin) | |||
|
67 | ||||
|
68 | import optparse | |||
|
69 | parser = optparse.OptionParser(usage='usage: %prog [options]') | |||
|
70 | parser.add_option("-i", "--input", dest="expr", type="string", | |||
|
71 | help="input $math$ expression to test") | |||
|
72 | parser.add_option("-o", "--output", dest="expect", type="string", | |||
|
73 | help="output result you expect") | |||
|
74 | ||||
|
75 | opts, args = parser.parse_args() | |||
|
76 | if opts.expr: | |||
|
77 | expression = unicode(opts.expr, encoding) | |||
|
78 | if opts.expect: | |||
|
79 | expected = unicode(opts.expect, encoding) | |||
|
80 | test_expr(expression, expected) | |||
|
81 | else: | |||
|
82 | print replace_dollar(expression) | |||
|
83 | else: | |||
|
84 | if opts.expect: | |||
|
85 | parser.print_help() | |||
|
86 | parser.error("output option requires input expression") | |||
|
87 | else: | |||
|
88 | test_dollar() |
General Comments 0
You need to be logged in to leave comments.
Login now