-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmkdocs.py
executable file
·86 lines (71 loc) · 3.18 KB
/
mkdocs.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# modified from https://github.com/conanhujinming/comments-for-awesome-courses/blob/main/update.py
import os
import yaml
from urllib.parse import quote
TXT_EXTS = ['md', 'txt']
EXCLUDE_DIRS = ['.git', 'docs', '.vscode', '.circleci', 'site', 'overrides', '.github']
README_MD = ['README.md', 'readme.md', 'index.md']
TXT_URL_PREFIX = 'https://github.com/TJ-CSCCG/TJCS-Course/blob/master/'
BIN_URL_PREFIX = 'https://github.com/TJ-CSCCG/TJCS-Course/raw/master/'
def list_files(course: str):
filelist_texts = '\n## 文件列表\n'
readme_path = ''
file_cnt = 0
for root, dirs, files in os.walk(course):
files.sort()
level = root.replace(course, '').count(os.sep)
indent = ' ' * 4 * level
filelist_texts += '{}- {}\n'.format(indent, os.path.basename(root))
subindent = ' ' * 4 * (level + 1)
for f in files:
if f not in README_MD:
file_cnt += 1
if f.split('.')[-1] in TXT_EXTS:
filelist_texts += '{}- [{}]({})\n'.format(subindent,
f, TXT_URL_PREFIX + quote('{}/{}'.format(root, f)))
else:
filelist_texts += '{}- [{}]({})\n'.format(subindent,
f, BIN_URL_PREFIX + quote('{}/{}'.format(root, f)))
elif root == course and readme_path == '':
readme_path = '{}/{}'.format(root, f)
if file_cnt == 0:
return '', readme_path
return filelist_texts, readme_path
def generate_md(term: str, course: str, readme_path: str, filelist_texts: str):
final_texts = ['\n'.encode(), filelist_texts.encode()]
if readme_path:
with open(readme_path, 'rb') as file:
final_texts = file.readlines() + final_texts
term_path = os.path.join('docs', term)
if not os.path.isdir(term_path):
os.mkdir(term_path)
with open(os.path.join(term_path, '{}.md'.format(course)), 'wb') as file:
file.writelines(final_texts)
if __name__ == '__main__':
if not os.path.isdir('docs'):
os.mkdir('docs')
# read courses from terms.yml
with open('terms.yml', 'r') as file:
yaml_data = yaml.safe_load(file)
terms = yaml_data.keys()
for term in terms:
courses = yaml_data[term]
for course in courses:
filelist_texts, readme_path = list_files(course)
generate_md(term, course, readme_path, filelist_texts)
# use main README.md as index.md
with open('README.md', 'rb') as file:
mainreadme_lines = file.readlines()
with open('docs/index.md', 'wb') as file:
file.writelines(mainreadme_lines)
# handle navigation part. if not specified, '大三上' will be ahead of '大二上', weird
with open ('mkdocs.yml', 'a') as file:
print('nav:', file=file)
print(' - Home: index.md', file=file)
for term in terms:
print(' - {}:'.format(term), file=file)
courses = yaml_data[term]
for course in courses:
print(' - {}: {}/{}.md'.format(course.replace('_', ' '), term, course), file=file)