not-wl support in pdf
[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.cover import ImageCover
12 from librarian.parser import WLDocument
13
14
15 if __name__ == '__main__':
16     usage = """Usage: %prog [options] SOURCE [SOURCE...]
17     Convert SOURCE files to PDF format."""
18
19     parser = OptionParser(usage)
20     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
21                       help='make lots of noise and revert to default interaction in LaTeX')
22     parser.add_option('-c', '--with-cover', action='store_true', dest='with_cover', default=False,
23                       help='create default cover')
24     parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
25                       help='create a directory for author and put the PDF in it')
26     parser.add_option('-t', '--save-tex', dest='save_tex', metavar='FILE',
27                       help='path to save the intermediary LaTeX file to')
28     parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
29                       help='specifies the output file')
30     parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
31                       help='specifies the directory for output')
32     parser.add_option('-m', '--morefloats', dest='morefloats', metavar='old/new/none',
33                       help='force morefloats in old (<1.0c), new (>=1.0c) or none')
34
35     parser.add_option('-i', '--with-images', action='store_true', dest='images', default=False,
36                       help='add images with <ilustr src="..."/>')
37     parser.add_option('-A', '--less-advertising', action='store_true', dest='less_advertising', default=False,
38                       help='less advertising, for commercial purposes')
39     parser.add_option('-W', '--not-wl', action='store_true', dest='not_wl', default=False,
40                       help='not a WolneLektury book')
41     parser.add_option('--cover', dest='cover', metavar='FILE',
42                       help='specifies the cover file')
43
44     (options, args) = parser.parse_args()
45
46     if len(args) < 1:
47         parser.print_help()
48         exit(1)
49
50     if options.output_dir and options.output_file:
51         raise ValueError("Either --output-dir or --output file should be specified")
52
53     try:
54         for main_input in args:
55             path, fname = os.path.realpath(main_input).rsplit('/', 1)
56             provider = DirDocProvider(path)
57             output_file, output_dir = options.output_file, options.output_dir
58             if not (options.output_file or options.output_dir):
59                 output_file = os.path.splitext(main_input)[0] + '.pdf'
60             else:
61                 output_file = None
62
63             doc = WLDocument.from_file(main_input, provider=provider)
64
65             if options.cover:
66                 cover = ImageCover(options.cover)
67             else:
68                 cover = options.with_cover
69
70             flags = []
71             if options.images:
72                 flags.append('images')
73             if options.less_advertising:
74                 flags.append('less-advertising')
75             if options.not_wl:
76                 flags.append('not-wl')
77
78             pdf = doc.as_pdf(save_tex=options.save_tex,
79                         cover=cover,
80                         flags=flags,
81                         morefloats=options.morefloats,
82                         verbose=options.verbose
83                     )
84
85             doc.save_output_file(pdf,
86                 output_file, options.output_dir, options.make_dir, 'pdf')
87     except ParseError, e:
88         print '%(file)s:%(name)s:%(message)s; use -v to see more output' % {
89             'file': main_input,
90             'name': e.__class__.__name__,
91             'message': e
92         }