Skip to content

Instantly share code, notes, and snippets.

@jbn
Created January 27, 2019 00:48
Show Gist options
  • Save jbn/f8c8900917a66251baf2a52f659ec9d2 to your computer and use it in GitHub Desktop.
Save jbn/f8c8900917a66251baf2a52f659ec9d2 to your computer and use it in GitHub Desktop.
import os
import re
import shutil
import subprocess
ROOT_DIR = "overleaf_kludge"
DISSERTATION_FILES_DIR = os.path.join(ROOT_DIR, "_jbn_dissertation_files")
###############################################################################
# ____ _____ _ #
# / ___|___ _ __ _ _ | ____|_ ____ _____| | ___ _ __ ___ #
# | | / _ \| '_ \| | | | | _| | '_ \ \ / / _ \ |/ _ \| '_ \ / _ \ #
# | |__| (_) | |_) | |_| | | |___| | | \ V / __/ | (_) | |_) | __/ #
# \____\___/| .__/ \__, | |_____|_| |_|\_/ \___|_|\___/| .__/ \___| #
# |_| |___/ |_| #
###############################################################################
shutil.rmtree(ROOT_DIR, ignore_errors=True)
shutil.copytree(os.path.join("template"), os.path.join(ROOT_DIR))
shutil.copy("bibliography.bibtex", os.path.join(ROOT_DIR, "bibliography.bibtex"))
shutil.copytree("_jbn_dissertation_files", DISSERTATION_FILES_DIR)
###############################################################################
# ____ ____ _____ _ __ ______ ______ #
# | _ \| _ \| ___| (_)/ _|_ _ / ___\ \ / / ___|___ #
# | |_) | | | | |_ _____| | |_| | | | \___ \\ \ / / | _/ __| #
# | __/| |_| | _|_____| | _| |_| | ___) |\ V /| |_| \__ \ #
# |_| |____/|_| |_|_| \__, | |____/ \_/ \____|___/ #
# |___/ #
###############################################################################
for file_name in os.listdir(DISSERTATION_FILES_DIR):
if not file_name.endswith('.svg'):
continue
src_path = os.path.join(DISSERTATION_FILES_DIR, file_name)
dst_path = os.path.join(DISSERTATION_FILES_DIR, file_name.replace(".svg", ".pdf"))
subprocess.check_call(["rsvg-convert", "-f", "pdf", "-o", dst_path, src_path])
with open("build/thesis.tex") as fp:
tex = fp.read()
tex = tex.replace("/src/template/sigs/compsocsci.tex", "sigs/compsocsci.tex")
tex = tex.replace(".svg}", ".pdf}")
###############################################################################
# ____ _ _ _ _ _____ _ _ #
# | _ \ __ _ _ __| |_(_) |_(_) ___ _ __ | ___(_) | ___ ___ #
# | |_) / _` | '__| __| | __| |/ _ \| '_ \ | |_ | | |/ _ \/ __| #
# | __/ (_| | | | |_| | |_| | (_) | | | | | _| | | | __/\__ \ #
# |_| \__,_|_| \__|_|\__|_|\___/|_| |_| |_| |_|_|\___||___/ #
# #
###############################################################################
CHAPTER_RE = re.compile(r"\\hypertarget{[^}]+}{%\n\\chapter\*?{([^}]+)}\\label{[^}]+}}", re.M|re.I)
names, boundaries = [], []
for i, m in enumerate(CHAPTER_RE.finditer(tex)):
pythonized_name = m.group(1).replace("\n", " ").replace("Appendix: ", "").replace(" ", "_").lower()
names.append(f'{i+1:02d}_{pythonized_name}')
boundaries.append(m.start())
names.append("__ignored__")
boundaries.append(len(tex))
###############################################################################
# ____ _ _ _____ #
# | _ \ _____ ___ __(_) |_ ___ |_ _|____ __ #
# | |_) / _ \ \ /\ / / '__| | __/ _ \ | |/ _ \ \/ / #
# | _ < __/\ V V /| | | | || __/ | | __/> < #
# |_| \_\___| \_/\_/ |_| |_|\__\___| |_|\___/_/\_\ #
# #
###############################################################################
INCLUDE_GRAPHICS_RE = re.compile(r"\\includegraphics{(_jbn_dissertation_files[^}]+)}", re.M|re.I)
BLANK_LISTING = "\\begin{lstlisting}[language=Python]\n\\end{lstlisting}"
def translate_images(tex, chapter):
assert isinstance(chapter, str)
chapter_name = chapter + "_media"
chapter_path = os.path.join(ROOT_DIR, chapter_name)
os.makedirs(chapter_path, exist_ok=True)
n = 0
while True:
m = INCLUDE_GRAPHICS_RE.search(tex)
if not m:
break
src = m.group(1)
dst = os.path.join(chapter_name, f"{n:03d}.pdf")
shutil.copyfile(os.path.join(ROOT_DIR, src),
os.path.join(ROOT_DIR, dst))
tex = tex.replace(m.group(0), r"\includegraphics{" + dst + "}")
if n > 1000:
break
n += 1
return tex
new_tex = tex[:boundaries[0]]
chapters = range(1, len(boundaries)+1)
for chapter, name, start, end in zip(chapters, names, boundaries[:-1], boundaries[1:]):
output_path = os.path.join(ROOT_DIR, name + ".tex")
with open(output_path, "w") as fp:
sub_tex = tex[start:end].replace(BLANK_LISTING, "")
sub_tex = translate_images(sub_tex, name.split("_")[0])
fp.write(sub_tex)
new_tex += "\n\\input{" + name + "}\n"
with open(os.path.join(ROOT_DIR, "jbn_dissertation.tex"), "w") as fp:
fp.write(new_tex)
shutil.rmtree(DISSERTATION_FILES_DIR, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment