From anonymous, 1 Month ago, written in Python.
Embed
  1. import subprocess
  2. import tempfile
  3. import pathlib
  4. import sys
  5.  
  6.  
  7. def run(*args, **kwargs):
  8.     print(*args)
  9.     subprocess.run(args, check=True, **kwargs)
  10.  
  11.  
  12. def main(filename, tmpdir):
  13.     code_to_pdf(filename, tmpdir)
  14.  
  15.  
  16. def code_to_pdf(filename, tmpdir):
  17.     tex = tmpdir / 'code.tex'
  18.     pdf = tmpdir / 'code.pdf'
  19.  
  20.     opts = [
  21.         'full',
  22.         'docclass=scrartcl',
  23.         r'preamble=\usepackage{minted}\fvset{breaklines=true}\usepackage{geometry}',
  24.         'title=' + sys.argv[2],
  25.     ]
  26.  
  27.     args = []
  28.     for opt in opts:
  29.         args += ['-P', opt]
  30.  
  31.     run('pygmentize', '-o', tex, *args, filename)
  32.     run('pdflatex', '-shell-escape', tex, cwd=tmpdir)
  33.     run('lp', pdf)
  34.  
  35.  
  36. with tempfile.TemporaryDirectory() as temp:
  37.     main(filename=pathlib.Path(sys.argv[1]), tmpdir=pathlib.Path(temp))
  38.