-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_index.py
executable file
·53 lines (40 loc) · 1.42 KB
/
gen_index.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
#!/usr/bin/env python3
import time
import os
import sys
from typing import Dict, List
def main(basedir, title, version):
header = f"""---
layout: doc
title: {title}
---
"""
cats = sorted(os.listdir("./%s" % basedir))
if 'index.md' in cats:
cats.remove('index.md')
all_md_files = [x for x in cats if x.endswith('.md')]
# if all files are .md files, then we are in a category page directory
if len(all_md_files) == len(cats):
with open(f"./{basedir}/index.md", 'w') as f:
f.write(header)
for cat in cats:
f.write(f"* [{cat[:-3]}]({cat[:-3]}.html)" + "\n")
return
category_efun_map: Dict[str, List[str]] = {}
for cat in cats:
if not '.md' in cat:
category_efun_map[cat] = os.listdir('./%s/%s' % (basedir, cat))
with open(f"./{basedir}/index.md", 'w') as f:
f.write(header)
for cat in sorted(category_efun_map.keys()):
f.write(f"## {cat}" + "\n")
# remove ".md" suffix
items = sorted([x[:-3] for x in category_efun_map[cat] if x.endswith('.md')])
for item in items:
if item != 'index':
f.write(f"* [{item}]({cat}/{item}.html)" + "\n")
# build index.md for subdir
for cat in sorted(category_efun_map.keys()):
main(basedir + "/" + cat, cat, version)
if __name__ == '__main__':
main(*sys.argv[1:])