-
Notifications
You must be signed in to change notification settings - Fork 2
/
genIndex.py
54 lines (42 loc) · 1.39 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
import os
import re
FOLDER = './source/_posts'
TITLE = re.compile(r'title: +(.*?)\n')
DATE = re.compile(r'date: +(\d{4})-(\d{1,2})-(\d{1,2})')
PRE = 'https://github.com/mbinary/mbinary.github.io/tree/hexo/source/_posts/'
BLOG = 'https://mbinary.github.io/'
def parseHeader(path):
cont = open(path).read()
date = title = ''
m = TITLE.search(cont)
if m:
title = m.group(1)
else:
name = os.path.basename(path)
title = name.rstrip('.md')
match = DATE.search(cont)
if match:
y, m, d = match.groups()
if len(m) < 2:
m = '0' + m
if len(d) < 2:
d = '0' + d
date =f'{y}-{m}-{d}'
return title, date
def genIndex(folder=FOLDER):
names = os.listdir(folder)
li = []
for name in names:
title, date = parseHeader(os.path.join(folder, name))
title = re.sub(r'[\(\[]', '{', title)
title = re.sub(r'[\)\]]', '}', title)
title = re.sub(r'[\*]', '-', title)
li.append((date, title, name.rstrip('.md')))
li.sort(reverse=True)
return '\n'.join(f'- {date}: {title}--[GitHub 网页]({PRE+name}.md),[博客网页]({BLOG+name}.html)' for date, title, name in li)
if __name__ == '__main__':
with open('README.md', 'w') as f:
cont = ''
with open('README.md.tpl') as f2:
cont = f2.read()
f.write(cont.format(index=genIndex()))