Path: blob/master/internal/book2/handle_book2_pybook.py
1192 views
import os1import argparse2import shutil34parser = argparse.ArgumentParser(description="Handle book2 pybook")5parser.add_argument("-bookgen", "--bookgen", type=int, choices=[0, 1], default=0, help="")6args = parser.parse_args()78# configs9macro = r"\newcommand{\pybook}[1]{\href{https://colab.research.google.com/github/probml/probml-notebooks/blob/master/notebooks/#1.ipynb}{#1.ipynb}}"10replaced_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}}"1112book_root = "../bookv2"13bookname = "book2"1415if args.bookgen:16# replace the macro17with open(os.path.join(book_root, "macros.tex"), "r") as f:18content = f.read()19with open(os.path.join(book_root, "macros.tex"), "w") as f:20f.write(content.replace(macro, replaced_macro))2122# generate the book23pdflatex_cmd = f"pdflatex --interaction=nonstopmode {bookname}"24bibtex_cmd = f"bibtex {bookname}"25os.system(26f"cd {book_root}/{bookname}; {pdflatex_cmd}; {bibtex_cmd}; {pdflatex_cmd}; mv {bookname}.pdf {bookname}_pybook.pdf"27)2829# restore the macro30with open(os.path.join(book_root, "macros.tex"), "r") as f:31content = f.read()32with open(os.path.join(book_root, "macros.tex"), "w") as f:33f.write(content.replace(replaced_macro, macro))3435# read the book36import fitz3738with fitz.open(os.path.join(book_root, bookname, f"{bookname}_pybook.pdf")) as doc:39text = ""40for page in doc:41text += page.get_text() + ""42text_one_line = text.replace("\n", "")4344# solve some glitches45glitch_dict = {"fi": "fi", "ff": "ff", "fl": "fl"}46for glitch, fix in glitch_dict.items():47text_one_line = text_one_line.replace(glitch, fix)4849# parse script names50import re5152all_nb = set(re.findall("sssssnb(.*?)eeeeenb", text_one_line))53# print(all_nb)5455# start transfer56for chap_nb in all_nb:57chap, nb = chap_nb.split("/")58try:59save_path = f"notebooks/book2/{chap}/{nb}"60try:61assert not os.path.exists(save_path)62except AssertionError:63raise Exception(f"{save_path} already exists")64save_dir = os.path.dirname(save_path)65if not os.path.exists(save_dir):66os.makedirs(save_dir)67shutil.copy(f"../probml-notebooks/notebooks/{nb}", save_path)68# print("done", chap, script)69except Exception as e:70print(e)717273