Implement parser for categorizing, reordering and restructuring.

In short, a major refactor before 2.0
This commit is contained in:
Satwik Kansal 2018-01-22 14:43:18 +05:30
parent 3b956caba2
commit e58700d098
1 changed files with 83 additions and 7 deletions

View File

@ -1,5 +1,4 @@
import pprint
# -*- coding: utf-8 -*-
fname = "README.md"
snippets = []
@ -52,13 +51,90 @@ with open(fname, 'r') as f:
"explanation": '\n'.join(explanation)
})
# separating by category
categories = ["a", "b", "c"]
'''
# Create a file
file_content = "\n\n".join([snip["title"] for snip in snippets])
snips_by_cat = {k:[] for k in categories}
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:
print("Done!")
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 = raw_input(snip["title"])
cat = snip["category"]
if not snips_by_cat.get(cat):
snips_by_cat[cat] = []
snips_by_cat[cat].append(snip)
pprint.pprint("hail", snippets)
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!")