-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
36 lines (26 loc) · 1.01 KB
/
utility.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
#%%
import re
from pathlib import Path
import pandas as pd
OUT_FOLDER = Path("fdf_posts")
def fix_multilevel(df: pd.DataFrame) -> pd.DataFrame:
df.columns = pd.MultiIndex.from_tuples(df.columns)
return df
def write_to_file(file, name, value):
pattern_tag = rf"\[//\]: # \({name}\)"
pattern_full = rf"({pattern_tag})([\s\S]*)({pattern_tag})"
file_content = file.read_text()
# print("===================")
# print(re.findall(pattern_tag, file_content))
# print(re.findall(pattern_full, file_content))
# print("===================")
repl = re.sub(pattern_full, rf"\1\n\n{value.strip()}\n\n\3", file_content)
file.write_text(repl)
def write_img_to_file(file, name, path: Path, alt_text=None, caption=None):
path = path.relative_to(OUT_FOLDER)
x = str(path).replace("\\", "/")
# print(x)
c = f"{caption}\n" if caption else ""
alt_text = alt_text or ""
img_tag = f"{c}![{alt_text}]({x})"
write_to_file(file, name, img_tag)