Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/internal/book2/handle_book2_pybook.py
1192 views
1
import os
2
import argparse
3
import shutil
4
5
parser = argparse.ArgumentParser(description="Handle book2 pybook")
6
parser.add_argument("-bookgen", "--bookgen", type=int, choices=[0, 1], default=0, help="")
7
args = parser.parse_args()
8
9
# configs
10
macro = r"\newcommand{\pybook}[1]{\href{https://colab.research.google.com/github/probml/probml-notebooks/blob/master/notebooks/#1.ipynb}{#1.ipynb}}"
11
replaced_macro = r"\newcommand{\pybook}[1]{\href{https://colab.research.google.com/github/probml/probml-notebooks/blob/master/notebooks/#1.ipynb}{sssssnb\twodigits{\thechapter}/#1.ipynbeeeeenb}}"
12
13
book_root = "../bookv2"
14
bookname = "book2"
15
16
if args.bookgen:
17
# replace the macro
18
with open(os.path.join(book_root, "macros.tex"), "r") as f:
19
content = f.read()
20
with open(os.path.join(book_root, "macros.tex"), "w") as f:
21
f.write(content.replace(macro, replaced_macro))
22
23
# generate the book
24
pdflatex_cmd = f"pdflatex --interaction=nonstopmode {bookname}"
25
bibtex_cmd = f"bibtex {bookname}"
26
os.system(
27
f"cd {book_root}/{bookname}; {pdflatex_cmd}; {bibtex_cmd}; {pdflatex_cmd}; mv {bookname}.pdf {bookname}_pybook.pdf"
28
)
29
30
# restore the macro
31
with open(os.path.join(book_root, "macros.tex"), "r") as f:
32
content = f.read()
33
with open(os.path.join(book_root, "macros.tex"), "w") as f:
34
f.write(content.replace(replaced_macro, macro))
35
36
# read the book
37
import fitz
38
39
with fitz.open(os.path.join(book_root, bookname, f"{bookname}_pybook.pdf")) as doc:
40
text = ""
41
for page in doc:
42
text += page.get_text() + ""
43
text_one_line = text.replace("\n", "")
44
45
# solve some glitches
46
glitch_dict = {"fi": "fi", "ff": "ff", "fl": "fl"}
47
for glitch, fix in glitch_dict.items():
48
text_one_line = text_one_line.replace(glitch, fix)
49
50
# parse script names
51
import re
52
53
all_nb = set(re.findall("sssssnb(.*?)eeeeenb", text_one_line))
54
# print(all_nb)
55
56
# start transfer
57
for chap_nb in all_nb:
58
chap, nb = chap_nb.split("/")
59
try:
60
save_path = f"notebooks/book2/{chap}/{nb}"
61
try:
62
assert not os.path.exists(save_path)
63
except AssertionError:
64
raise Exception(f"{save_path} already exists")
65
save_dir = os.path.dirname(save_path)
66
if not os.path.exists(save_dir):
67
os.makedirs(save_dir)
68
shutil.copy(f"../probml-notebooks/notebooks/{nb}", save_path)
69
# print("done", chap, script)
70
except Exception as e:
71
print(e)
72
73