Skip to content

Instantly share code, notes, and snippets.

@urschrei
Created April 13, 2011 18:37
Show Gist options
  • Save urschrei/918097 to your computer and use it in GitHub Desktop.
Save urschrei/918097 to your computer and use it in GitHub Desktop.
Convert a MultimarkDown file into a .tex using MMD3, and compile into a PDF using XeLaTeX, Biber and Biblatex
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
mmd3_proc.py
Convert a MultiMarkdown file into a latex file using MMD3,
and process it using XeLaTeX, Biber and Makeglossaries
Takes a single argument: the input file to process
Outputs a PDF with the same basename as as the input file to cwd
Created by Stephan Hügel on 2011-04-11
"""
import sys
import os
import subprocess
import shutil
import tempfile
import glob
# import traceback
def main():
"""
main function
"""
def copy_files(orig, extn, dest):
"""
Copy files with specified extension from orig to specified destination
copy_file(original dir, '*.ext', destination)
"""
return [shutil.copy2(f, dest) for
f in glob.iglob(os.path.join(orig, extn)) if os.path.isfile(f)]
infile = sys.argv[1]
basename, _ = os.path.splitext(infile)
cwd = os.getcwd()
tmp = tempfile.mkdtemp()
# figure out what files we need to copy to our temp dir
try:
copy_files(cwd, '*.bib', tmp)
copy_files(cwd, '*.jpg', tmp)
copy_files(cwd, '*.png', tmp)
except IOError:
print '***** Could not copy files to temp dir %s *****' % tmp
shutil.rmtree(tmp)
raise
try:
subprocess.check_call(['multimarkdown', '-t', 'latex', infile,
'-o', os.path.join(tmp, basename + '.tex')])
subprocess.check_call(['xelatex',
'-output-directory', tmp, '-interaction', 'batchmode',
os.path.join(tmp, basename + '.tex')])
subprocess.check_call(['biber', '-q',
os.path.join(tmp, basename)])
subprocess.check_call(['makeindex', '-q',
'-s', os.path.join(tmp, basename + '.ist'),
'-t', os.path.join(tmp, basename + '.glg'),
'-o', os.path.join(tmp, basename + '.gls'),
os.path.join(tmp, basename + '.glo')])
subprocess.check_call(['xelatex',
'-output-directory', tmp, '-interaction', 'batchmode',
os.path.join(tmp, basename + '.tex')])
subprocess.check_call(['xelatex',
'-output-directory', tmp, '-interaction', 'batchmode',
os.path.join(tmp, basename + '.tex')])
except subprocess.CalledProcessError:
print '***** Error, check log file(s) in %s *****' % cwd
try:
copy_files(tmp, '*.log', cwd)
except IOError:
print '***** Could not copy log file(s) back to %s *****' % cwd
shutil.rmtree(tmp)
raise
# if all goes well, copy basename.pdf back to cwd and clean up temp dir
try:
copy_files(tmp, '*.pdf', cwd)
print "<br /><br />***** Wrote %s.pdf to %s *****" % (basename, cwd)
except IOError:
print "<br /><br />Could not copy %s.pdf to %s" % (basename, cwd)
subprocess.Popen(['open', os.path.join(tmp, basename + '.pdf')])
raise
finally:
shutil.rmtree(tmp)
if __name__ == "__main__":
try:
main()
except (KeyboardInterrupt, SystemExit):
# actually raise these, for a clean exit
raise
except Exception, error:
# all other exceptions: display the error
print error
# print "Stack trace:\n", traceback.print_exc(file = sys.stdout)
else:
pass
finally:
# exit cleanly once we've done everything else
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment