forked from iswbm/PythonCodingTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2rst.py
120 lines (103 loc) · 3.64 KB
/
md2rst.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
# coding:utf-8
import os
import commands
import subprocess
import platform
blog_path = ''
base_link = "http:https://python-online.cn/zh_CN/latest/"
readme_header = '''
这是我的个人博客( [MING's BLOG](http:https://python-online.cn/) ),主要写关于Python的一些思考总结。
关于搭建教程,感兴趣的可以查看这边:[Sphinx 搭建博客的图文教程](http:https://python-online.cn/zh_CN/latest/c04/c04_03.rst)
'''
readme_tooter = '''
---
![关注公众号,获取最新干货!](http:https://image.python-online.cn/20191117155836.png)
'''
osName = platform.system()
if (osName == 'Windows'):
blog_path = 'F:\\VMP-Code\\07. PythonCodingTime\\source'
index_path = 'F:\\VMP-Code\\07. PythonCodingTime\\README.md'
elif (osName == 'Darwin'):
blog_path = '/Users/MING/Github/PythonCodingTime/source'
index_path = '/Users/MING/Github/PythonCodingTime/README.md'
def get_file_info(filename):
with open(filename, 'r') as file:
first_line = file.readline().replace("#", "").strip()
return first_line.split(' ', 1)
def make_line(chapter, file):
page_name, _ = os.path.splitext(file)
(index, title) = get_file_info(file)
url = base_link + chapter + "/" + page_name + ".html"
item_list = ["-", index, "[{}]({})\n".format(title, url)]
return " ".join(item_list)
def render_index_page(index_info):
'''
生成 readme.md 索引文件,包含所有文件目录
'''
# 重新排序
index_info = sorted(index_info.iteritems(), key=lambda item:item[0], reverse=False)
# 写入文件
with open(index_path, 'w+') as file:
file.write(readme_header)
for chp, info in index_info:
chp_name = info["name"]
file.write("## " + chp_name + "\n")
for line in info["contents"]:
file.write(line)
file.write("\n")
file.write(readme_tooter)
def convert_md5_to_rst(file):
'''
转换格式:md5转换成rst
'''
(filename, extension) = os.path.splitext(file)
convert_cmd = 'pandoc -V mainfont="SimSun" -f markdown -t rst {md_file} -o {rst_file}'.format(
md_file=filename+'.md', rst_file=filename+'.rst'
)
status, output = commands.getstatusoutput(convert_cmd)
#retcode = subprocess.call(convert_cmd)
if status != 0:
print(output)
if status == 0:
print(file + ' 处理完成')
else:
print(file + '处理失败')
def get_all_dir():
'''
获取所有的目录
'''
dir_list = []
file_list = os.listdir(blog_path)
for item in file_list:
abs_path = os.path.join(blog_path, item)
if os.path.isdir(abs_path):
dir_list.append(abs_path)
return dir_list
def init_index_info():
'''
初始化索引
'''
index_info = {}
chapter_dir = os.path.join(blog_path, "chapters")
os.chdir(chapter_dir)
for file in os.listdir(chapter_dir):
name, _ = os.path.splitext(file)
with open(file, 'r') as f:
chapter_name = f.readlines()[1].strip()
index_info[name.replace("p", "")] = {"name": chapter_name, "contents": []}
return index_info
def main(index_info):
for folder in get_all_dir():
os.chdir(folder)
chapter = os.path.split(folder)[1]
all_file = os.listdir(folder)
all_md_file = sorted([file for file in all_file if file.endswith('md')])
for file in all_md_file:
line = make_line(chapter, file)
index_info[chapter.replace("c", "")]["contents"].append(line)
convert_md5_to_rst(file)
if __name__ == '__main__':
index_info = init_index_info()
main(index_info)
render_index_page(index_info)
print("OK")