-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_readme.py
56 lines (41 loc) · 1.96 KB
/
gen_readme.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
import os
import frontmatter
POST_DIR = './content/posts/'
DOMAIN = 'https://xwlearn.com'
# 递归获取提供目录下所有文件
def list_all_files(root_path, ignore_dirs=[]):
files = []
default_dirs = [".git", ".obsidian", ".config"]
ignore_dirs.extend(default_dirs)
for parent, dirs, filenames in os.walk(root_path):
dirs[:] = [d for d in dirs if not d in ignore_dirs]
filenames = [f for f in filenames if not f[0] == '.']
for file in filenames:
if file.endswith(".md"):
files.append(file)
return sorted(files,reverse=True)
if __name__ == '__main__':
posts = list_all_files(POST_DIR)
# post 字典列表
post_list = []
# 获取文章元数据
for post in posts:
temp_dict = {}
with open(POST_DIR + post, 'r', encoding='utf-8') as f:
content = frontmatter.loads(f.read())
if content.metadata.get('draft', False) == True:
continue
temp_dict['title'] = content.metadata['title']
split_post = post.split('-',3)
temp_dict['date'] = '-'.join(split_post[:3])
temp_dict['slug'] = split_post[-1].rstrip('.md')
post_list.append(temp_dict)
# 写入README
with open('README.md', 'w', encoding='utf-8') as f:
header = "## [习吾学]({})\n\n[![](https://img.shields.io/badge/blog-xwlearn-green.svg)](https://xwlearn.com) [![](https://github.com/imxw/xwlearn/workflows/CI/badge.svg)](https://github.com/imxw/xwlearn/actions/workflows/main.yml) [![](https://github.com/imxw/xwlearn/workflows/Generate%20README/badge.svg)](https://github.com/imxw/xwlearn/actions/workflows/readme.yml) \n\n".format(DOMAIN)
format_posts = []
for post in post_list:
temp = "[{}]({}/{})--{}\n".format(post['title'], DOMAIN, post['slug'], post['date'])
format_posts.append(temp)
body = "## 所有文章\n" + '- ' + '- '.join(format_posts)
f.write(header + body)