convert.py 1.7 KB

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