1
0
mirror of https://github.com/satwikkansal/wtfpython synced 2024-06-08 22:27:48 +02:00
wtfpython/parse_readme.py

65 lines
1.8 KiB
Python
Raw Normal View History

2018-01-17 18:31:51 +01:00
import pprint
fname = "README.md"
2018-01-21 11:21:17 +01:00
snippets = []
2018-01-17 18:31:51 +01:00
with open(fname, 'r') as f:
2018-01-21 11:21:17 +01:00
lines = iter(f.readlines())
line = lines.next()
try:
while True:
# check if it's a H3
if line.startswith("### "):
title = line.replace("### ", "")
# print(title, "found")
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()
# print("Description captured", description[:10])
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()
# print("explanation captured", explanation[:10])
# 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)
})
2018-01-17 18:31:51 +01:00
# separating by category
categories = ["a", "b", "c"]
snips_by_cat = {k:[] for k in categories}
for snip in snippets:
2018-01-21 11:21:17 +01:00
cat = raw_input(snip["title"])
snips_by_cat[cat].append(snip)
2018-01-17 18:31:51 +01:00
2018-01-21 11:21:17 +01:00
pprint.pprint("hail", snippets)