-
Notifications
You must be signed in to change notification settings - Fork 45
/
pimp_notebook.py
48 lines (42 loc) · 2.22 KB
/
pimp_notebook.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
import glob
import re
from tweak_markdown import read_file, write_file
def transform_note_reference(re_find, content_note=False):
num_note=re.findall(r'\d+', re_find.group())[0]
if content_note is False:
note_html = f"<a name=\"cite_ref-{num_note}\"></a>[<sup>[{num_note}]</sup>](#cite_note-{num_note})"
else:
note_html = "```{=html}\n" + f"<a name=\"cite_note-{num_note}\"></a>{num_note}. [^](#cite_ref-{num_note})"+ "\n```\n"
return note_html
def change_box_markdown(fl):
content = read_file(fl)
content = re.sub(r"(“|”)",'"',content)
print(f"File: {fl}")
# BOXES
list_rows = content.split("\n")
corresp_boxes = {
"note": "::: {.cell .markdown}\n```{=html}\n<div class=\"alert alert-info\" role=\"alert\">\n```",
"warning": "::: {.cell .markdown}\n```{=html}\n<div class=\"alert alert-danger\" role=\"alert\">\n```",
"danger": "::: {.cell .markdown}\n```{=html}\n<div class=\"alert alert-danger\" role=\"alert\">\n```",
"exercise": "::: {.cell .markdown}\n```{=html}\n<div class=\"alert alert-success\" role=\"alert\">\n```",
"hint": "::: {.cell .markdown}\n```{=html}\n<div class=\"alert alert-warning\" role=\"alert\">\n```"}
tweak_md = [corresp_boxes[re.search('status=\"(.*?)\"',l).group(1)] if l.startswith("{{% box") else l for l in list_rows]
tweak_md = ["```{=html}\n</div>\n```\n:::" if l.startswith("{{% /box") else l for l in tweak_md]
tweak_md = "\n".join(tweak_md)
# FOOTNOTES
p = re.compile("\[\^[0-9]+\]:")
list_match = list(p.finditer(tweak_md))
for i in range(0, len(list_match)):
m = list_match[i]
tweak_md = tweak_md.replace(m.group(0), transform_note_reference(m, content_note=True))
# 2. REFERENCE TO THE FOOTNOTE
p = re.compile("\[\^[0-9]+\]")
list_match = list(p.finditer(tweak_md))
for i in range(0, len(list_match)):
m = list_match[i]
tweak_md = tweak_md.replace(m.group(0), transform_note_reference(m, content_note=False))
write_file(fl, tweak_md)
if __name__ == '__main__':
list_files = glob.glob("./content/course/**/index.qmd", recursive=True)
print(list_files)
[change_box_markdown(fl) for fl in list_files if not fl.endswith("_index.md")]