-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummary.py
32 lines (28 loc) · 1.16 KB
/
summary.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
from pathlib import Path
import re
from slugify import slugify
class Config():
start_dir: Path = Path(__file__).parent.resolve()
chapters_dir: Path = start_dir/"kapitler"
appendix_dir: Path = start_dir/"appendiks"
def main():
files: list[Path] = list(Config.chapters_dir.iterdir())
def sort_method(da_file: Path):
stringy = da_file.stem
return int(stringy)
files.sort(key=sort_method)
summary = "# A: Sammendrag\n"
for filepath in files:
filepath.resolve()
with filepath.open("r") as chap_file:
stringy = chap_file.read()
stringy = stringy.split("##")[0] # Get only introductory paragraph
stringy = re.sub(r"\(.*\)=\n", "", stringy) # Remove myst labels to avoid duplicates
title = re.search(r"#\s?\d*:?\s?(.*)\n", stringy).group(1)+" sammendrag"
stringy = "("+slugify(title)+")=\n"+stringy # Make new myst label from slug of title
stringy = stringy.replace("#", "##") # Only one h1 per page
summary += "\n" + stringy
with (Config.appendix_dir/"A.md").open("w") as summary_file:
summary_file.write(summary)
if __name__ == "__main__":
main()