-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathupdate_index.py
112 lines (85 loc) · 3.31 KB
/
update_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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'''
File that generates the config for navigation and the index page
'''
import os
import sys
import yaml
DOCS_DIR = './docs'
def get_entry(path):
with open(path, 'r') as file_stream:
yaml_part = ''
for count, line in enumerate(file_stream):
if count < 6:
yaml_part += line
if count > 5:
break
try:
yaml_doc = yaml.load(
yaml_part,
Loader=yaml.SafeLoader
)
except yaml.YAMLError as exc:
print(f'Problem with Meta: {path}')
sys.exit(1)
return yaml_doc
def main():
categories = {}
all_entries = []
for root, dirs, files in os.walk(DOCS_DIR):
for dir_ in dirs:
if root == './docs/img':
continue
categories[dir_] = []
with os.scandir(os.path.join(DOCS_DIR, dir_)) as it:
for entry in it:
if entry.name.endswith(".md") and entry.is_file():
#print(entry.name, dir_)
rec = get_entry(entry.path)
if isinstance(rec, dict):
date = rec.get('date')
title = rec.get('title')
summary = rec.get('summary')
category = rec.get('category')
else:
raise ValueError(f'{entry.path} has no front matter')
record = {
'date': date,
'title': title,
'summary': summary,
'category': category,
'file': entry.path
}
categories[dir_].append(record)
all_entries.append(record)
# print(date, title, summary, category)
for entry in all_entries:
if not entry.get('date'):
print('No date: ', entry)
newlist = sorted(all_entries, key=lambda k: k['date'], reverse=True)
category_keys = list(categories.keys())
category_keys = sorted(category_keys)
the_10_most_recent = newlist[0:10]
## Open a file for writing
with open(os.path.join(DOCS_DIR, 'index.md'), 'w') as f:
f.write('# Home\n\n')
f.write('A repo of documentation, notes, summaries, fixes and solutions on software development and related topics\n\n')
f.write('## Most Recent Posts\n\n')
for item in the_10_most_recent:
f.write(f'- { item["date"] }: _{ item["category"] }_ [{ item["title"] }]({ item["file"][7:] })\n')
f.write('\n## Table of Contents\n\n')
f.write('[TOC]\n\n')
main_topics = []
for key in category_keys:
if key == 'img':
continue
records = categories[key]
# sort records by date
records = sorted(records, key=lambda k: k['date'], reverse=True)
f.write(f'## { key.title() }\n')
for item in records:
f.write(f'- { item["date"] }: [{ item["title"] }]({ item["file"][7:] })\n')
f.write('\n')
if len(records) > 12:
main_topics.append(key)
if __name__ == '__main__':
main()