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 • 2.72 kiB
Python script, ASCII text executable
        
            
1
import re
2
3
4
inlineRegex = r"""
5
(?P<em>[*_]{1,7}) (?P<textEm>(?:\\[*]|[^*])*) (?P=em) # emphasis
6
|
7
[`] (?P<textCode>(?:\\[`]|[^`])*) [`] # inline code
8
|
9
(?P<imageFlag>!?) \[ (?P<urlText>[^\[\]]*) \] \((?P<urlDestination>[^\(\)]*)\) # hyperlink or media
10
|
11
(?P<strike>~{2}) (?P<textStrike>(?:\\[~]|[^~])*) (~{2}) # strikethrough
12
|
13
(?P<diff>\-\-|\+\+) (?P<textDiff>(?:\\[-+]|[^-+])*) (?P=diff) # diffs
14
"""
15
16
17
def leading(string, character):
18
return len(string) - len(string.lstrip(character))
19
20
21
def trailing(string, character):
22
return len(string) - len(string.rstrip(character))
23
24
25
class Element:
26
def __init__(self):
27
pass
28
29
def __repr__(self):
30
return "Void block"
31
32
33
class Heading(Element):
34
def __init__(self, content, level):
35
super().__init__()
36
self.content = content
37
self.level = level
38
pass
39
40
def __repr__(self):
41
return f"Heading level {self.level}:\n\t" + self.content
42
43
44
class Paragraph(Element):
45
def __init__(self, content):
46
super().__init__()
47
self.content = content
48
49
def addLine(self, content):
50
self.content += content.strip() + " "
51
52
def __repr__(self):
53
return "Paragraph:\n\t" + self.content
54
55
56
class Emphasis(Element):
57
def __init__(self, content, bold, italic):
58
super().__init__()
59
self.content = content
60
self.bold = bold
61
self.italic = italic
62
63
def __repr__(self):
64
return f"Emphasis (bold {self.bold}, italic {self.italic}):\n\t" + self.content
65
66
67
def _parseLine(source):
68
if trailing(source, "\\") == 1:
69
source = source.rstrip("\\")
70
source += "\n"
71
72
tokens = [source]
73
74
75
def _tokenise(source):
76
tokens = []
77
78
currentBlock = Element
79
80
for line in source.split("\n"):
81
if not line.strip():
82
# Void block
83
84
tokens.append(currentBlock)
85
currentBlock = Element()
86
elif line.startswith("#") and leading(line.lstrip("#"), " ") == 1:
87
tokens.append(currentBlock)
88
89
content = line.lstrip("#").strip()
90
currentBlock = Heading(content, leading(line, "#"))
91
else:
92
if not isinstance(currentBlock, Paragraph):
93
# Paragraph is default
94
95
tokens.append(currentBlock)
96
currentBlock = Paragraph("")
97
98
currentBlock.addLine(line)
99
100
tokens.append(currentBlock)
101
102
return tokens
103
104
105
for i in _tokenise(
106
"""
107
# Hello World!
108
## Title 1
109
### Part 1
110
#### Chapter 1
111
##### Article 1
112
###### Section 1
113
Lorem ipsum
114
dolor sit amet
115
116
..."""
117
):
118
print(repr(i))
119
120
121
def parseMarkdown(source):
122
tokens = _tokenise(source)
123
124
125
parseMarkdown("")
126