forked from yhfyhf/Markdown-Parser-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
284 lines (220 loc) · 8.76 KB
/
Copy pathparser.py
File metadata and controls
284 lines (220 loc) · 8.76 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# -*- encoding: utf-8 -*-
'''Parses Markdown strings and return a Markdown object.
First split strings into blocks as paragraphs. For each paragraph,
parse it when it is a List item, a Header, an Emphasis, a Bold, or just simple text.
Usage:
>>> parse_markdown('This is a paragraph.')
Markdown([Paragraph([Text('This is a paragraph.')])])
>>> parse_markdown('The first paragraph.\\n\\nThe second paragraph.')
Markdown([Paragraph([Text('The first paragraph.')]), Paragraph([Text('The second paragraph.')])])
>>> parse_markdown('Emphasis *is* supported.')
Markdown([Paragraph([Text('Emphasis '), Emphasis('is'), Text(' supported.')])])
>>> parse_markdown('Bold **is** supported.')
Markdown([Paragraph([Text('Bold '), Bold('is'), Text(' supported.')])])
>>> parse_markdown('- a list')
Markdown([List([Paragraph([Text('a list')])])])
>>> parse_markdown('- the first item\\n- the second item')
Markdown([List([Paragraph([Text('the first item')]), Paragraph([Text('the second item')])])])
>>> parse_markdown('- Emphasis *is* supported.')
Markdown([List([Paragraph([Text('Emphasis '), Emphasis('is'), Text(' supported.')])])])
>>> parse_markdown('* the first item\\n* the second item')
Markdown([List([Paragraph([Text('the first item')]), Paragraph([Text('the second item')])])])
>>> parse_markdown('- item1\\n- item2\\n - item2.1\\n - item2.2\\n- item3')
Markdown([List([Paragraph([Text('item1')]), Paragraph([Paragraph([Text('item2')]), List([Paragraph([Text('item2.1')]), Paragraph([Text('item2.2')])])]), Paragraph([Text('item3')])])])
>>> parse_markdown('#######this is header')
Markdown([Paragraph([Header(6,[Text('#this is header')])])])
>>> parse_markdown('#######this is *header* and **header**')
Markdown([Paragraph([Header(6,[Text('#this is '), Emphasis('header'), Text(' and '), Bold('header'), Text('')])])])
>>> parse_markdown('#header1\\n##header2\\n\\nanother paragraph')
Markdown([Paragraph([Header(1,[Text('header1')]), Header(2,[Text('header2')])]), Paragraph([Text('another paragraph')])])
>>> parse_markdown('hello ##this is not *header* and **header**')
Markdown([Paragraph([Text('hello ##this is not '), Emphasis('header'), Text(' and '), Bold('header'), Text('')])])
>>> parse_markdown(\'\'\'```js
var x = 1
var y = 2
console.info(x+y)
```\'\'\')
Markdown([Paragraph([Code('```js\nvar x = 1\nvar y = 2\n\n# debug\nconsole.info(x+y)\n```')])])
'''
import re
import doctest
def parse_markdown(string):
return Markdown([parse_block(block) for block in split_into_blocks(string)])
def split_into_blocks(string):
'''Return a list of blocks split by '\n\n'
>>> split_into_blocks('The first block.\\n\\n\\nThe second block.')
['The first block.', 'The second block.']'''
_blocks = re.split(r'\n{2,}', string)
blocks = []
tmp = []
closed = True
for block in _blocks:
if closed:
tmp.clear()
# code start
tmp.append(block)
if closed:
if re.match('```[\s\S]*[^`]$', block):
closed = False
# code end
else:
if re.match('[\s\S]*```$', block):
closed = True
if closed:
blocks.append('\n\n'.join(tmp))
return blocks
def parse_block(block):
'''Handle block that contains list items
'''
# parse code block
match = re.match(r'^```', block)
if match:
return Paragraph([Code(block)])
# extract headers before list
block = parse_header(block)
if not isinstance(block, str):
for header in block.items:
parts = re.split(
'(\\*\\*[^\\*]*\\*\\*|\\*[^\\*]*\\*)', header.items)
header.items = list(map(parse_part, parts))
return block
# List items begins with ' - ' or ' * '
match = re.match(r'^[-|\*]\s+', block)
if match is not None:
# '^' should match at the beginning of the string and at the beginning of each line
items = [item.strip() for item in re.split(
r'^[-|\*]\s+', block, flags=re.M)[1:]]
lists = List(list(map(parse_paragraph, items)))
for item in lists.items:
match = re.search(r'\s+[-|\*]\s+', item.items[0].text)
if match is not None:
inner_items = [inner_item.strip() for inner_item in re.split(
r'\s+[-|\*]\s+', item.items[0].text, flags=re.M)]
item.items = list()
item.items.append(parse_paragraph(inner_items[0]))
inner_lists = List(list(map(parse_paragraph, inner_items[1:])))
item.items.append(inner_lists)
return lists
return parse_paragraph(block)
def parse_paragraph(block):
# split out Emphasis and Bold
parts = re.split('(\\*\\*[^\\*]*\\*\\*|\\*[^\\*]*\\*)', block)
return Paragraph(list(map(parse_part, parts)))
def parse_part(string):
'''Parse Emphasis and Bold
'''
for regexp, klass in INLINE_ELEMENTS:
match = re.match(regexp, string)
if match is not None:
return klass(match.group(1))
return Text(string)
def parse_header(string):
'''Parse headers
'''
matches = re.findall(r'^#{1,6}', string, flags=re.M)
header_items = re.split(r'^#{1,6}', string, flags=re.M)[1:]
if len(matches) > 0:
return Paragraph(list(map(lambda match_items: Header(len(match_items[0]), match_items[1].strip()), zip(matches, header_items))))
return string
class Markdown(object):
def __init__(self, blocks):
self.blocks = blocks
def __repr__(self):
return 'Markdown({!r})'.format(self.blocks)
class Paragraph(object):
def __init__(self, items):
self.items = items
def __repr__(self):
return 'Paragraph({!r})'.format(self.items)
class List(object):
def __init__(self, items):
self.items = items
def __repr__(self):
return 'List({!r})'.format(self.items)
class Code(object):
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Code({!r})'.format(self.text)
class Text(object):
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Text({!r})'.format(self.text)
class Emphasis(object):
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Emphasis({!r})'.format(self.text)
class Bold(object):
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Bold({!r})'.format(self.text)
class Header(object):
def __init__(self, level, items):
self.level = level
self.items = items
def __repr__(self):
return 'Header({},{!r})'.format(self.level, self.items)
INLINE_ELEMENTS = [
(r'\*\*([^\*]*)\*\*', Bold),
(r'\*([^\*]*)\*', Emphasis)
]
def print_block(block, depth=0):
if type(block) == Paragraph:
print('======Paragraph start======')
for sub_block in block.items:
print_block(sub_block, depth+1)
print('======Paragraph end======\n')
elif type(block) == List:
print('-----List start-----')
for sub_block in block.items:
print_block(sub_block, depth+1)
print('-----List end-----\n')
else:
print('{} {}'.format('-'*depth, block))
def output_block(block, depth=0):
result = []
if type(block) == Paragraph:
for sub_block in block.items:
result.extend(output_block(sub_block, depth+1))
result.append('\n')
elif type(block) == List:
for sub_block in block.items:
result.append('- ')
result.extend(output_block(sub_block, depth+1))
result.append('\n\n')
elif type(block) == Header:
result.append('#'*block.level + ' ')
for sub_block in block.items:
result.extend(output_block(sub_block, depth+1))
result.append('\n\n')
elif type(block) == Emphasis:
result.append('*{}*'.format(block))
elif type(block) == Bold:
result.append('**{}**'.format(block))
elif type(block) == Code:
result.append('\n')
result.append(block.text)
elif type(block) == Text:
if block.text.startswith('>'):
result.append('\n')
result.append(block.text)
if depth == 0 and block.text.endswith('\n---'):
result.append('\n\n')
if depth == 0 and block.text.endswith('\n```'):
result.append('\n\n')
# fanyi
if not (block.text.startswith('---') or block.text.startswith('>')) and block.text.strip():
print(block.text)
print('---------------------\n')
if depth == 0:
result.append('\n')
return result
if __name__ == '__main__':
with open('out.md', 'w') as fo, open('test.md', 'r') as f:
string = f.read()
for block in parse_markdown(string).blocks:
fo.write(''.join(output_block(block, 0)))
# print_block(block)