convert.py 1.6 KB

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