This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
mdrenderer.py
200 lines (166 loc) · 4.58 KB
/
mdrenderer.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
# coding: utf-8
"""
Markdown renderer
=================
This class renders parsed markdown back to markdown.
It is useful for automatic modifications of the md contents.
## Authors and License
Copyright (C) 2015 Jaroslav Kysela
License: WTFPL 2
"""
from mistune import Renderer
class Object:
pass
class MdRenderer(Renderer):
def get_block(text):
type = text[0]
p = text.find(':')
if p <= 0:
return ('', '', '')
l = int(text[1:p])
t = text[p+1:p+1+l]
return (text[p+1+l:], type, t)
def newline(self):
return '\n'
def text(self, text):
return text
def linebreak(self):
return '\n'
def hrule(self):
return '---\n'
def header(self, text, level, raw=None):
return '#'*(level+1) + text + '\n\n'
def paragraph(self, text):
return text + '\n\n'
def list(self, text, ordered=True):
r = ''
while text:
text, type, t = MdRenderer.get_block(text)
if type == 'l':
r += (ordered and ('# ' + t) or ('* ' + t)) + '\n'
return r
def list_item(self, text):
return 'l' + str(len(text)) + ':' + text
def block_code(self, code, lang=None):
return '```no-highlight\n' + code + '\n```\n'
def block_quote(self, text):
r = ''
for line in text.splitlines():
r += (line and '> ' or '') + line + '\n'
return r
def _emphasis(self, text, pref):
return pref + text + pref + ' '
def emphasis(self, text):
return self._emphasis(text, '_')
def double_emphasis(self, text):
return self._emphasis(text, '__')
def strikethrough(self, text):
return self._emphasis(text, '~~')
def codespan(self, text):
return '`' + text + '`'
def autolink(self, link, is_email=False):
return '<' + link + '>'
def link(self, link, title, text, image=False):
r = (image and '!' or '') + '[' + text + '](' + link + ')'
if title:
r += '"' + title + '"'
return r
def image(self, src, title, text):
self.link(src, title, text, image=True)
def table(self, header, body):
hrows = []
while header:
header, type, t = MdRenderer.get_block(header)
if type == 'r':
flags = {}
cols = []
while t:
t, type2, t2 = MdRenderer.get_block(t)
if type2 == 'f':
fl, v = t2.split('=')
flags[fl] = v
elif type2 == 'c':
c = Object()
c.flags = flags
c.text = t2
cols.append(c)
hrows.append(cols)
brows = []
while body:
body, type, t = MdRenderer.get_block(body)
if type == 'r':
flags = {}
cols = []
while t:
t, type2, t2 = MdRenderer.get_block(t)
if type2 == 'f':
fl, v = t2.split('=')
flags[fl] = v
elif type2 == 'c':
c = Object()
c.flags = flags
c.text = t2
cols.append(c)
brows.append(cols)
colscount = 0
colmax = [0] * 100
align = [''] * 100
for row in hrows + brows:
colscount = max(len(row), colscount)
i = 0
for col in row:
colmax[i] = max(len(col.text), colmax[i])
if 'align' in col.flags:
align[i] = col.flags['align'][0]
i += 1
r = ''
for row in hrows:
i = 0
for col in row:
if i > 0:
r += ' | '
r += col.text.ljust(colmax[i])
i += 1
r += '\n'
for i in range(colscount):
if i > 0:
r += ' | '
if align[i] == 'c':
r += ':' + '-'.ljust(colmax[i]-2, '-') + ':'
elif align[i] == 'l':
r += ':' + '-'.ljust(colmax[i]-1, '-')
elif align[i] == 'r':
r += '-'.ljust(colmax[i]-1, '-') + ':'
else:
r += '-'.ljust(colmax[i], '-')
r += '\n'
for row in brows:
i = 0
for col in row:
if i > 0:
r += ' | '
r += col.text.ljust(colmax[i])
i += 1
r += '\n'
return r
def table_row(self, content):
return 'r' + str(len(content)) + ':' + content
def table_cell(self, content, **flags):
content = content.replace('\n', ' ')
r = ''
for fl in flags:
v = flags[fl]
if type(v) == type(True):
v = v and 1 or 0
v = str(v) and str(v) or ''
r += 'f' + str(len(fl) + 1 + len(v)) + ':' + fl + '=' + v
return r + 'c' + str(len(content)) + ':' + content
def footnote_ref(self, key, index):
return '[^' + str(index) + ']'
def footnote_item(self, key, text):
r = '[^' + str(index) + ']:\n'
for l in text.split('\n'):
r += ' ' + l.lstrip().rstrip() + '\n'
return r
def footnotes(self, text):
return text