-
Notifications
You must be signed in to change notification settings - Fork 7
/
rst2html.py
executable file
·60 lines (49 loc) · 1.64 KB
/
rst2html.py
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
#!/usr/bin/python3
"""
Cleans-up Sphinx-only constructs (ie from README.rst),
so that *PyPi* can format it properly.
To check for remaining errors, install ``sphinx`` and run::
python setup.py --long-description | sed -file 'this_file.sed' | rst2html.py --halt=warning
"""
import re
def yield_sphinx_only_markup(lines):
"""
:param file_inp: a `filename` or ``sys.stdin``?
:param file_out: a `filename` or ``sys.stdout`?`
"""
substs = [
# Selected Sphinx-only Roles.
#
(r":abbr:`([^`]+)`", r"\1"),
(r":ref:`([^`]+)`", r"`\1`_"),
(r":term:`([^`]+)`", r"**\1**"),
(r":dfn:`([^`]+)`", r"**\1**"),
(r":(samp|guilabel|menuselection):`([^`]+)`", r"``\2``"),
# Sphinx-only roles:
# :foo:`bar` --> foo(``bar``)
# :a:foo:`bar` XXX afoo(``bar``)
#
# (r'(:(\w+))?:(\w+):`([^`]*)`', r'\2\3(``\4``)'),
(r":(\w+):`([^`]*)`", r"\1(``\2``)"),
# Sphinx-only Directives.
#
(r"\.\. doctest", r"code-block"),
(r"\.\. plot::", r".. "),
(r"\.\. seealso", r"info"),
(r"\.\. glossary", r"rubric"),
(r"\.\. figure::", r".. "),
# Other
#
(r"\|version\|", r"x.x.x"),
]
regex_subs = [(re.compile(regex, re.IGNORECASE), sub) for (regex, sub) in substs]
def clean_line(line):
try:
for (regex, sub) in regex_subs:
line = regex.sub(sub, line)
except Exception as ex:
print(f"ERROR: {regex}, (line({sub})")
raise ex
return line
for line in lines:
yield clean_line(line)