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