Skip to content

Commit e58700d

Browse files
committed
Implement parser for categorizing, reordering and restructuring.
In short, a major refactor before 2.0
1 parent 3b956ca commit e58700d

File tree

1 file changed

+83
-7
lines changed

1 file changed

+83
-7
lines changed

parse_readme.py

+83-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import pprint
2-
1+
# -*- coding: utf-8 -*-
32
fname = "README.md"
43
snippets = []
54

@@ -52,13 +51,90 @@
5251
"explanation": '\n'.join(explanation)
5352
})
5453

55-
# separating by category
56-
categories = ["a", "b", "c"]
54+
'''
55+
# Create a file
56+
file_content = "\n\n".join([snip["title"] for snip in snippets])
57+
58+
with open("add_categories", "w") as f:
59+
f.write(file_content)
60+
'''
61+
62+
snips_by_title = {}
5763

58-
snips_by_cat = {k:[] for k in categories}
64+
with open("add_categories", "r") as f:
65+
content = iter(f.readlines())
66+
try:
67+
while True:
68+
title = content.next()
69+
cat = content.next().strip()
70+
is_new = True if cat[-1]=="*" else False
71+
cat = cat.replace('*','')
72+
snips_by_title[title] = {
73+
"category": cat,
74+
"is_new": is_new
75+
}
76+
content.next()
77+
except StopIteration:
78+
print("Done!")
5979

80+
for idx, snip in enumerate(snippets):
81+
snippets[idx]["category"] = snips_by_title[snip["title"]]["category"]
82+
snippets[idx]["is_new"] = snips_by_title[snip["title"]]["is_new"]
83+
84+
85+
snips_by_cat = {}
6086
for snip in snippets:
61-
cat = raw_input(snip["title"])
87+
cat = snip["category"]
88+
if not snips_by_cat.get(cat):
89+
snips_by_cat[cat] = []
6290
snips_by_cat[cat].append(snip)
6391

64-
pprint.pprint("hail", snippets)
92+
snippet_template = """
93+
94+
### ▶ {title}{is_new}
95+
96+
{description}
97+
98+
{explanation}
99+
100+
---
101+
"""
102+
103+
category_template = """
104+
---
105+
106+
## {category}
107+
108+
{content}
109+
"""
110+
111+
result = ""
112+
113+
category_names = {
114+
"a": "Appearances are Deceptive!",
115+
"t": "The Hiddent treasures",
116+
"f": "Strain your Brain",
117+
"c": "Be careful of these",
118+
"m": "Miscallaneous"
119+
}
120+
121+
categories_in_order = ["a", "t", "f", "c", "m"]
122+
123+
for category in categories_in_order:
124+
snips = snips_by_cat[category]
125+
for i, snip in enumerate(snips):
126+
print(i, ":", snip["title"])
127+
content = ""
128+
for _ in snips:
129+
snip = snips[int(raw_input())]
130+
is_new = " *" if snip["is_new"] else ""
131+
content += snippet_template.format(title=snip["title"].strip(),
132+
is_new=is_new,
133+
description=snip["description"].strip().replace("\n\n", "\n"),
134+
explanation=snip["explanation"].strip().replace("\n\n", "\n"))
135+
result += category_template.format(category=category_names[category], content=content.replace("\n\n\n", "\n\n"))
136+
137+
with open("generated.md", "w") as f:
138+
f.write(result.replace("\n\n\n", "\n\n"))
139+
140+
print("Done!")

0 commit comments

Comments
 (0)