1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #!/usr/bin/env python3
- import os
- import sys
- from re import match
- from subprocess import run
- from os import listdir, rename
- from shutil import copyfile
- from os.path import splitext, join
- def make_tex(infilename):
- outfilename = splitext(infilename)[0]+'.tex'
- run(('pandoc', '--biblatex', '-F', 'pandoc-crossref', '-s', infilename, '-o', outfilename,
- '--metadata=bibliography:references.bib',
- '--template=chapter',
- ))
- with open(outfilename, 'r') as f:
- body = f.read()
- body = body.replace(r'\printbibliography', '')
- with open(outfilename, 'w') as f:
- f.write(body)
- def make_html(infilename):
- outfilename = splitext(infilename)[0]+'.html'
- run(('pandoc', '-F', 'pandoc-crossref', '-F', 'pandoc-citeproc', '-s', infilename, '-o', outfilename,
- '--mathjax',
- '--metadata=linkReferences:true',
- '--metadata=bibliography:references.bib',
- '--metadata=citation-style:american-physics-society.csl',
- '--template=chapter',
- ))
- def main():
- if len(sys.argv) != 2 or sys.argv[1] not in ('html', 'tex'):
- print('usage: ./convert.py [html|tex]')
- sys.exit(1)
- for fname in listdir('content'):
- if match(r'.*\.md', fname):
- copyfile(join('content', fname), join('build', fname))
- os.chdir('build')
- fmt = sys.argv[1]
- for infilename in listdir('.'):
- if not match(r'.*\.md', infilename):
- continue
- print(f'Converting {infilename} to {fmt}...', end='', flush=True)
- if fmt == 'html':
- make_html(infilename)
- else:
- make_tex(infilename)
- print('Done')
- if __name__ == '__main__':
- main()
|