File: rst2html.py

package info (click to toggle)
sasmodels 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,888 kB
  • sloc: python: 25,392; ansic: 7,377; makefile: 149; sh: 61
file content (277 lines) | stat: -rw-r--r-- 8,897 bytes parent folder | download | duplicates (2)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
r"""
Convert a restructured text document to html.

Inline math markup can uses the *math* directive, or it can use latex
style *\$expression\$*.  Math can be rendered using simple html and
unicode, or with mathjax.
"""

import re
from contextlib import contextmanager

# CRUFT: locale.getlocale() fails on some versions of OS X
# See https://bugs.python.org/issue18378
import locale
if hasattr(locale, '_parse_localename'):
    try:
        locale._parse_localename('UTF-8')
    except ValueError:
        _old_parse_localename = locale._parse_localename
        def _parse_localename(localename):
            code = locale.normalize(localename)
            if code == 'UTF-8':
                return None, code
            else:
                return _old_parse_localename(localename)
        locale._parse_localename = _parse_localename

from docutils.core import publish_parts
from docutils.writers.html4css1 import HTMLTranslator
from docutils.nodes import SkipNode

def rst2html(rst, part="whole", math_output="mathjax"):
    r"""
    Convert restructured text into simple html.

    Valid *math_output* formats for formulas include:
    - html
    - mathml
    - mathjax
    See `<http://docutils.sourceforge.net/docs/user/config.html#math-output>`_
    for details.

    The following *part* choices are available:
    - whole: the entire html document
    - html_body: document division with title and contents and footer
    - body: contents only

    There are other parts, but they don't make sense alone:

        subtitle, version, encoding, html_prolog, header, meta,
        html_title, title, stylesheet, html_subtitle, html_body,
        body, head, body_suffix, fragment, docinfo, html_head,
        head_prefix, body_prefix, footer, body_pre_docinfo, whole
    """
    # Ick! mathjax doesn't work properly with math-output, and the
    # others don't work properly with math_output!
    if math_output == "mathjax":
        # TODO: this is copied from docs/conf.py; there should be only one
        mathjax_path = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"
        settings = {"math_output": math_output + " " + mathjax_path}
    else:
        settings = {"math-output": math_output}

    # TODO: support stylesheets
    #html_root = "/full/path/to/_static/"
    #sheets = [html_root+s for s in ["basic.css","classic.css"]]
    #settings["embed_styesheet"] = True
    #settings["stylesheet_path"] = sheets

    # math2html and mathml do not support \frac12
    rst = replace_compact_fraction(rst)

    # mathml, html do not support \tfrac
    if math_output in ("mathml", "html"):
        rst = rst.replace(r'\tfrac', r'\frac')

    rst = replace_dollar(rst)
    with suppress_html_errors():
        parts = publish_parts(source=rst, writer_name='html',
                              settings_overrides=settings)
    return parts[part]

@contextmanager
def suppress_html_errors():
    r"""
    Context manager for keeping error reports out of the generated HTML.

    Within the context, system message nodes in the docutils parse tree
    will be ignored.  After the context, the usual behaviour will be restored.
    """
    visit_system_message = HTMLTranslator.visit_system_message
    HTMLTranslator.visit_system_message = _skip_node
    yield None
    HTMLTranslator.visit_system_message = visit_system_message

def _skip_node(self, node):
    raise SkipNode


_compact_fraction = re.compile(r"(\\[cdt]?frac)([0-9])([0-9])")
def replace_compact_fraction(content):
    r"""
    Convert \frac12 to \frac{1}{2} for broken latex parsers
    """
    return _compact_fraction.sub(r"\1{\2}{\3}", content)


_dollar = re.compile(r"(?:^|(?<=\s|[-(]))[$]([^\n]*?)(?<![\\])[$](?:$|(?=\s|[-.,;:?\\)]))")
_notdollar = re.compile(r"\\[$]")
def replace_dollar(content):
    r"""
    Convert dollar signs to inline math markup in rst.
    """
    content = _dollar.sub(r":math:`\1`", content)
    content = _notdollar.sub("$", content)
    return content


def test_dollar():
    """
    Test substitution of dollar signs with equivalent RST math markup
    """
    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"

def load_rst_as_html(filename):
    # type: (str) -> str
    """Load rst from file and convert to html"""
    from os.path import expanduser
    with open(expanduser(filename)) as fid:
        rst = fid.read()
    html = rst2html(rst)
    return html

def wxview(html, url="", size=(850, 540)):
    # type: (str, str, Tuple[int, int]) -> "wx.Frame"
    """View HTML in a wx dialog"""
    import wx
    from wx.html2 import WebView
    frame = wx.Frame(None, -1, size=size)
    view = WebView.New(frame)
    view.SetPage(html, url)
    frame.Show()
    return frame

def view_html_wxapp(html, url=""):
    # type: (str, str) -> None
    """HTML viewer app in wx"""
    import wx  # type: ignore
    app = wx.App()
    frame = wxview(html, url)
    app.MainLoop()

def view_url_wxapp(url):
    # type: (str) -> None
    """URL viewer app in wx"""
    import wx  # type: ignore
    from wx.html2 import WebView
    app = wx.App()
    frame = wx.Frame(None, -1, size=(850, 540))
    view = WebView.New(frame)
    view.LoadURL(url)
    frame.Show()
    app.MainLoop()

def qtview(html, url=""):
    # type: (str, str) -> "QWebView"
    """View HTML in a Qt dialog"""
    try:
        from PyQt5.QtWebKitWidgets import QWebView
        from PyQt5.QtCore import QUrl
    except ImportError:
        from PyQt4.QtWebKit import QWebView
        from PyQt4.QtCore import QUrl
    helpView = QWebView()
    helpView.setHtml(html, QUrl(url))
    helpView.show()
    return helpView

def view_html_qtapp(html, url=""):
    # type: (str, str) -> None
    """HTML viewer app in Qt"""
    import sys
    try:
        from PyQt5.QtWidgets import QApplication
    except ImportError:
        from PyQt4.QtGui import QApplication
    app = QApplication([])
    frame = qtview(html, url)
    sys.exit(app.exec_())

def view_url_qtapp(url):
    # type: (str) -> None
    """URL viewer app in Qt"""
    import sys
    try:
        from PyQt5.QtWidgets import QApplication
    except ImportError:
        from PyQt4.QtGui import QApplication
    app = QApplication([])
    try:
        from PyQt5.QtWebKitWidgets import QWebView
        from PyQt5.QtCore import QUrl
    except ImportError:
        from PyQt4.QtWebKit import QWebView
        from PyQt4.QtCore import QUrl
    frame = QWebView()
    frame.load(QUrl(url))
    frame.show()
    sys.exit(app.exec_())

# Set default html viewer
view_html = view_html_qtapp

def can_use_qt():
    # type: () -> bool
    """
    Return True if QWebView exists.

    Checks first in PyQt5 then in PyQt4
    """
    try:
        from PyQt5.QtWebKitWidgets import QWebView
        return True
    except ImportError:
        try:
            from PyQt4.QtWebKit import QWebView
            return True
        except ImportError:
            return False

def view_help(filename, qt=False):
    # type: (str, bool) -> None
    """View rst or html file.  If *qt* use q viewer, otherwise use wx."""
    import os

    if qt:
        qt = can_use_qt()

    url = "file:///"+os.path.abspath(filename).replace("\\", "/")
    if filename.endswith('.rst'):
        html = load_rst_as_html(filename)
        if qt:
            view_html_qtapp(html, url)
        else:
            view_html_wxapp(html, url)
    else:
        if qt:
            view_url_qtapp(url)
        else:
            view_url_wxapp(url)

def main():
    # type: () -> None
    """Command line interface to rst or html viewer."""
    import sys
    view_help(sys.argv[1], qt=False)

if __name__ == "__main__":
    main()