-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_readme.py
102 lines (75 loc) · 2.31 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
from functools import reduce
import os
import re
path_to_template = "./README-TEMPLATE.md"
problem_template = """<li>
<a href="{2}/{1}">
{0}
</a> - {3}
</li>"""
topic_template = """<details>
<summary>{}</summary>
<ul>
{}
</ul>
</details>"""
dirs_to_exclude = [".git", ".github"]
data = []
readme_template = ""
problems_count = 0
def to_title_case(str):
if len(str) == 0:
return str
new_str = re.sub("-", " ", str)
return re.sub(r"\b\w", lambda match: match.group(0).upper(), new_str)
def to_kebab_case(str):
if len(str) == 0:
return str
new_str = re.sub(r"\b\w", lambda match: match.group(0).lower(), str)
return re.sub(r"\W", "-", new_str)
def get_difficulty(dir):
with open(dir) as p:
match = re.search(r"(?<=!\[)\w+(?=\])", p.read())
if match == None:
return ""
return match.group(0)
def map_problem(map, problem):
global problems_count
key = problem["topic"]
if key not in map:
map[key] = []
map[key].append((problem["name"], problem["diff"]))
problems_count += 1
return map
dirs = os.listdir(os.getcwd())
for dir in dirs:
if not os.path.isdir(dir) or dir in dirs_to_exclude:
continue
problems = os.listdir(os.path.join(os.getcwd(), dir))
for problem in problems:
diff = get_difficulty(os.path.join(os.getcwd(), dir, problem, "README.md"))
p_map = {
"name": to_title_case(problem),
"topic": to_title_case(dir),
"diff": diff,
}
data.append(p_map)
with open(path_to_template) as template_file:
readme_template = template_file.read()
problems_per_topic = reduce(map_problem, data, {})
problemsHTML = ""
for topic in problems_per_topic:
list_of_problems = problems_per_topic[topic]
all_problems_for_topic = reduce(
lambda html, problem: html
+ problem_template.format(
problem[0], to_kebab_case(problem[0]), to_kebab_case(topic), problem[1]
),
list_of_problems,
"",
)
problemsHTML += topic_template.format(topic, all_problems_for_topic)
new_readme = re.sub("\$\{problems\}", problemsHTML, readme_template)
new_readme = re.sub("\$\{number-of-problems\}", str(problems_count), new_readme)
with open("README.md", "w") as r:
r.write(new_readme)