|
4 | 4 | import markdown
|
5 | 5 | import shutil
|
6 | 6 | from getSize import getSize
|
7 |
| -from config import PATH,HTML,WALKDIR,TARDIR,IGNORE,NAME,DOWNLOAD |
| 7 | +from config import PATH, HTML, WALKDIR, TARDIR, IGNORE, NAME, DOWNLOAD |
8 | 8 | hasPinyin = False
|
9 | 9 | try:
|
10 | 10 | from pypinyin import pinyin
|
11 | 11 | hasPinyin = True
|
12 | 12 | except:
|
13 | 13 | print('No module pypinyin, using defalut method to sort')
|
14 | 14 |
|
| 15 | + |
15 | 16 | def pinyinSort(items):
|
16 | 17 | if hasPinyin:
|
17 |
| - dic = {''.join(sum(pinyin(i,style=0),[])).lower():i for i in items} |
| 18 | + dic = {''.join(sum(pinyin(i, style=0), [])).lower(): i for i in items} |
18 | 19 | return [dic[i] for i in sorted(dic.keys())]
|
19 | 20 | else:
|
20 | 21 | print('No module pypinyin')
|
21 | 22 | return items
|
22 | 23 |
|
| 24 | + |
23 | 25 | def md2html(s):
|
24 |
| - exts = ['markdown.extensions.extra', 'markdown.extensions.codehilite','markdown.extensions.tables','markdown.extensions.toc'] |
25 |
| - s = re.sub(r'\<\!--.*?--\>','',s,flags=re.DOTALL) |
26 |
| - return markdown.markdown(s,extensions=exts) |
| 26 | + exts = [ |
| 27 | + 'markdown.extensions.extra', 'markdown.extensions.codehilite', |
| 28 | + 'markdown.extensions.tables', 'markdown.extensions.toc' |
| 29 | + ] |
| 30 | + s = re.sub(r'\<\!--.*?--\>', '', s, flags=re.DOTALL) |
| 31 | + return markdown.markdown(s, extensions=exts) |
| 32 | + |
27 | 33 |
|
28 | 34 | def getFmt():
|
29 |
| - dic={} |
30 |
| - sound_suf = ['file-sound-o',['mp3','wave','snd','aif','wav']] |
31 |
| - movie_suf =['file-movie-o', ['mp4','avi','mov','swf']] |
32 |
| - zip_suf = ['file-zip-o',['zip','rar','7z','tar','gz','bz','jar','z']] |
33 |
| - word_suf=['file-word-o',['doc','docx']] |
34 |
| - excel_suf=['file-excelo',['xls','xlt']] |
35 |
| - ppt_suf = ['file-powerpoint-o',['ppt','pptx','pps','pptx','ppa','ppam']] |
36 |
| - pdf_suf = ['file-pdf-o',['pdf']] |
37 |
| - pic_suf =['file-picture-o',['bmp','gif','png','jpg','jpeg','pic']] |
38 |
| - code_suf=['file-code-o',['c','o','h','sh','cc','m','cpp','py','lisp','scala','rust','java']] |
39 |
| - lst_suf=[sound_suf,movie_suf,zip_suf,word_suf,excel_suf,ppt_suf,pdf_suf,pic_suf,code_suf] |
| 35 | + dic = {} |
| 36 | + sound_suf = ['file-audio', ['mp3', 'wave', 'snd', 'aif', 'wav']] |
| 37 | + movie_suf = ['file-video', ['mp4', 'avi', 'mov', 'swf']] |
| 38 | + zip_suf = [ |
| 39 | + 'file-archive', ['zip', 'rar', '7z', 'tar', 'gz', 'bz', 'jar', 'z'] |
| 40 | + ] |
| 41 | + word_suf = ['file-word', ['doc', 'docx']] |
| 42 | + excel_suf = ['file-excel', ['xls', 'xlt']] |
| 43 | + ppt_suf = [ |
| 44 | + 'file-powerpoint', ['ppt', 'pptx', 'pps', 'pptx', 'ppa', 'ppam'] |
| 45 | + ] |
| 46 | + pdf_suf = ['file-pdf', ['pdf']] |
| 47 | + pic_suf = ['file-image', ['bmp', 'gif', 'png', 'jpg', 'jpeg', 'pic']] |
| 48 | + code_suf = [ |
| 49 | + 'file', |
| 50 | + [ |
| 51 | + 'c', 'o', 'h', 'sh', 'cc', 'm', 'cpp', 'py', 'lisp', 'scala', |
| 52 | + 'rust', 'java' |
| 53 | + ] |
| 54 | + ] |
| 55 | + lst_suf = [ |
| 56 | + sound_suf, movie_suf, zip_suf, word_suf, excel_suf, ppt_suf, pdf_suf, |
| 57 | + pic_suf, code_suf |
| 58 | + ] |
40 | 59 |
|
41 | 60 | for lst in lst_suf:
|
42 | 61 | suf, li = lst
|
43 | 62 | for i in li:
|
44 |
| - dic[i]=suf |
| 63 | + dic[i] = suf |
45 | 64 | dic['dir'] = 'folder'
|
46 |
| - dic['other']='pencil-square-o' |
| 65 | + dic['other'] = 'pencil-square-o' |
47 | 66 | return dic
|
48 | 67 |
|
| 68 | + |
49 | 69 | FMT_DIC = getFmt()
|
50 | 70 |
|
| 71 | + |
51 | 72 | def getIcon(name):
|
52 |
| - suf=name[name.rfind('.')+1:] |
| 73 | + suf = name[name.rfind('.') + 1:] |
53 | 74 | return FMT_DIC[suf] if suf in FMT_DIC else FMT_DIC['other']
|
54 | 75 |
|
| 76 | + |
55 | 77 | def prepare():
|
56 | 78 | if os.path.exists(TARDIR):
|
57 |
| - os.system('rm -rf '+TARDIR) |
| 79 | + os.system('rm -rf ' + TARDIR) |
58 | 80 | try:
|
59 | 81 | os.mkdir(TARDIR)
|
60 |
| - with open(os.path.join(TARDIR,'_config.yml'),'w',encoding='utf-8') as f: |
| 82 | + with open( |
| 83 | + os.path.join(TARDIR, '_config.yml'), 'w', |
| 84 | + encoding='utf-8') as f: |
61 | 85 | f.write('theme: jekyll-theme-cayman\n')
|
62 |
| - except:return |
| 86 | + except: |
| 87 | + return |
| 88 | + |
| 89 | + |
63 | 90 | def handleDir(target):
|
64 | 91 | prepare()
|
65 | 92 | n = len(target)
|
66 | 93 | gen = os.walk(target)
|
67 |
| - for path,dirs,files in gen: |
| 94 | + for path, dirs, files in gen: |
68 | 95 | dirs = [d for d in dirs if d not in IGNORE]
|
69 | 96 | dirs = pinyinSort(dirs)
|
70 | 97 | files = pinyinSort(files)
|
71 | 98 | path = path[n:].strip(os.path.sep)
|
72 | 99 | segs = path.split(os.path.sep)
|
73 |
| - if path.startswith('.') or any(seg in IGNORE for seg in segs) :continue |
74 |
| - tar = os.path.join(TARDIR ,path) |
| 100 | + if path.startswith('.') or any(seg in IGNORE for seg in segs): continue |
| 101 | + tar = os.path.join(TARDIR, path) |
75 | 102 | if 'index.html' in files:
|
76 |
| - try:shutil.copytree(path,tar) |
| 103 | + try: |
| 104 | + shutil.copytree(path, tar) |
77 | 105 | except Exception as e:
|
78 |
| - print(e,path) |
79 |
| - else: genIndex(path,dirs,files) |
| 106 | + print(e, path) |
| 107 | + else: |
| 108 | + genIndex(path, dirs, files) |
| 109 | + |
80 | 110 |
|
81 |
| -def genIndex(path,dirs,files,htmlTemp = HTML): |
| 111 | +def genIndex(path, dirs, files, htmlTemp=HTML): |
82 | 112 | md = ''
|
83 | 113 | if 'README.md' in files:
|
84 |
| - with open(os.path.join(path,'README.md'),'r',errors='ignore') as f : |
| 114 | + with open(os.path.join(path, 'README.md'), 'r', errors='ignore') as f: |
85 | 115 | #<hr>\n<span style="color:orange;text-align:center;">Read Me</span>\n<hr>\n
|
86 |
| - md = '\n<h1 style="color:red;text-align:center;">Read Me</h1>\n'+f.read() |
| 116 | + md = '\n<h1 style="color:red;text-align:center;">Read Me</h1>\n' + f.read( |
| 117 | + ) |
87 | 118 | files.remove('README.md')
|
88 | 119 | cur = getPath(path)
|
89 |
| - tar = os.path.join(TARDIR ,path) |
90 |
| - if not os.path.exists(tar):os.mkdir(tar) |
| 120 | + tar = os.path.join(TARDIR, path) |
| 121 | + if not os.path.exists(tar): os.mkdir(tar) |
91 | 122 |
|
92 |
| - dirLst = genDirectoryList(path,dirs) |
93 |
| - fileLst = genFileList(path,files,tar) |
94 |
| - cont = htmlTemp.format(DOWNLOAD=DOWNLOAD+path,cur=cur,dirLst = dirLst,fileLst = fileLst,readme=md2html(md)) |
| 123 | + dirLst = genDirectoryList(path, dirs) |
| 124 | + fileLst = genFileList(path, files, tar) |
| 125 | + cont = htmlTemp.format( |
| 126 | + DOWNLOAD=DOWNLOAD + path, |
| 127 | + cur=cur, |
| 128 | + dirLst=dirLst, |
| 129 | + fileLst=fileLst, |
| 130 | + readme=md2html(md)) |
95 | 131 | filename = os.path.join(tar, NAME)
|
96 |
| - with open(filename,'w') as f: |
| 132 | + with open(filename, 'w') as f: |
97 | 133 | f.write(cont)
|
98 | 134 |
|
| 135 | + |
99 | 136 | def getPath(path):
|
100 | 137 | lst = path.split(os.path.sep)
|
101 | 138 | lst = lst[::-1]
|
102 |
| - lst.append('home <i class="fa fa-home"></i>') |
| 139 | + lst.append('<i class="fa fa-home"></i>') |
103 | 140 | url = 'index.html'
|
104 | 141 | res = []
|
105 | 142 | for i in lst:
|
106 |
| - res.append('<a href="{url}">{txt}</a>'.format(url = url,txt = i)) |
107 |
| - url='../'+url |
| 143 | + res.append('<a href="{url}">{txt}</a>'.format(url=url, txt=i)) |
| 144 | + url = '../' + url |
108 | 145 | return '/'.join(res[::-1])
|
109 | 146 |
|
| 147 | + |
110 | 148 | LIITEM = '<li><a href="{path}"><i class="fa fa-{icon}"></i> {name}</a></li>'
|
111 |
| -def genFileList(path,files,tar = TARDIR): |
| 149 | + |
| 150 | + |
| 151 | +def genFileList(path, files, tar=TARDIR): |
112 | 152 | files = [i for i in files if not i.startswith('.')]
|
113 | 153 | link = {}
|
114 | 154 | for k in files:
|
115 | 155 | if k.endswith('.md'):
|
116 |
| - shutil.copy(os.path.join(path,k),tar) |
| 156 | + shutil.copy(os.path.join(path, k), tar) |
117 | 157 | link[k] = k[:-3] + '.html'
|
118 | 158 | else:
|
119 |
| - link[k] = os.path.join(PATH,path,k) |
120 |
| - lst = [LIITEM.format(icon=getIcon(key),name = key+'---({})'.format(getSize(os.path.join(path,key))),path = link[key]) for key in files] |
121 |
| - if lst==[]: lst.append('<li><i class="fa fa-meh-o"></i> None</li>') |
| 159 | + link[k] = os.path.join(PATH, path, k) |
| 160 | + lst = [ |
| 161 | + LIITEM.format( |
| 162 | + icon=getIcon(key), |
| 163 | + name=key + '---({})'.format(getSize(os.path.join(path, key))), |
| 164 | + path=link[key]) for key in files |
| 165 | + ] |
| 166 | + if lst == []: lst.append('<li><i class="fa fa-meh-o"></i> None</li>') |
122 | 167 | return '\n'.join(lst)
|
123 | 168 |
|
124 |
| -def genDirectoryList(path,dirs): |
125 |
| - keys=[i for i in dirs if i[0]!='.'] |
126 |
| - link = {i:os.path.join(i,'index.html') for i in keys if i[0]!='.'} |
127 |
| - lst = [LIITEM.format(icon=FMT_DIC['dir'],name = key,path =link[key]) for key in keys] |
128 |
| - if lst==[]: lst.append('<li><i class="fa fa-meh-o"></i> None</li>') |
| 169 | + |
| 170 | +def genDirectoryList(path, dirs): |
| 171 | + keys = [i for i in dirs if i[0] != '.'] |
| 172 | + link = {i: os.path.join(i, 'index.html') for i in keys if i[0] != '.'} |
| 173 | + lst = [ |
| 174 | + LIITEM.format(icon=FMT_DIC['dir'], name=key, path=link[key]) |
| 175 | + for key in keys |
| 176 | + ] |
| 177 | + if lst == []: lst.append('<li><i class="fa fa-meh-o"></i> None</li>') |
129 | 178 | return '\n'.join(lst)
|
130 | 179 |
|
131 |
| -if __name__ =='__main__': |
| 180 | + |
| 181 | +if __name__ == '__main__': |
132 | 182 | handleDir(WALKDIR)
|
0 commit comments