forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_parser.py
More file actions
68 lines (56 loc) · 1.68 KB
/
custom_parser.py
File metadata and controls
68 lines (56 loc) · 1.68 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
# -*- coding: utf-8 -*-
"""
@author:XuMing([email protected])
@description:
"""
def parse(text):
# 检测当前 input 解析状态
result = test_state(input)
if block_state == BLOCK.Block:
return result
# 分析标题标记 #
title_rank = 0
for i in range(6, 0, -1):
if input[:i] == '#' * i:
title_rank = i
break
if title_rank != 0:
# 处理标题,转化为相应的 HTML 文本
result = handleTitle(input, title_rank)
return result
# 分析分割线标记 --
if len(input) > 2 and all_same(input[:-1], '-') and input[-1] == '\n':
result = "<hr>"
return result
# 解析无序列表
unorderd = ['+', '-']
if result != "" and result[0] in unorderd:
result = handleUnorderd(result)
is_normal = False
f = input[0]
count = 0
sys_q = False
while f == '>':
count += 1
f = input[count]
sys_q = True
if sys_q:
result = "<blockquote style=\"color:#8fbc8f\"> " * count + "<b>" + input[
count:] + "</b>" + "</blockquote>" * count
is_normal = False
# 处理特殊标记,比如 ***, ~~~
result = tokenHandler(result)
# END
# 解析图像链接
result = link_image(result)
pa = re.compile(r'^(\s)*$')
a = pa.match(input)
if input[-1] == "\n" and is_normal == True and not a:
result += "</br>"
return result
with open('c.md', 'r', encoding='utf-8') as f:
# 逐行解析 markdwon 文件
for eachline in f:
result = parse(eachline.strip())
if result:
print(result)