fix
[librarian.git] / scripts / book2epub
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 import optparse
9
10 from librarian import epub, DirDocProvider, ParseError
11 from librarian.cover import ImageCover
12
13
14 if __name__ == '__main__':
15     # Parse commandline arguments
16     usage = """Usage: %prog [options] SOURCE [SOURCE...]
17     Convert SOURCE files to EPUB format."""
18
19     parser = optparse.OptionParser(usage=usage)
20
21     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
22         help='print status messages to stdout')
23     parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
24                       help='create a directory for author and put the PDF in it')
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('-i', '--with-images', action='store_true', dest='images', default=False,
30                       help='add images with <ilustr src="..."/>')
31     parser.add_option('-A', '--less-advertising', action='store_true', dest='less_advertising', default=False,
32                       help='less advertising, for commercial purposes')
33     parser.add_option('-W', '--not-wl', action='store_true', dest='not_wl', default=False,
34                       help='not a WolneLektury book')
35     parser.add_option('-c', '--cover', dest='cover', metavar='FILE',
36                       help='specifies the cover file')
37
38     options, input_filenames = parser.parse_args()
39
40     if len(input_filenames) < 1:
41         parser.print_help()
42         exit(1)
43
44     # Do some real work
45     try:
46         for main_input in input_filenames:
47             if options.verbose:
48                 print main_input
49             path, fname = os.path.realpath(main_input).rsplit('/', 1)
50             provider = DirDocProvider(path)
51
52             output_dir = output_file = None
53             if options.output_dir:
54                 output_dir = options.output_dir
55             elif options.output_file:
56                 output_file = options.output_file
57             else:
58                 output_dir = path
59
60             cover = None
61             if options.cover:
62                 cover = ImageCover(options.cover)
63
64             flags = []
65             if options.images:
66                 flags.append('images')
67             if options.less_advertising:
68                 flags.append('less-advertising')
69             if options.not_wl:
70                 flags.append('not-wl')
71
72             epub.transform(provider, file_path=main_input, output_dir=output_dir, output_file=output_file, make_dir=options.make_dir,
73                 cover=cover, flags=flags)
74     except ParseError, e:
75         print '%(file)s:%(name)s:%(message)s' % {
76             'file': main_input,
77             'name': e.__class__.__name__,
78             'message': e
79         }