convert.py 1.7 KB

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