-
Notifications
You must be signed in to change notification settings - Fork 35
/
list_to_collapsible.py
97 lines (84 loc) · 3.51 KB
/
list_to_collapsible.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""This script turns a list in a markdown file into a collapsible dropdown.
The markdown file is assumed to be exported from Notion (particularly at this
link: https://chawins.github.io/llm-sp). The collapsible dropdown is in HTML.
"""
def reformat_header(line):
dropdown_header = line.strip()[2:]
title_end_idx = dropdown_header.find("[[")
title = dropdown_header[:title_end_idx]
# Find all the links
links = []
link_string = dropdown_header
while True:
# Not very efficient but will do
_idx = link_string.find("[[")
if _idx == -1:
break
name_start_idx = _idx + 2
name_end_idx = link_string.find("]")
name = link_string[name_start_idx:name_end_idx]
url_start_idx = name_end_idx + 2
url_start = link_string[url_start_idx:]
url_len = url_start.find(")")
url = link_string[url_start_idx : url_start_idx + url_len]
new_link = f'[<a href="{url}">{name}</a>]'
links.append(new_link)
link_string = link_string[url_start_idx + url_len + 2 :]
# What's left is the emojis
emojis = link_string
links = " ".join(links)
return f"{title}{links}{emojis}"
def markdown_to_collapsible_dropdown(filename):
with open(filename, "r", encoding="utf-8") as f:
content = f.readlines()
new_content = []
in_list = False
dropdown_content = []
line_idx = 0
for line in content:
if "Other resources" in line:
break
if line.startswith("-") and not line.startswith("---") and not in_list:
# Start new paper
dropdown_content = []
dropdown_content.append("<details>")
new_header = reformat_header(line)
dropdown_content.append(f"<summary>{new_header}</summary>\n\n")
in_list = True
elif line.startswith("-") and not line.startswith("---"):
dropdown_content.append("</details>\n")
dropdown_content.append("\n")
new_content.extend(dropdown_content)
dropdown_content = []
dropdown_content.append("<details>")
new_header = reformat_header(line)
dropdown_content.append(f"<summary>{new_header}</summary>\n\n")
in_list = True
elif line.startswith(" "):
assert in_list, line
if "Untitled" in line and ".png" in line:
# e.g., ![Untitled](LLM%20Security%20&%20Privacy%20c1bca11f7bec40988b2ed7d997667f4d/Untitled%201.png)
figure_id = line.strip().replace(".png)", "").split("Untitled")[-1].replace("%20", "")
if not figure_id:
figure_id = 0
dropdown_content.append(f" ![Untitled](figures/{figure_id}.png)\n")
else:
dropdown_content.append(line[4:])
else:
if dropdown_content:
dropdown_content.append("</details>\n")
dropdown_content.append("\n")
new_content.extend(dropdown_content)
dropdown_content = []
new_content.append(line)
in_list = False
line_idx += 1
new_content.extend(content[line_idx:])
# Write to a new file
with open("converted_" + filename, "w", encoding="utf-8") as f:
for line in new_content:
f.write(line)
if __name__ == "__main__":
markdown_file = input("Enter the Markdown filename: ")
markdown_to_collapsible_dropdown(markdown_file)
print(f"Converted content written to converted_{markdown_file}")