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 • 1.74 kiB
Python script, ASCII text executable
        
            
1
import re
2
3
4
def leading(string, character):
5
return len(string) - len(string.lstrip(character))
6
7
8
def trailing(string, character):
9
return len(string) - len(string.rstrip(character))
10
11
12
class Element:
13
def __init__(self):
14
pass
15
16
def __repr__(self):
17
return "Void block"
18
19
20
class Heading(Element):
21
def __init__(self, content, level):
22
super().__init__()
23
self.content = content
24
self.level = level
25
pass
26
27
def __repr__(self):
28
return f"Heading level {self.level}:\n\t" + self.content
29
30
31
class Paragraph(Element):
32
def __init__(self, content):
33
super().__init__()
34
self.content = content
35
36
def addLine(self, content):
37
self.content += content.strip() + " "
38
39
def __repr__(self):
40
return "Paragraph:\n\t" + self.content
41
42
43
def _tokenise(source):
44
tokens = []
45
46
currentBlock = Element
47
48
for line in source.split("\n"):
49
if not line.strip():
50
# Void block
51
52
tokens.append(currentBlock)
53
currentBlock = Element()
54
elif line.startswith("#") and leading(line.lstrip("#"), " ") == 1:
55
tokens.append(currentBlock)
56
57
content = line.lstrip("#").strip()
58
currentBlock = Heading(content, leading(line, "#"))
59
else:
60
if not isinstance(currentBlock, Paragraph):
61
tokens.append(currentBlock)
62
currentBlock = Paragraph("")
63
64
currentBlock.addLine(line)
65
66
tokens.append(currentBlock)
67
68
return tokens
69
70
71
for i in _tokenise(
72
"""
73
# Hello World!
74
## Title 1
75
### Part 1
76
#### Chapter 1
77
##### Article 1
78
###### Section 1
79
Lorem ipsum
80
dolor sit amet"""
81
):
82
print(repr(i))
83
84
85
def parseMarkdown(source):
86
tokens = _tokenise(source)
87
88
89
parseMarkdown("")
90