-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
186 lines (133 loc) · 5.86 KB
/
Copy pathparser.py
File metadata and controls
186 lines (133 loc) · 5.86 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
# -*- 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('')])])
'''
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.']'''
return re.split(r'\n{2,}', string)
def parse_block(block):
'''Handle block that contains list items
'''
# 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 = 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(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(map(parse_paragraph, inner_items[1:]))
item.items.append(inner_lists)
return lists
return parse_paragraph(block)
def parse_paragraph(block):
parts = re.split('(\\*\\*[^\\*]*\\*\\*|\\*[^\\*]*\\*)', block) # split out Emphasis and Bold
return Paragraph(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(map(lambda (match, items): Header(len(match), items.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 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)
]
if __name__ == '__main__':
doctest.testmod()
with open('test.md', 'r') as f:
string = f.read()
for block in parse_markdown(string).blocks:
print block