|
| 1 | +import pprint |
| 2 | +fname = "/Users/300041709/code/self/wtfpython/README.md" |
| 3 | +examples = [] |
| 4 | + |
| 5 | +# The globals |
| 6 | +current_example = 1 |
| 7 | +current_section_name = "" |
| 8 | + |
| 9 | + |
| 10 | +def parse_example_parts(lines): |
| 11 | + parts = { |
| 12 | + "build_up": [], |
| 13 | + "explanation": [] |
| 14 | + } |
| 15 | + next_line = next(lines) |
| 16 | + sequence_num = 1 |
| 17 | + content = [] |
| 18 | + # store build_up till an H4 (explanation) is encountered |
| 19 | + while not next_line.startswith("#### "): |
| 20 | + # Watching out for the snippets |
| 21 | + if next_line.startswith("```"): |
| 22 | + # It's a snippet, whatever found until now is text |
| 23 | + if content: |
| 24 | + parts["build_up"].append( |
| 25 | + { |
| 26 | + "type": "text", |
| 27 | + "sequence_num": sequence_num, |
| 28 | + "value": content |
| 29 | + } |
| 30 | + ) |
| 31 | + sequence_num += 1 |
| 32 | + content = [] |
| 33 | + |
| 34 | + next_line = next(lines) |
| 35 | + while not next_line.startswith("```"): |
| 36 | + content.append(next_line) |
| 37 | + next_line = next(lines) |
| 38 | + # Snippet is over |
| 39 | + parts["build_up"].append( |
| 40 | + { |
| 41 | + "type": "code", |
| 42 | + "sequence_num": sequence_num, |
| 43 | + "value": content |
| 44 | + } |
| 45 | + ) |
| 46 | + sequence_num += 1 |
| 47 | + content = [] |
| 48 | + next_line = next(lines) |
| 49 | + continue |
| 50 | + else: |
| 51 | + # It's a text, go on. |
| 52 | + content.append(next_line) |
| 53 | + next_line = next(lines) |
| 54 | + |
| 55 | + # Explanation encountered, save any content till now (if any) |
| 56 | + if content: |
| 57 | + parts["build_up"].append( |
| 58 | + { |
| 59 | + "type": "text", |
| 60 | + "sequence_num": sequence_num, |
| 61 | + "value": content |
| 62 | + } |
| 63 | + ) |
| 64 | + |
| 65 | + # Reset stuff |
| 66 | + sequence_num = 1 |
| 67 | + content = [] |
| 68 | + |
| 69 | + # store lines again until --- or another H3 is encountered |
| 70 | + while not (next_line.startswith("---") or |
| 71 | + next_line.startswith("### ")): |
| 72 | + |
| 73 | + if next_line.startswith("```"): |
| 74 | + # It's a snippet, whatever found until now is text |
| 75 | + if content: |
| 76 | + parts["explanation"].append( |
| 77 | + { |
| 78 | + "type": "text", |
| 79 | + "sequence_num": sequence_num, |
| 80 | + "value": content |
| 81 | + } |
| 82 | + ) |
| 83 | + sequence_num += 1 |
| 84 | + content = [] |
| 85 | + |
| 86 | + next_line = next(lines) |
| 87 | + while not next_line.startswith("```"): |
| 88 | + content.append(next_line) |
| 89 | + next_line = next(lines) |
| 90 | + # Snippet is over |
| 91 | + parts["explanation"].append( |
| 92 | + { |
| 93 | + "type": "code", |
| 94 | + "sequence_num": sequence_num, |
| 95 | + "value": content |
| 96 | + } |
| 97 | + ) |
| 98 | + sequence_num += 1 |
| 99 | + content = [] |
| 100 | + next_line = next(lines) |
| 101 | + continue |
| 102 | + else: |
| 103 | + # It's a text, go on. |
| 104 | + content.append(next_line) |
| 105 | + next_line = next(lines) |
| 106 | + |
| 107 | + # All done |
| 108 | + if content: |
| 109 | + parts["explanation"].append( |
| 110 | + { |
| 111 | + "type": "text", |
| 112 | + "sequence_num": sequence_num, |
| 113 | + "value": content |
| 114 | + } |
| 115 | + ) |
| 116 | + |
| 117 | + return next_line, parts |
| 118 | + |
| 119 | + |
| 120 | +with open(fname, 'r+', encoding="utf-8") as f: |
| 121 | + lines = iter(f.readlines()) |
| 122 | + line = next(lines) |
| 123 | + result = [] |
| 124 | + try: |
| 125 | + while True: |
| 126 | + if line.startswith("## "): |
| 127 | + # A section is encountered |
| 128 | + current_section_name = line.replace("## ", "").strip() |
| 129 | + line = next(lines) |
| 130 | + # Until a new section is encountered |
| 131 | + while not (line.startswith("## " )): |
| 132 | + # check if it's a H3 |
| 133 | + if line.startswith("### "): |
| 134 | + # An example is encountered |
| 135 | + title = line.replace("### ", "") |
| 136 | + example_details = { |
| 137 | + "id": current_example, |
| 138 | + "title": line.replace("### ", ""), |
| 139 | + "section": current_section_name |
| 140 | + } |
| 141 | + line, example_details["parts"] = parse_example_parts(lines) |
| 142 | + result.append(example_details) |
| 143 | + current_example += 1 |
| 144 | + else: |
| 145 | + # todo catch section text |
| 146 | + line = next(lines) |
| 147 | + else: |
| 148 | + line = next(lines) |
| 149 | + |
| 150 | + except StopIteration: |
| 151 | + pprint.pprint(result, indent=2) |
| 152 | + print(len(result)) |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
0 commit comments