convert.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. from subprocess import run
  5. from shutil import copyfile
  6. from os.path import splitext, join
  7. def make_tex(infilename):
  8. outfilename = splitext(infilename)[0]+'.tex'
  9. run(('pandoc', '--biblatex',
  10. '-F', 'pandoc-crossref',
  11. '-F', '../filters/note_filter.py',
  12. # '-F', '../filters/shorthand_filter.py',
  13. '-s', infilename, '-o', outfilename,
  14. '--metadata=bibliography:references.bib',
  15. '--template=chapter',
  16. ))
  17. with open(outfilename, 'r') as f:
  18. body = f.read()
  19. body = body.replace(r'\printbibliography', '')
  20. with open(outfilename, 'w') as f:
  21. f.write(body)
  22. def make_html(infilename):
  23. outfilename = splitext(infilename)[0]+'.html'
  24. run(('pandoc',
  25. '-F', 'pandoc-crossref',
  26. '-F', 'pandoc-citeproc',
  27. # '-F', '../filters/shorthand_filter.py',
  28. '-s', infilename, '-o', outfilename,
  29. '--mathjax',
  30. '--metadata=linkReferences:true',
  31. '--metadata=bibliography:references.bib',
  32. '--metadata=citation-style:american-institute-of-physics.csl',
  33. '--template=chapter',
  34. ))
  35. def main(src, dest_fmt):
  36. print(f'Converting {src} to {dest_fmt}...', end='', flush=True)
  37. copyfile(join('content', src), join('build', src))
  38. os.chdir('build')
  39. if dest_fmt == 'html':
  40. make_html(src)
  41. else:
  42. make_tex(src)
  43. print('Done')
  44. if __name__ == '__main__':
  45. if len(sys.argv) < 3 or sys.argv[2] not in ('html', 'tex'):
  46. print('usage: ./convert.py input_markdown [html|tex]')
  47. sys.exit(1)
  48. main(sys.argv[1], sys.argv[2])