aboutsummaryrefslogtreecommitdiff
path: root/build.py
blob: f811420d9ec84ee93b6d1623080131cb4abd870c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3

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",
                    settings_overrides={'initial_header_level':'2',
                        'language_code':'it'})
        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',
                        'language_code':'it'})
        #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)

    old_dir = os.path.abspath(os.getcwd())
    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)
    os.chdir(old_dir)

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",
                        settings_overrides={'language_code':'it'})
            except IOError: # allow it to die on PIL issues
                pass

def main():
    parser = argparse.ArgumentParser(description='building script')
    parser.add_argument('directory',nargs='+',choices=['esempi','strumenti'])
    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()

    for dirname in args.directory:
        if args.html:
            build_html(dirname,os.path.join('build','html',dirname))
        if args.latex or args.pdf:
            build_latex(dirname,os.path.join('build','latex',dirname))
        if args.pdf:
            build_pdf(os.path.join('build','latex',dirname),
                    os.path.join('build','pdf',dirname))
        if args.odt:
            build_odt(dirname,os.path.join('build','odt',dirname))

if __name__ == '__main__': main()