|
1 | 1 | import pprint
|
2 | 2 |
|
3 | 3 | fname = "README.md"
|
4 |
| -snipepts = [] |
| 4 | +snippets = [] |
5 | 5 |
|
6 | 6 | with open(fname, 'r') as f:
|
7 |
| - lines = f.readlines() |
8 |
| - iterator |
9 |
| - while iterator: |
10 |
| - # check if it's a H3 |
11 |
| - if line.startswith("###"): |
12 |
| - title = line.replace("### ", "") |
13 |
| - description = '' |
14 |
| - next_line = itertor.next |
15 |
| - while not next_line.startswith("#### "): |
16 |
| - # store lines till an H4 (explanation) is encountered |
17 |
| - description.append(next_line) |
18 |
| - next_line = iterator.next |
19 |
| - # store lines again until --- or another H3 is encountered |
20 |
| - snippets.append({ |
21 |
| - "title":, |
22 |
| - "description":, |
23 |
| - "explanation": |
24 |
| - }) |
25 |
| - # repeat until EOL is encoutered |
| 7 | + lines = iter(f.readlines()) |
| 8 | + line = lines.next() |
| 9 | + |
| 10 | + try: |
| 11 | + while True: |
| 12 | + # check if it's a H3 |
| 13 | + if line.startswith("### "): |
| 14 | + title = line.replace("### ", "") |
| 15 | + # print(title, "found") |
| 16 | + description = [] |
| 17 | + next_line = lines.next() |
| 18 | + |
| 19 | + # store lines till an H4 (explanation) is encountered |
| 20 | + while not next_line.startswith("#### "): |
| 21 | + description.append(next_line) |
| 22 | + next_line = lines.next() |
| 23 | + |
| 24 | + # print("Description captured", description[:10]) |
| 25 | + |
| 26 | + explanation = [] |
| 27 | + # store lines again until --- or another H3 is encountered |
| 28 | + while not (next_line.startswith("---") or |
| 29 | + next_line.startswith("### ")): |
| 30 | + explanation.append(next_line) |
| 31 | + next_line = lines.next() |
| 32 | + |
| 33 | + # print("explanation captured", explanation[:10]) |
| 34 | + |
| 35 | + |
| 36 | + # Store the results finally |
| 37 | + snippets.append({ |
| 38 | + "title": title, |
| 39 | + "description": '\n'.join(description), |
| 40 | + "explanation": '\n'.join(explanation) |
| 41 | + }) |
| 42 | + |
| 43 | + line = next_line |
| 44 | + |
| 45 | + else: |
| 46 | + line = lines.next() |
| 47 | + |
| 48 | + except StopIteration: |
| 49 | + snippets.append({ |
| 50 | + "title": title, |
| 51 | + "description": '\n'.join(description), |
| 52 | + "explanation": '\n'.join(explanation) |
| 53 | + }) |
26 | 54 |
|
27 | 55 | # separating by category
|
28 | 56 | categories = ["a", "b", "c"]
|
29 | 57 |
|
30 | 58 | snips_by_cat = {k:[] for k in categories}
|
31 | 59 |
|
32 | 60 | for snip in snippets:
|
33 |
| - cat = raw_input(snip["title"]) |
34 |
| - snips_by_cat[cat].append(snip) |
| 61 | + cat = raw_input(snip["title"]) |
| 62 | + snips_by_cat[cat].append(snip) |
35 | 63 |
|
36 |
| -pprint.pprint(snips_by_cat) |
| 64 | +pprint.pprint("hail", snippets) |
0 commit comments