-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateBlog.py
More file actions
112 lines (102 loc) · 2.91 KB
/
Copy pathUpdateBlog.py
File metadata and controls
112 lines (102 loc) · 2.91 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
import os
import sys
import shutil
import subprocess
def replace_nav(srcfile, dstfile):
nav_before_lines = []
with open(srcfile, 'r', encoding='utf8') as f:
lines = f.readlines()
for line in lines:
if 'nav:' in line:
break
nav_before_lines.append(line)
nav_after_lines = []
with open(dstfile, 'r', encoding='utf8') as f:
lines = f.readlines()
is_nav = False
for line in lines:
if 'nav:' in line:
is_nav = True
if is_nav:
nav_after_lines.append(line)
with open(dstfile, 'w', encoding='utf8') as f:
f.writelines(nav_before_lines)
f.writelines(nav_after_lines)
script_dir = r'D:\Blog\scripts'
lo, hi = 0, 3
if len(sys.argv) == 2:
lo = int(sys.argv[1])
hi = lo + 1
if len(sys.argv) == 3:
lo = int(sys.argv[1])
hi = int(sys.argv[2])
'''
生成手机端
'''
if lo <= 0 < hi:
dstdir = r'D:\BlogSite\masterAllen.github.io'
subprocess.run(
["python", "main.py"],
cwd=f'{script_dir}/web',
check=True
)
# 手机端:必须要删除 partials/comments.html
comments_pth = os.path.join(dstdir, 'overrides', 'partials', 'comments.html')
if os.path.exists(comments_pth):
os.remove(comments_pth)
replace_nav(os.path.join(script_dir, 'web', 'mkdocs-phone.yml'), os.path.join(dstdir, 'mkdocs.yml'))
subprocess.run(
["zensical", "build"],
cwd=dstdir,
check=True
)
# 在重命名前检测目标文件夹是否已存在,如果存在则先删除
site_src = os.path.join(dstdir, 'site')
site_dst = os.path.join(dstdir, 'site-phone')
if os.path.exists(site_dst):
# 删除内容
for item in os.listdir(site_dst):
item_path = os.path.join(site_dst, item)
if os.path.isdir(item_path):
shutil.rmtree(item_path)
else:
os.remove(item_path)
# 复制内容
for item in os.listdir(site_src):
s = os.path.join(site_src, item)
d = os.path.join(site_dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, dirs_exist_ok=True)
else:
shutil.copy2(s, d)
'''
生成 Web 端
'''
if lo <= 1 < hi:
dstdir = r'D:\BlogSite\masterAllen.github.io'
subprocess.run(
["python", "main.py"],
cwd=f'{script_dir}/web',
check=True
)
replace_nav(os.path.join(script_dir, 'web', 'mkdocs-web.yml'), os.path.join(dstdir, 'mkdocs.yml'))
subprocess.run(
["zensical", "build"],
cwd=dstdir,
check=True
)
# '''
# 生成本地端
# '''
# if lo <= 2 < hi:
# dstdir = r'D:\BlogSite\offline'
# subprocess.run(
# ["python", "main.py"],
# cwd=f'{script_dir}/local',
# check=True
# )
# subprocess.run(
# ["mkdocs", "build"],
# cwd=dstdir,
# check=True
# )