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