#!/usr/bin/env python import argparse import os import shutil import subprocess from docutils.core import publish_file def build_html(srcdir,destdir): if not os.path.isdir(destdir): os.makedirs(destdir) for fname in os.listdir(srcdir): (basename,ext) = os.path.splitext(fname) if ext == '.rst': publish_file(source_path=os.path.join(srcdir,fname), destination_path=os.path.join(destdir,basename+'.html'), writer_name="html") else: shutil.copy(os.path.join(srcdir,fname),os.path.join(destdir,fname)) def build_latex(srcdir,destdir): if not os.path.isdir(destdir): os.makedirs(destdir) for fname in os.listdir(srcdir): (basename,ext) = os.path.splitext(fname) if ext == '.rst': publish_file(source_path=os.path.join(srcdir,fname), destination_path=os.path.join(destdir,basename+'.tex'), writer_name="xetex", settings_overrides={'stylesheet':'amssymb'}) #elif ext == '.svg': # subprocess.call(['inkscape','--export-pdf', # os.path.join(destdir,basename+'pdf'), # os.path.join(srcdir,fname)]) else: shutil.copy(os.path.join(srcdir,fname),os.path.join(destdir,fname)) def build_pdf(latexdir,destdir): if not os.path.isdir(destdir): os.makedirs(destdir) destdir = os.path.abspath(destdir) os.chdir(latexdir) for fname in os.listdir('.'): (basename,ext) = os.path.splitext(fname) if ext == '.tex': if subprocess.call(['xelatex',fname]) == 0: subprocess.call(['xelatex',fname]) subprocess.call(['xelatex',fname]) shutil.copy(basename+'.pdf',destdir) def build_odt(srcdir,destdir): if not os.path.isdir(destdir): os.makedirs(destdir) for fname in os.listdir(srcdir): (basename,ext) = os.path.splitext(fname) if ext == '.rst': try: publish_file(source_path=os.path.join(srcdir,fname), destination_path=os.path.join(destdir,basename+'.odt'), writer_name="odf_odt") except IOError: # allow it to die on PIL issues pass def main(): parser = argparse.ArgumentParser(description='building script') parser.add_argument('-H','--html',action='store_true') parser.add_argument('-l','--latex',action='store_true') parser.add_argument('-p','--pdf',action='store_true') parser.add_argument('-o','--odt',action='store_true') args = parser.parse_args() if args.html: build_html('esempi',os.path.join('build','html','esempi')) if args.latex or args.pdf: build_latex('esempi',os.path.join('build','latex','esempi')) if args.pdf: build_pdf(os.path.join('build','latex','esempi'), os.path.join('build','pdf','esempi')) if args.odt: build_odt('esempi',os.path.join('build','odt','esempi')) if __name__ == '__main__': main()