From 67b7a5055ae881022dd4a946be352579af0f116a Mon Sep 17 00:00:00 2001 From: Satwik Kansal Date: Sun, 21 Jan 2018 15:51:17 +0530 Subject: [PATCH] Turn pseudo code to actual code --- parse_readme.py | 74 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/parse_readme.py b/parse_readme.py index a7763da..0ace3df 100644 --- a/parse_readme.py +++ b/parse_readme.py @@ -1,28 +1,56 @@ import pprint fname = "README.md" -snipepts = [] +snippets = [] with open(fname, 'r') as f: - lines = f.readlines() - iterator - while iterator: - # check if it's a H3 - if line.startswith("###"): - title = line.replace("### ", "") - description = '' - next_line = itertor.next - while not next_line.startswith("#### "): - # store lines till an H4 (explanation) is encountered - description.append(next_line) - next_line = iterator.next - # store lines again until --- or another H3 is encountered - snippets.append({ - "title":, - "description":, - "explanation": - }) - # repeat until EOL is encoutered + 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) + }) # separating by category categories = ["a", "b", "c"] @@ -30,7 +58,7 @@ categories = ["a", "b", "c"] snips_by_cat = {k:[] for k in categories} for snip in snippets: - cat = raw_input(snip["title"]) - snips_by_cat[cat].append(snip) + cat = raw_input(snip["title"]) + snips_by_cat[cat].append(snip) -pprint.pprint(snips_by_cat) +pprint.pprint("hail", snippets)