forked from USTC-Resource/USTC-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genIndex.py
172 lines (145 loc) · 4.91 KB
/
genIndex.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
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
#coding: utf-8
import os
import re
import markdown
import shutil
from getSize import getSize
from config import PATH, HTML, WALKDIR, TARDIR, IGNORE, NAME, DOWNLOAD
hasPinyin = False
try:
from pypinyin import pinyin
hasPinyin = True
except:
print('No module pypinyin, using defalut method to sort')
def pinyinSort(items):
if hasPinyin:
dic = {''.join(sum(pinyin(i, style=0), [])).lower(): i for i in items}
return [dic[i] for i in sorted(dic.keys())]
else:
print('No module pypinyin')
return items
def md2html(s):
exts = [
'markdown.extensions.extra', 'markdown.extensions.codehilite',
'markdown.extensions.tables', 'markdown.extensions.toc'
]
s = re.sub(r'\<\!--.*?--\>', '', s, flags=re.DOTALL)
return markdown.markdown(s, extensions=exts)
def getFmt():
dic = {
'file-audio': ['mp3', 'wave', 'snd', 'aif', 'wav'],
'file-video': ['mp4', 'avi', 'mov', 'swf'],
'file-archive': ['zip', 'rar', '7z', 'tar', 'gz', 'bz', 'jar', 'z'],
'file-word': ['doc', 'docx'],
'file-excel': ['xls', 'xlt'],
'file-powerpoint': ['ppt', 'pptx', 'pps', 'pptx', 'ppa', 'ppam'],
'file-pdf': ['pdf'],
'file-image': ['bmp', 'gif', 'png', 'jpg', 'jpeg', 'pic'],
'file-code': [
'c', 'o', 'h', 'sh', 'cc', 'm', 'cpp', 'py', 'lisp', 'scala',
'rust', 'java'
],
'file-import': ['md'],
}
FMT_DIC = {}
for i, li in dic.items():
for suf in li:
FMT_DIC[suf] = i
FMT_DIC['dir'] = 'folder'
FMT_DIC['other'] = 'file'
return FMT_DIC
FMT_DIC = getFmt()
def getIcon(name):
suf = name[name.rfind('.') + 1:]
return FMT_DIC[suf] if suf in FMT_DIC else FMT_DIC['other']
def prepare():
if os.path.exists(TARDIR):
os.system('rm -rf ' + TARDIR)
try:
os.mkdir(TARDIR)
with open(
os.path.join(TARDIR, '_config.yml'), 'w',
encoding='utf-8') as f:
f.write('theme: jekyll-theme-cayman\n')
except:
return
def handleDir(target):
prepare()
n = len(target)
gen = os.walk(target)
for path, dirs, files in gen:
dirs = [d for d in dirs if d not in IGNORE]
dirs = pinyinSort(dirs)
files = pinyinSort(files)
path = path[n:].strip(os.path.sep)
segs = path.split(os.path.sep)
if path.startswith('.') or any(seg in IGNORE for seg in segs): continue
tar = os.path.join(TARDIR, path)
if 'index.html' in files:
try:
shutil.copytree(path, tar)
except Exception as e:
print(e, path)
else:
genIndex(path, dirs, files)
def genIndex(path, dirs, files, htmlTemp=HTML):
md = ''
if 'README.md' in files:
with open(os.path.join(path, 'README.md'), 'r', errors='ignore') as f:
#<hr>\n<span style="color:orange;text-align:center;">Read Me</span>\n<hr>\n
md = '\n<h1 style="color:red;text-align:center;">Read Me</h1>\n' + f.read(
)
files.remove('README.md')
cur = getPath(path)
tar = os.path.join(TARDIR, path)
if not os.path.exists(tar): os.mkdir(tar)
dirLst = genDirectoryList(path, dirs)
fileLst = genFileList(path, files, tar)
cont = htmlTemp.format(
DOWNLOAD=DOWNLOAD + path,
cur=cur,
dirLst=dirLst,
fileLst=fileLst,
readme=md2html(md))
filename = os.path.join(tar, NAME)
with open(filename, 'w') as f:
f.write(cont)
def getPath(path):
lst = path.split(os.path.sep)
lst = lst[::-1]
lst.append('<i class="fas fa-home"></i>')
url = 'index.html'
res = []
for i in lst:
res.append('<a href="{url}">{txt}</a>'.format(url=url, txt=i))
url = '../' + url
return '/'.join(res[::-1])
LIITEM = '<li><a href="{path}"><i class="fas fa-{icon}"></i> {name}</a></li>'
def genFileList(path, files, tar=TARDIR):
files = [i for i in files if not i.startswith('.')]
link = {}
for k in files:
if k.endswith('.md'):
shutil.copy(os.path.join(path, k), tar)
link[k] = k[:-3] + '.html'
else:
link[k] = os.path.join(PATH, path, k)
lst = [
LIITEM.format(
icon=getIcon(key),
name=key + '---({})'.format(getSize(os.path.join(path, key))),
path=link[key]) for key in files
]
if lst == []: lst.append('<li><i class="fas fa-meh"></i> None</li>')
return '\n'.join(lst)
def genDirectoryList(path, dirs):
keys = [i for i in dirs if i[0] != '.']
link = {i: os.path.join(i, 'index.html') for i in keys if i[0] != '.'}
lst = [
LIITEM.format(icon=FMT_DIC['dir'], name=key, path=link[key])
for key in keys
]
if lst == []: lst.append('<li><i class="fas fa-meh"></i> None</li>')
return '\n'.join(lst)
if __name__ == '__main__':
handleDir(WALKDIR)