source: sasview/docs/sphinx-docs/source/_extensions/dollarmath.py @ 1a5d5f2

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 1a5d5f2 was eddf6af5, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

interpret $ expressions as latex in the docs

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[eddf6af5]1# This program is public domain
2# Author: Paul Kienzle
3r"""
4Allow $math$ markup in text and docstrings, ignoring \$.
5
6The $math$ markup should be separated from the surrounding text by spaces.  To
7embed markup within a word, place backslash-space before and after.  For
8convenience, the final $ can be followed by punctuation (period, comma or
9semicolon).
10"""
11
12import re
13_dollar = re.compile(r"(?:^|(?<=\s))[$]([^\n]*?)(?<![\\])[$](?:$|(?=\s|[.,;:?\\()]))")
14_notdollar = re.compile(r"\\[$]")
15
16def replace_dollar(content):
17    content = _dollar.sub(r":math:`\1`",content)
18    content = _notdollar.sub("$", content)
19    return content
20
21def rewrite_rst(app, docname, source):
22    source[0] = replace_dollar(source[0])
23
24def rewrite_autodoc(app, what, name, obj, options, lines):
25    lines[:] = [replace_dollar(L) for L in lines]
26
27def setup(app):
28    app.connect('source-read', rewrite_rst)
29    app.connect('autodoc-process-docstring', rewrite_autodoc)
30
31
32def test_dollar():
33    assert replace_dollar(u"no dollar")==u"no dollar"
34    assert replace_dollar(u"$only$")==u":math:`only`"
35    assert replace_dollar(u"$first$ is good")==u":math:`first` is good"
36    assert replace_dollar(u"so is $last$")==u"so is :math:`last`"
37    assert replace_dollar(u"and $mid$ too")==u"and :math:`mid` too"
38    assert replace_dollar(u"$first$, $mid$, $last$")==u":math:`first`, :math:`mid`, :math:`last`"
39    assert replace_dollar(u"dollar\$ escape")==u"dollar$ escape"
40    assert replace_dollar(u"dollar \$escape\$ too")==u"dollar $escape$ too"
41    assert replace_dollar(u"emb\ $ed$\ ed")==u"emb\ :math:`ed`\ ed"
42    assert replace_dollar(u"$first$a")==u"$first$a"
43    assert replace_dollar(u"a$last$")==u"a$last$"
44    assert replace_dollar(u"a $mid$dle a")==u"a $mid$dle a"
45
46if __name__ == "__main__":
47    test_dollar()
Note: See TracBrowser for help on using the repository browser.