532577cb506dfc49c2aa2e537abf0e66405f3c25
[librarian.git] / scripts / book2pdf
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 #
7 import os.path
8 from optparse import OptionParser
9 from librarian import pdf, DirDocProvider, ParseError
10
11 if __name__ == '__main__':
12     usage = """Usage: %prog [options] SOURCE [SOURCE...]
13     Convert SOURCE files to PDF format."""
14
15     parser = OptionParser(usage)
16     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
17                       help='make lots of noise and revert to default interaction in LaTeX')
18     parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
19                       help='create a directory for author and put the PDF in it')
20     parser.add_option('-t', '--save-tex', dest='save_tex', metavar='FILE',
21                       help='path to save the intermediary LaTeX file to')
22     parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
23                       help='specifies the output file')
24     parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
25                       help='specifies the directory for output')
26     parser.add_option('-m', '--morefloats', dest='morefloats', metavar='old/new/none',
27                       help='force morefloats in old (<1.0c), new (>=1.0c) or none')
28     (options, args) = parser.parse_args()
29
30     if len(args) < 1:
31         parser.print_help()
32         exit(1)
33
34     try:
35         if options.output_dir and options.output_file:
36             raise ValueError("Either --output-dir or --output file should be specified")
37
38         for main_input in args:
39             if options.verbose:
40                 print main_input
41             path, fname = os.path.realpath(main_input).rsplit('/', 1)
42             provider = DirDocProvider(path)
43
44             output_file = output_dir = None
45             if options.output_dir:
46                 output_dir = options.output_dir
47             elif options.output_file:
48                 output_file = options.output_file
49             else:
50                 output_dir = path
51
52             pdf.transform(provider,
53                 file_path=main_input,
54                 output_file=output_file,
55                 output_dir=output_dir,
56                 verbose=options.verbose,
57                 make_dir=options.make_dir,
58                 save_tex=options.save_tex,
59                 morefloats=options.morefloats
60                 )
61     except ParseError, e:
62         print '%(file)s:%(name)s:%(message)s; use -v to see more output' % {
63             'file': main_input,
64             'name': e.__class__.__name__,
65             'message': e.message
66         }