-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_readme.py
executable file
·114 lines (76 loc) · 2.54 KB
/
generate_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
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
113
114
#!/usr/bin/env python
import os
import re
from operator import attrgetter
from typing import List, Optional
from dataclasses import dataclass
@dataclass
class Note:
filepath: str
category: str
title: str
created_at: str
def get_title(filepath: str) -> str:
with open(filepath) as note:
return next(note).strip("#").strip()
def get_date(file: str) -> Optional[str]:
match = re.search(r"(\d{4}-\d{2}-\d{2})", file)
if match:
return match.group(0)
return ""
def get_notes() -> List[Note]:
results = []
for root, dirs, files in os.walk("."):
if root == ".":
for dir_ in list(dirs):
if dir_.startswith("."):
dirs.remove(dir_)
continue
for file in files:
if not file.endswith(".md"):
continue
basedir = os.path.basename(root)
filepath = os.path.join(basedir, file)
results.append(
Note(
filepath=filepath,
created_at=get_date(file),
category=basedir,
title=get_title(filepath),
)
)
return results
def sort_notes(notes: List[Note]) -> List[Note]:
title_asc = sorted(notes, key=attrgetter("title"))
created_at_desc = sorted(title_asc, key=attrgetter("created_at"), reverse=True)
category_asc = sorted(created_at_desc, key=attrgetter("category"))
return category_asc
def generate_header() -> str:
with open(".notes/header.md", "r") as content_file:
return content_file.read()
def generate_category(category: str) -> str:
return f"\n### {category}\n\n"
def generate_item(note: Note) -> str:
if note.created_at:
return f"- [{note.title.lower()}]({note.filepath}) - {note.created_at}\n"
return f"- [{note.title.lower()}]({note.filepath})\n"
def generate_footer() -> str:
with open(".notes/footer.md", "r") as content_file:
return content_file.read()
def generate_readme_content(notes: List[Note]):
content = generate_header()
category = None
for note in notes:
if note.category != category:
category = note.category
content += generate_category(category)
content += generate_item(note)
content += generate_footer()
return content
def main():
notes = sort_notes(get_notes())
content = generate_readme_content(notes)
with open("README.md", "w") as readme:
readme.write(content)
if __name__ == "__main__":
main()