Created
September 14, 2021 13:08
-
-
Save AntonMyr/0f72a9a8e7e338cf011ba84d199fbb1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from datetime import datetime | |
from lib import tag_checker | |
tags = tag_checker() | |
tags.add_tag("question", "<q", "q>") | |
tags.add_tag("remember", "r==", "==r") | |
tags.add_tag("idea", "<i", "i>") | |
dirs = ["/Users/antonmyrberg/extending-markdown-with-python/notes"] | |
for folder in dirs: | |
for filename in os.listdir(folder): | |
correct_md = filename[-2:] == "md" | |
if not correct_md: | |
continue | |
full_path = folder + "/" + filename | |
curr_fp = open(full_path, "r") | |
for line in curr_fp: | |
tags.run(line) | |
curr_fp.close() | |
tmp_file = "/tmp/" + datetime.now().strftime("%y%m%d%H%M%S") + ".md" | |
tmp_fp = open(tmp_file, "w") | |
tmp_fp.write("# Collection of tags\n") | |
for tag in tags.tags: | |
for i, tag_item in enumerate(tags.tags[tag]["tag_list"]): | |
tmp_fp.write("## %s %d\n" % (tag, i)) | |
tmp_fp.write(tag_item + "\n") | |
tmp_fp.close() | |
print(tmp_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
╰─$ python step_1.py | |
Lines: # This is the title | |
| |
Lines: | |
| |
Lines: Here is a line | |
| |
Lines: | |
| |
Lines: Markdown actually uses two newline characters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
╰─$ python step_2.py | |
tag_start_index: 4 | |
Line starting from index: A programming tutorial where you just follow along i> | |
| |
Original line: * <i A programming tutorial where you just follow along i> | |
| |
tag_start_index: 2 | |
Line starting from index: | |
| |
Original line: <i | |
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
markdown_fp = open("step_2.md", "r") | |
# Needed for later | |
idea_list = [] | |
idea_counter = 0 | |
start_t = "<i" | |
end_t = "i>" | |
inside_tag = False | |
for line in markdown_fp: | |
start_tag = start_t in line | |
end_tag = end_t in line | |
outside_tag = not inside_tag | |
if start_tag and outside_tag: | |
# Start tag | |
tag_start_index = line.index("<i") + len("<i") | |
line = line[tag_start_index:] | |
# This is where we'll store the idea | |
idea_list.append("") | |
inside_tag = True | |
if end_tag and inside_tag: | |
# End tag | |
end_tag_index = line.index("i>") | |
line = line[:end_tag_index] | |
tag_list[idea_counter] += line | |
idea_counter += 1 | |
inside_tag = False | |
if inside_tag: | |
# Extract | |
idea_list[idea_counter] += line | |
markdown_fp.close() | |
output_fp = open("output.md", "w") | |
output_fp.write("# Collection of ideas") | |
for i, idea in enumerate(idea_list): | |
output_fp.write("## Idea %d\n" % (i)) | |
output_fp.write(idea + "\n") | |
output_fp.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# File: step_2.py | |
markdown_fp = open("step_2.md", "r") | |
| |
# Needed for later | |
idea_list = [] | |
idea_counter = 0 | |
| |
inside_tag = False | |
for line in markdown_fp: | |
start_tag = "<i" in line | |
end_tag = "i>" in line | |
outside_tag = not inside_tag | |
| |
if start_tag and outside_tag: | |
# Start tag | |
if end_tag and inside_tag: | |
# End tag | |
pass | |
if inside_tag: | |
# Extract | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment