Skip to content

Instantly share code, notes, and snippets.

@MuellerSeb
Last active April 9, 2021 09:22
Show Gist options
  • Save MuellerSeb/e9449aadec0ac1788332876ecfdeaa48 to your computer and use it in GitHub Desktop.
Save MuellerSeb/e9449aadec0ac1788332876ecfdeaa48 to your computer and use it in GitHub Desktop.
A code snippet creator. Convert code files to cropped PDFs.
# -*- coding: utf-8 -*-
"""A code snippet creator. Convert code files to cropped PDFs.
Copy this file to the folder containing your code-snippets.
A "pdf" folder will be created containing the rendered snippets.
Will convert "*.py" and "*.txt" files by default.
Needs pdflatex and minted installed.
"""
from pathlib import Path
import tempfile as tmp
import subprocess as sub
import shutil
TEX_FILE = """\\documentclass[10pt,border=0mm,preview,varwidth={width}mm]
{{standalone}}
\\usepackage{{minted,xcolor}}
\\usemintedstyle{{friendly}}
\\definecolor{{bg}}{{HTML}}{{f0f0f0}}
\\setminted{{bgcolor=bg}}
\\begin{{document}}
\\inputminted[frame=lines,framesep=2mm,fontsize=\\scriptsize]
{{{language}}}{{{file}}}
\\end{{document}}"""
def render_snippet(file, out_dir=None, cols=None):
r"""
Render a python code-snippet into a PDF.
Parameters
----------
file : Path
File containing the code snippet.
out_dir : Path
Desired output folder. Parent folder of file by default.
cols : number
Number of columns in rendered snippet.
Default is the maximal line length in the given file.
"""
cols = len(max(file.open(), key=len)) if cols is None else float(cols)
out_dir = file.parent if out_dir is None else Path(out_dir)
language = "python" if file.suffix == ".py" else "bash"
tex_name, pdf_name = [file.stem + ext for ext in [".tex", ".pdf"]]
# pdflatex with minted (shell-escape needed)
call = ["pdflatex", "--interaction=nonstopmode", "-shell-escape", tex_name]
# width of 75mm for 56 cols
tex = TEX_FILE.format(width=cols * 75 / 56, language=language, file=file)
with tmp.TemporaryDirectory() as tmp_dir:
(Path(tmp_dir) / tex_name).write_text(tex)
sub.run(call, cwd=tmp_dir, stdout=sub.PIPE, check=True)
shutil.copy2(Path(tmp_dir) / pdf_name, out_dir / pdf_name)
if __name__ == "__main__":
HERE = Path(__file__).parent
OUT_DIR = HERE / "pdf"
OUT_DIR.mkdir(exist_ok=True)
EXCLUDE = ["pdf_export.py"]
EXT = [".py", ".txt"]
# go for it
FILES = sum([list(HERE.glob("*" + ext)) for ext in EXT], [])
for snippet in FILES:
if snippet.name not in EXCLUDE:
print("converting file: '{}'".format(snippet.name))
render_snippet(snippet, out_dir=OUT_DIR, cols=56)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment