This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert.py
executable file
·273 lines (213 loc) · 7.2 KB
/
convert.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
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
#!/usr/bin/env python3
"""
Convert md.md and rst.rst into useful output and comparisons.
"""
import re
import textwrap
import docutils.core
import markdown2
from docutils.writers.html4css1 import Writer, HTMLTranslator
class Buffer:
def __init__(self):
self.buffer = []
def append(self, text):
self.buffer.append(text)
def clear(self):
self.buffer = []
def flush(self):
buffered = "".join(self.buffer).strip()
if buffered:
yield "text", buffered
self.clear()
def parse_md(lines):
buffer = Buffer()
parsing_headers = True
for line in lines:
if "parse-headers-off" in line:
parsing_headers = False
continue
elif "parse-headers-on" in line:
parsing_headers = True
continue
is_header = False
if parsing_headers:
header_match = re.search(r"^(#+) (.+)$", line)
if header_match:
is_header = True
if is_header:
yield from buffer.flush()
hashes, text = header_match.groups()
yield f"h{len(hashes)}", text
elif "<!-- note:" in line:
note = line.strip().strip("-!><").partition(":")[-1].strip()
yield "note", note
else:
buffer.append(line)
yield from buffer.flush()
def parse_rst(lines):
buffer = Buffer()
prev_line = None
rules = {}
parsing_headers = True
for line in lines:
if "parse-headers-off" in line:
parsing_headers = False
continue
elif "parse-headers-on" in line:
parsing_headers = True
continue
is_header = False
if parsing_headers:
header_match = re.search(r"^([^\w\d])\1\1+$", line)
if header_match and prev_line:
is_header = True
if is_header:
char = header_match.group(1)
if char not in rules:
rules[char] = len(rules) + 1
level = rules[char]
if level != 1:
yield from buffer.flush()
buffer.clear()
yield (f"h{level}", prev_line.rstrip())
prev_line = None
elif ".. note: " in line:
yield ("note", line.partition(':')[-1].strip())
else:
if prev_line:
buffer.append(prev_line)
prev_line = line
if prev_line:
buffer.append(prev_line)
yield from buffer.flush()
TABLE_HEAD = """\
.. list-table::
:widths: 15 30 30 15
:header-rows: 1
* - What
- Markdown
- reStructuredText
- Notes
"""
ROW_FORMAT = """\
* - {h3}
- ::
{md}
- ::
{rst}
-
{notes}
"""
ROW_INDENT = 10
def row_indented(text):
return textwrap.indent(text, prefix=" " * ROW_INDENT)
def sections(parsed_data):
"""Convert a stream of parsed tokens into sections with text and notes.
Yields a stream of:
('h-level', 'header text', 'text', 'notes')
"""
header = None
text = []
notes = []
for ttype, ttext in parsed_data:
if ttype.startswith('h'):
if header:
yield *header, "\n".join(text), "\n".join(notes)
text = []
notes = []
header = (ttype, ttext)
elif ttype == "text":
text.append(ttext)
elif ttype == "note":
notes.append(ttext)
else:
raise Exception(f"Don't know ttype {ttype!r}")
yield *header, "\n".join(text), "\n".join(notes)
SCARE_COMMENT = """
.. Don't edit this file directly. It's created from four parts:
.. sheet_head.rst is the first content
.. md.md is a Markdown file parsed for content to go in the table.
.. rst.rst is an RST file parsed for content to go in the table.
.. sheet_foot.rst is the final content
..
.. See the README.rst for instructions.
"""
def combine(mdlines, rstlines):
"""Combine md lines and rst lines into a comparison sheet.
Yields text chunks.
"""
msections = sections(parse_md(mdlines))
rsections = sections(parse_rst(rstlines))
yield SCARE_COMMENT
with open("sheet_head.rst") as fhead:
yield fhead.read()
for (mh, mhtext, mtext, mnotes), (rh, rhtext, rtext, rnotes) in zip(msections, rsections):
# print(repr([mtype, mtext, rtype, rtext]))
if mh != rh:
raise Exception(f"Header mismatch: {mh} vs {rh}: {mhtext!r} vs {rhtext!r}")
if rh == "h1":
# H1 is for the document titles, which differ.
continue
if mhtext != rhtext:
raise Exception(f"Mismatched headers: {mhtext!r} vs {rhtext!r}")
if rh == "h2":
yield mhtext
yield "*" * len(mhtext)
yield TABLE_HEAD
elif rh == "h3":
notes = (mnotes + "\n" + rnotes).strip()
yield ROW_FORMAT.format(
h3=rhtext,
md=row_indented(mtext),
rst=row_indented(rtext),
notes=row_indented(notes),
)
else:
raise Exception(f"Surprising header: {rh!r}: {rhtext!r}")
with open("sheet_foot.rst") as fhead:
yield fhead.read()
MD_SOURCE = "md.md"
RST_SOURCE = "rst.rst"
def make_comparison(md_filename, rst_filename, output_filename):
print(f"Making comparison: {md_filename} + {rst_filename} -> {output_filename}")
with open(output_filename, "w") as out:
with open(md_filename) as mdlines:
with open(rst_filename) as rstlines:
for text in combine(mdlines, rstlines):
out.write(text)
out.write("\n")
def md_to_html(md_text):
"""Convert Markdown, using GitHub-like options."""
# I copied this monstrosity from: https://github.com/trentm/python-markdown2/wiki/link-patterns#converting-links-into-links-automatically
link_patterns = [
(re.compile(
r'((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+(:[0-9]+)?|(?:www\.|[\-;:&=\+\$,\w]+@)'
r'[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)'), r'\1'),
]
html_text = markdown2.markdown(
md_text,
extras=['fenced-code-blocks', 'link-patterns', 'footnotes'],
link_patterns=link_patterns,
)
return html_text
def make_md(md_filename, html_filename):
print(f"Converting markdown: {md_filename} -> {html_filename}")
with open(md_filename) as mdfile:
with open(html_filename, "w") as htmlfile:
htmlfile.write(md_to_html(mdfile.read()))
def rst_to_html(rst):
html_fragment_writer = Writer()
html_fragment_writer.translator_class = HTMLTranslator
return docutils.core.publish_string(rst, writer=html_fragment_writer)
def make_rst(rst_filename, html_filename):
print(f"Converting ReST: {rst_filename} -> {html_filename}")
with open(rst_filename) as rstfile:
with open(html_filename, "wb") as htmlfile:
htmlfile.write(rst_to_html(rstfile.read()))
def main():
make_md(MD_SOURCE, "md.html")
make_rst(RST_SOURCE, "rst.html")
make_comparison(MD_SOURCE, RST_SOURCE, "mdrst.rst")
make_rst("mdrst.rst", "mdrst.html")
if __name__ == "__main__":
main()