forked from totoroyyw/USTC.Courses.Resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genIndex.py
119 lines (105 loc) · 4.18 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
#coding: utf-8
import os
import markdown
import shutil
from getSize import getSize
from config import PATH,HTML,WALKDIR,TARDIR,IGNORE
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']
return markdown.markdown(s,extensions=exts)
def getFmt():
dic={}
sound_suf = ['file-sound-o',['mp3','wave','snd','aif','wav']]
movie_suf =['file-movie-o', ['mp4','avi','mov','swf']]
zip_suf = ['file-zip-o',['zip','rar','7z','tar','gz','bz','jar','z']]
word_suf=['file-word-o',['doc','docx']]
excel_suf=['file-excelo',['xls','xlt']]
ppt_suf = ['file-powerpoint-o',['ppt','pptx','pps','pptx','ppa','ppam']]
pdf_suf = ['file-pdf-o',['pdf']]
pic_suf =['file-picture-o',['bmp','gif','png','jpg','jpeg','pic']]
code_suf=['file-code-o',['c','o','h','sh','cc','m','cpp','py','lisp','scala','rust','java']]
lst_suf=[sound_suf,movie_suf,zip_suf,word_suf,excel_suf,ppt_suf,pdf_suf,pic_suf,code_suf]
for lst in lst_suf:
suf, li = lst
for i in li:
dic[i]=suf
dic['dir'] = 'folder'
dic['other']='pencil-square-o'
return 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 handleDir(target):
if os.path.exists(TARDIR):
os.system('rm -rf '+TARDIR)
try:os.mkdir(TARDIR)
except:return
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()
cur = getPath(path)
dirLst = genDirectoryList(path,dirs)
fileLst = genFileList(path,files)
cont = htmlTemp.format(cur=cur,dirLst = dirLst,fileLst = fileLst,readme=md2html(md))
tar = os.path.join(TARDIR ,path)
if not os.path.exists(tar):os.mkdir(tar)
filename = os.path.join(tar, 'index.html')
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="fa 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="fa fa-{icon}"></i> {name}</a></li>'
def genFileList(path,files):
keys=[i for i in files if i[0]!='.']
link= {i:os.path.join(path,i) for i in keys}
lst = [LIITEM.format(icon=getIcon(key),name = key+'---({})'.format(getSize(link[key])),path = os.path.join(PATH,link[key])) for key in keys]
if lst==[]: lst.append('<li><i class="fa fa-meh-o"></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="fa fa-meh-o"></i> None</li>')
return '\n'.join(lst)
if __name__ =='__main__':
handleDir(WALKDIR)