converters interface changed: WLDocument in, OutputFile out
[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
10 from librarian import DirDocProvider, ParseError
11 from librarian.parser import WLDocument
12
13
14 if __name__ == '__main__':
15     usage = """Usage: %prog [options] SOURCE [SOURCE...]
16     Convert SOURCE files to PDF format."""
17
18     parser = OptionParser(usage)
19     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
20                       help='make lots of noise and revert to default interaction in LaTeX')
21     parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
22                       help='create a directory for author and put the PDF in it')
23     parser.add_option('-t', '--save-tex', dest='save_tex', metavar='FILE',
24                       help='path to save the intermediary LaTeX file to')
25     parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
26                       help='specifies the output file')
27     parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
28                       help='specifies the directory for output')
29     parser.add_option('-m', '--morefloats', dest='morefloats', metavar='old/new/none',
30                       help='force morefloats in old (<1.0c), new (>=1.0c) or none')
31     (options, args) = parser.parse_args()
32
33     if len(args) < 1:
34         parser.print_help()
35         exit(1)
36
37     if options.output_dir and options.output_file:
38         raise ValueError("Either --output-dir or --output file should be specified")
39
40     try:
41         for main_input in args:
42             path, fname = os.path.realpath(main_input).rsplit('/', 1)
43             provider = DirDocProvider(path)
44             output_file, output_dir = options.output_file, options.output_dir
45             if not (options.output_file or options.output_dir):
46                 output_file = os.path.splitext(main_input)[0] + '.pdf'
47             else:
48                 output_file = None
49
50             doc = WLDocument.from_file(main_input, provider=provider)
51             pdf = doc.as_pdf(save_tex=options.save_tex,
52                         morefloats=options.morefloats)
53
54             doc.save_output_file(pdf,
55                 output_file, options.output_dir, options.make_dir, 'pdf')
56     except ParseError, e:
57         print '%(file)s:%(name)s:%(message)s; use -v to see more output' % {
58             'file': main_input,
59             'name': e.__class__.__name__,
60             'message': e
61         }