forked from satwikkansal/wtfpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_readme.py
More file actions
144 lines (112 loc) · 3.92 KB
/
parse_readme.py
File metadata and controls
144 lines (112 loc) · 3.92 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
"""
This inefficient module would parse the README.md in the initial version of
WTFPython, and enabl me to categorize and reorder a hell lot of examples with
the help of the file `add_categories` (part of which is automatically
generated).
After the refactor, this module would not work now with necessary updates in
the code.
"""
fname = "README.md"
snippets = []
with open(fname, 'r') as f:
lines = iter(f.readlines())
line = lines.next()
try:
while True:
# check if it's a H3
if line.startswith("### "):
title = line.replace("### ", "")
description = []
next_line = lines.next()
# store lines till an H4 (explanation) is encountered
while not next_line.startswith("#### "):
description.append(next_line)
next_line = lines.next()
explanation = []
# store lines again until --- or another H3 is encountered
while not (next_line.startswith("---") or
next_line.startswith("### ")):
explanation.append(next_line)
next_line = lines.next()
# Store the results finally
snippets.append({
"title": title,
"description": '\n'.join(description),
"explanation": '\n'.join(explanation)
})
line = next_line
else:
line = lines.next()
except StopIteration:
snippets.append({
"title": title,
"description": '\n'.join(description),
"explanation": '\n'.join(explanation)
})
'''
# Create a file
file_content = "\n\n".join([snip["title"] for snip in snippets])
with open("add_categories", "w") as f:
f.write(file_content)
'''
snips_by_title = {}
with open("add_categories", "r") as f:
content = iter(f.readlines())
try:
while True:
title = content.next()
cat = content.next().strip()
is_new = True if cat[-1]=="*" else False
cat = cat.replace('*','')
snips_by_title[title] = {
"category": cat,
"is_new": is_new
}
content.next()
except StopIteration:
for idx, snip in enumerate(snippets):
snippets[idx]["category"] = snips_by_title[snip["title"]]["category"]
snippets[idx]["is_new"] = snips_by_title[snip["title"]]["is_new"]
snips_by_cat = {}
for snip in snippets:
cat = snip["category"]
if not snips_by_cat.get(cat):
snips_by_cat[cat] = []
snips_by_cat[cat].append(snip)
snippet_template = """
### ▶ {title}{is_new}
{description}
{explanation}
---
"""
category_template = """
---
## {category}
{content}
"""
result = ""
category_names = {
"a": "Appearances are Deceptive!",
"t": "The Hiddent treasures",
"f": "Strain your Brain",
"c": "Be careful of these",
"m": "Miscallaneous"
}
categories_in_order = ["a", "t", "f", "c", "m"]
for category in categories_in_order:
snips = snips_by_cat[category]
for i, snip in enumerate(snips):
print(i, ":", snip["title"])
content = ""
for _ in snips:
snip = snips[int(raw_input())]
is_new = " *" if snip["is_new"] else ""
content += snippet_template.format(title=snip["title"].strip(),
is_new=is_new,
description=snip["description"].strip().replace("\n\n", "\n"),
explanation=snip["explanation"].strip().replace("\n\n", "\n"))
result += category_template.format(category=category_names[category], content=content.replace("\n\n\n", "\n\n"))
with open("generated.md", "w") as f:
f.write(result.replace("\n\n\n", "\n\n"))
print("Done!")