By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 markdown.py

View raw Download
text/x-script.python • 12.9 kiB
Python script, ASCII text executable
        
            
1
import re
2
import bs4 as beautifulsoup
3
import sys
4
5
6
def only_chars(string, chars):
7
chars = set(chars)
8
all_chars = set(string)
9
return all_chars.issubset(chars)
10
11
12
inline_regex = r"""
13
(?P<imageFlag>!?) \[ (?P<urlText>[^\[\]]*) \] \((?P<urlDestination>[^\(\)]*)\) # hyperlink or media
14
|
15
(?P<em>\*{1,7}) (?P<textEm>(?:\\\*|[^*])*) (?P=em) # emphasis with * not requiring space on either side
16
|
17
(?:^|\s)(?P<em2>_{1,7}) (?P<textEm2>(?:\\.|[^*])*) (?P=em2)(?=\s|$) # emphasis with _ requiring space on at least one side
18
|
19
[`] (?P<textCode>(?:\\[`]|[^`])*) [`] # inline code
20
|
21
(?P<strike>~{2}) (?P<textStrike>(?:\\[~]|[^~])*) (~{2}) # strikethrough
22
|
23
(?P<diff>\-\-|\+\+) (?P<textDiff>(?:\\[-+]|[^-+])*) (?P=diff) # diffs
24
"""
25
26
27
def leading(string, character):
28
return len(string) - len(string.lstrip(character))
29
30
31
def trailing(string, character):
32
return len(string) - len(string.rstrip(character))
33
34
35
class Element:
36
def __init__(self):
37
self.classes = []
38
self.content = None
39
pass
40
41
def __repr__(self):
42
return "Void block"
43
44
@property
45
def tag_name(self):
46
return "m-void"
47
48
49
class Container(Element):
50
def __init__(self, content):
51
super().__init__()
52
self.content = parse_line(content)
53
54
def __repr__(self):
55
return "Generic container element: " + repr(self.content)
56
57
58
class Rule(Element):
59
def __init__(self):
60
super().__init__()
61
62
def __repr__(self):
63
return "Rule"
64
65
@property
66
def tag_name(self):
67
return "hr"
68
69
70
class HardBreak(Element):
71
def __init__(self):
72
super().__init__()
73
74
def __repr__(self):
75
return "Hard break"
76
77
@property
78
def tag_name(self):
79
return "br"
80
81
82
class Heading(Container):
83
def __init__(self, content, level):
84
super().__init__(content)
85
self.level = level
86
pass
87
88
def __repr__(self):
89
return f"Heading level {self.level}:\n\t" + repr(self.content)
90
91
@property
92
def tag_name(self):
93
return "h" + str(self.level)
94
95
96
class Paragraph(Container):
97
def __init__(self, content):
98
super().__init__("")
99
self.content = parse_line(content)
100
101
def __repr__(self):
102
return "Paragraph:\n\t" + repr(self.content)
103
104
@property
105
def tag_name(self):
106
return "p"
107
108
109
class CodeBlock(Element):
110
def __init__(self, content, language="text"):
111
super().__init__()
112
self.content = content
113
self.language = language
114
115
def __repr__(self):
116
return f"Code block ({self.language}):\n\t" + repr(self.content)
117
118
@property
119
def tag_name(self):
120
return "pre"
121
122
123
class UnorderedList(Element):
124
def __init__(self, content):
125
super().__init__()
126
self.content = content
127
128
def __repr__(self):
129
return "Unordered list:\n\t" + repr(self.content)
130
131
@property
132
def tag_name(self):
133
return "ul"
134
135
136
class OrderedList(Element):
137
def __init__(self, content):
138
super().__init__()
139
self.content = content
140
141
def __repr__(self):
142
return "Ordered list:\n\t" + repr(self.content)
143
144
@property
145
def tag_name(self):
146
return "ol"
147
148
149
class ListItem(Element):
150
def __init__(self, content):
151
super().__init__()
152
self.content = tokenise(content)
153
154
def __repr__(self):
155
return "List item:\n\t" + repr(self.content)
156
157
@property
158
def tag_name(self):
159
return "li"
160
161
162
class Blockquote(Paragraph):
163
def __init__(self, content):
164
super().__init__("")
165
self.content = tokenise(content)
166
167
def __repr__(self):
168
return "Blockquote:\n\t" + repr(self.content)
169
170
@property
171
def tag_name(self):
172
return "blockquote"
173
174
175
class Emphasis(Container):
176
def __init__(self, content, value):
177
super().__init__(content)
178
self.value = value
179
if value >= 4:
180
self.classes.append("emphasis-3")
181
if value % 4 >= 2:
182
self.classes.append("emphasis-2")
183
if value % 2:
184
self.classes.append("emphasis-1")
185
186
def __repr__(self):
187
return f"Emphasis ({self.value}): " + repr(self.content)
188
189
@property
190
def tag_name(self):
191
return "em" if self.value == 1 else "strong"
192
193
194
class Code(Element):
195
def __init__(self, content):
196
super().__init__()
197
self.content = [content]
198
199
def __repr__(self):
200
return f"Inline code: {self.content}"
201
202
@property
203
def tag_name(self):
204
return "code"
205
206
207
class Strikethrough(Container):
208
def __init__(self, content):
209
super().__init__(content)
210
211
def __repr__(self):
212
return f"Strikethrough: {repr(self.content)}"
213
214
@property
215
def tag_name(self):
216
return "s"
217
218
219
class Diff(Container):
220
def __init__(self, content, value):
221
super().__init__(content)
222
self.value = value
223
224
def __repr__(self):
225
return f"Diff ({self.value}): {self.content}"
226
227
@property
228
def tag_name(self):
229
return "ins" if self.value == "++" else "del"
230
231
232
class Link(Element):
233
def __init__(self, content, destination, image=False):
234
super().__init__()
235
self.content = parse_line(content)
236
self.destination = destination
237
self.image = image
238
239
def __repr__(self):
240
return f"{'Image' if self.image else 'Link'}: {self.content} -> {self.destination}"
241
242
@property
243
def tag_name(self):
244
return "a"
245
246
247
class Image(Link):
248
def __init__(self, text, destination):
249
super().__init__(text, destination, True)
250
251
@property
252
def tag_name(self):
253
return "img"
254
255
256
def parse_line(source):
257
if trailing(source, "\\") == 1:
258
source = source.rstrip("\\")
259
hard_break = True
260
else:
261
hard_break = False
262
263
tokens = []
264
pattern = re.compile(inlineRegex, re.MULTILINE | re.DOTALL | re.VERBOSE)
265
matches = pattern.finditer(source)
266
267
lookup = 0
268
for i in matches:
269
l = i.start()
270
r = i.end()
271
tokens.append(source[lookup:l])
272
273
lookup = r
274
275
if i.group("em"):
276
tokens.append(Emphasis(i.group("textEm"), len(i.group("em"))))
277
if i.group("em2"):
278
tokens.append(Emphasis(i.group("textEm2"), len(i.group("em2"))))
279
if i.group("textCode"):
280
tokens.append(Code(i.group("textCode")))
281
if i.group("strike"):
282
tokens.append(Strikethrough(i.group("textStrike")))
283
if i.group("diff"):
284
tokens.append(Diff(i.group("textDiff"), i.group("diff")))
285
if i.group("urlText"):
286
if i.group("imageFlag"):
287
tokens.append(Image(i.group("urlText"), i.group("urlDestination")))
288
else:
289
tokens.append(Link(i.group("urlText"), i.group("urlDestination")))
290
291
tokens.append(source[lookup:])
292
293
if hard_break:
294
tokens.append(HardBreak())
295
296
return tokens
297
298
299
def tokenise(source):
300
tokens = []
301
302
current_block = Element()
303
304
lines = [line[1:] if line.startswith(" ") else line for line in source.split("\n")] # remove leading spaces
305
306
i = 0
307
while i < len(lines):
308
line = lines[i]
309
if not line.strip() or line.startswith(";"):
310
# Void block
311
312
tokens.append(current_block)
313
current_block = Element()
314
315
i += 1
316
elif only_chars(line.strip(), "-_* ") and len(line.strip()) >= 3:
317
# Horizontal rule
318
319
tokens.append(current_block)
320
current_block = Rule()
321
322
i += 1
323
elif (lines[i].startswith("*") or lines[i].startswith("+") or lines[i].startswith("-")) and lines[i][1:].startswith(" "):
324
if not isinstance(current_block, UnorderedList):
325
tokens.append(current_block)
326
327
content = []
328
329
while i < len(lines) and ((lines[i].startswith("*") or lines[i].startswith("+") or lines[i].startswith("-")) and lines[i][1:].startswith(" ")):
330
inner_content = lines[i][2:].strip() + "\n" # discard marker and space
331
i += 1
332
while i < len(lines) and lines[i].strip() and not ((lines[i].startswith("*") or lines[i].startswith("+") or lines[i].startswith("-")) and lines[i][1] == " "):
333
inner_content += lines[i] + "\n"
334
i += 1
335
336
content.append(ListItem(inner_content))
337
338
current_block = UnorderedList(content)
339
elif re.match(r"^\d+\.", line):
340
if not isinstance(current_block, UnorderedList):
341
tokens.append(current_block)
342
343
content = []
344
345
while i < len(lines) and re.match(r"^ ?\d+\.", lines[i]) and len(lines[i].split(".", 1)) > 1:
346
inner_content = lines[i].split(".", 1)[1] + "\n" # discard number and period
347
i += 1
348
marker_length = len(lines[i].split(".", 1)[0]) + 1
349
while i < len(lines) and lines[i].strip() and not re.match(r"^ ?\d+\.", lines[i]):
350
if re.match(r"^ \d+\.", lines[i]):
351
marker_length = 2
352
inner_content += lines[i][marker_length:] + "\n"
353
i += 1
354
355
content.append(ListItem(inner_content))
356
357
current_block = OrderedList(content)
358
elif line.startswith("#") and leading(line.lstrip("#"), " "):
359
tokens.append(current_block)
360
361
content = line.lstrip("#").strip()
362
current_block = Heading(content, leading(line, "#"))
363
364
i += 1
365
elif line.startswith(">"):
366
if not isinstance(current_block, Blockquote):
367
tokens.append(current_block)
368
369
content = ""
370
371
while i < len(lines) and (lines[i].startswith(">") or (not lines[i].startswith("#") and not lines[i].startswith(">") and lines[i].strip()) and not only_chars(line.strip(), "-_* ") and len(line.strip()) >= 3):
372
content += lines[i].lstrip(">") + "\n"
373
i += 1
374
375
current_block = Blockquote(content)
376
elif leading(line, "~") == 3 or leading(line, "`") == 3:
377
if not isinstance(current_block, CodeBlock):
378
tokens.append(current_block)
379
380
language = line.lstrip("`~").strip()
381
382
content = ""
383
i += 1 # skip the opening fence
384
while i < len(lines) and not lines[i].strip() in ("```", "~~~"):
385
content += lines[i] + "\n"
386
i += 1
387
388
if i < len(lines):
389
i += 1 # prevent a new block from beginning with the closing fence
390
391
current_block = CodeBlock(content, language=language)
392
elif i < len(lines) - 1 and (only_chars(lines[i+1].strip(), "=") or only_chars(lines[i+1].strip(), "-")) and lines[i+1].strip():
393
tokens.append(current_block)
394
395
content = line.strip()
396
current_block = Heading(content, 1 if lines[i+1].startswith("=") else 2)
397
398
i += 2
399
else:
400
if not isinstance(current_block, Paragraph):
401
# Create a paragraph, if there is no other specifier
402
tokens.append(current_block)
403
404
content = ""
405
406
while (i < len(lines)
407
and not lines[i].startswith("#")
408
and not lines[i].startswith(">")
409
and not lines[i].startswith(";")
410
and not lines[i].startswith("* ")
411
and not lines[i].startswith("+ ")
412
and not lines[i].startswith("- ")
413
and not lines[i].startswith("~~~")
414
and not lines[i].startswith("```")
415
and not re.match(r"^\d+\.", lines[i])
416
and lines[i].strip()):
417
content += lines[i].strip() + "\n"
418
i += 1
419
420
current_block = Paragraph(content)
421
422
tokens.append(current_block)
423
424
return tokens
425
426
427
def make_html(ast):
428
soup = beautifulsoup.BeautifulSoup()
429
for i in ast:
430
# Use bs4 to generate HTML
431
if isinstance(i, str):
432
soup.append(i)
433
elif hasattr(i, "content") and i.tag_name != "m-void":
434
tag = soup.new_tag(str(i.tag_name))
435
if i.tag_name == "a":
436
tag["href"] = i.destination
437
if i.tag_name == "img":
438
tag["src"] = i.destination
439
tag["alt"] = " ".join(i.content)
440
if i.tag_name == "pre":
441
tag["data-language"] = i.language
442
if i.classes:
443
tag["class"] = " ".join(i.classes)
444
try:
445
if isinstance(i.content, list):
446
tag.append(make_html(i.content))
447
elif i.content and i.tag_name != "img":
448
tag.string = i.content
449
450
if i.tag_name == "img":
451
tag.string = ""
452
except AttributeError as exc:
453
# print(i)
454
print(exc, file=sys.stderr)
455
soup.append(tag)
456
return soup
457
458
459
def markdown2html(markdown):
460
return make_html(tokenise(markdown))
461
462