-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransform_to_html.py
81 lines (55 loc) · 1.94 KB
/
transform_to_html.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
# -*- coding:utf-8 -*-
import re
# 要处理的文件路径
__origin_path__ = '/Users/taffy/Downloads/api'
# 生成 html 文件路径
__generate_path__ = '/Users/taffy/api_document/api'
prefix = '<body style="font:14px/1.5 tahoma,arial,sans-serif;"><h1><meta charset="utf-8" /><a name="totop">API 文档汇总:</a></h1>'
toTop = '<a style="position:fixed;top: 100px;right:50px;" href="#totop">回到顶部</a>'
suffix = '</body>'
def __filter():
pattern = re.compile('https:\/\/.*?\n', re.I)
items = re.findall(pattern, fileString)
return items
def __judge_http(str, pos):
for item in items:
if str == item.rstrip():
return True
return False
def __insert():
# 合成字符串,然后写入文件
httpString = ''
i = 0
while i < len(httpList):
httpString += str(httpList[i])
i += 1
# 写入文件
with open(__generate_path__ + '.html', 'w') as file:
file.write(prefix + toTop + httpString + htmlString + suffix)
with open(__origin_path__, 'r') as file:
fileString = file.read()
items = __filter()
with open(__origin_path__, 'r') as file:
# 以行获取文件
alllines = file.readlines()
htmlString = ''
httpList = []
position = 0
for line in alllines:
position += 1
s = line.rstrip()
# 每条 url 与 文本中的 url 做绑定
if __judge_http(s, position):
a_name = str(position + 5 + len(items))
httpList.append('<div style="height:25px;"><a style="text-decoration: none;" href="#'+ a_name +'">' + s + '</a></div>')
htmlString += '<div style="height:20px;"><a name="'+ a_name +'">' + s + '</a></div>'
continue
# 非 url 的行
tt = '<div style="height:20px; color: #999;">'
for c in s:
if c == ' ':
tt += ' '
else:
break
htmlString += tt + s +'</div>'
__insert()