forked from donkirkby/donimoes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_parser.py
184 lines (145 loc) · 4.9 KB
/
book_parser.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
import re
class Styles(object):
Normal = 'Normal'
Heading = 'Heading'
Heading1 = 'Heading1'
Heading2 = 'Heading2'
Diagram = 'Diagram'
def parse(source):
lines = source.splitlines()
states = []
state = StartState()
for line in lines:
new_state = state.add(line)
if new_state is not state:
states.append(new_state)
state = new_state
links = {}
unlinked_states = []
for s in states:
try:
name, address = s.get_link()
except:
unlinked_states.append(s)
continue
links[name] = address
printed_states = []
for s in unlinked_states:
if not s.is_printed():
continue
s.text = replace_links(s.text, links)
s.text = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', s.text)
printed_states.append(s)
return printed_states
def replace_links(text, links):
replacement = ''
index = 0
for match in re.finditer(r'\[([^\]]+)\]\[([^\]]+)\]', text):
block = text[index:match.start()]
replacement += block
link_name = match.group(2)
link = links[link_name]
replacement += '<a href="%s">%s</a>' % (link, match.group(1))
index = match.end()
if replacement:
block = text[index:]
if block.startswith(' '):
block = ' ' + block[1:]
replacement += block
return replacement or text
class ParsingState(object):
def __init__(self, text=None, style=Styles.Normal, bullet=None):
self.text = text
self.style = style
self.bullet = bullet
def add(self, line):
if line.startswith(' '):
return DiagramState('').add(line)
match = re.match(r'^\[([^\]]+)\]:\s*(.*)$', line)
if match:
link_name = match.group(1)
address = match.group(2)
return LinkState(link_name, address)
match = re.match(r'^(#+)\s*(.*?)\s*#*$', line)
if match:
level = len(match.group(1))
heading_text = match.group(2)
return ParsingState(heading_text, Styles.Heading + str(level))
match = re.match(r'^(\d+)\.\s+(.*)$', line)
if match:
bullet = match.group(1)
text = match.group(2)
return BulletedState(text, bullet=bullet)
if line:
return ParagraphState(line)
return self
def is_printed(self):
return True
def __repr__(self):
return 'ParsingState({!r}, {!r}, {!r})'.format(self.text,
self.style,
self.bullet)
def __eq__(self, other):
return (self.text == other.text and
self.style == other.style and
self.bullet == other.bullet)
class StartState(ParsingState):
def is_printed(self):
return False
def __repr__(self):
return 'StartState()'
class ParagraphState(ParsingState):
def add(self, line):
if line:
self.text = self.text + ' ' + line
return self
return StartState()
def __repr__(self):
return 'ParagraphState({!r})'.format(self.text)
class BulletedState(ParsingState):
def add(self, line):
if not line.startswith(' '):
return StartState().add(line)
self.text = self.text + ' ' + line.strip()
return self
def __repr__(self):
return 'BulletedState({!r}, bullet={!r})'.format(self.text,
self.bullet)
class LinkState(ParsingState):
def __init__(self, name, address):
self.name = name
self.address = address
def get_link(self):
return self.name, self.address
def is_printed(self):
return False
def __repr__(self):
return 'LinkState({!r}, {!r})'.format(self.name, self.address)
class DiagramState(ParsingState):
def __init__(self, line):
super(DiagramState, self).__init__(line, Styles.Diagram)
def add(self, line):
if line.startswith(' '):
self.text = self.text + line[4:] + '\n'
return self
return StartState().add(line)
def __repr__(self):
return 'DiagramState({!r})'.format(self.text)
if __name__ == '__live_coding__':
import unittest
def testSomething(self):
source = """\
Paragraph with **emphasized text**.
"""
expected_tree = [
ParagraphState('Paragaph with <b>emphasized text</b>.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
class DummyTest(unittest.TestCase):
def test_delegation(self):
testSomething(self)
suite = unittest.TestSuite()
suite.addTest(DummyTest("test_delegation"))
test_results = unittest.TextTestRunner().run(suite)
print(test_results.errors)
print(test_results.failures)