2 # -*- coding: utf-8 -*-
4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from collections import namedtuple
11 from librarian import DirDocProvider, ParseError
12 from librarian.parser import WLDocument
13 from librarian.cover import DefaultEbookCover
17 """Option for optparse. Use it like `optparse.OptionParser.add_option`."""
18 def __init__(self, *names, **options):
20 self.options = options
22 def add(self, parser):
23 parser.add_option(*self.names, **self.options)
26 return self.options['dest']
28 def value(self, options):
29 return getattr(options, self.name())
32 class Book2Anything(object):
33 """A class for creating book2... scripts.
35 Subclass it for any format you want to convert to.
37 format_name = None # Set format name, like "PDF".
38 ext = None # Set file extension, like "pdf".
39 uses_cover = False # Can it add a cover?
40 cover_optional = True # Only relevant if uses_cover
41 uses_provider = False # Does it need a DocProvider?
42 transform = None # Transform method. Uses WLDocument.as_{ext} by default.
43 parser_options = [] # List of Option objects for additional parser args.
44 transform_options = [] # List of Option objects for additional transform args.
45 transform_flags = [] # List of Option objects for supported transform flags.
50 # Parse commandline arguments
51 usage = """Usage: %%prog [options] SOURCE [SOURCE...]
52 Convert SOURCE files to %s format.""" % cls.format_name
54 parser = optparse.OptionParser(usage=usage)
56 parser.add_option('-v', '--verbose',
57 action='store_true', dest='verbose', default=False,
58 help='print status messages to stdout')
59 parser.add_option('-t', '--html-toc',
60 action='store_true', dest='html_toc', default=False,
61 help='with inline html toc [book2epub only]')
62 parser.add_option('-k', '--use-kindlegen',
63 action='store_true', dest='use_kindlegen', default=False,
64 help='use kindlegen tool [book2mobi only]')
65 parser.add_option('-d', '--make-dir',
66 action='store_true', dest='make_dir', default=False,
67 help='create a directory for author and put the output file in it')
68 parser.add_option('-o', '--output-file',
69 dest='output_file', metavar='FILE',
70 help='specifies the output file')
71 parser.add_option('-O', '--output-dir',
72 dest='output_dir', metavar='DIR',
73 help='specifies the directory for output')
75 if cls.cover_optional:
76 parser.add_option('-c', '--with-cover',
77 action='store_true', dest='with_cover', default=False,
78 help='create default cover')
79 parser.add_option('-C', '--image-cache',
80 dest='image_cache', metavar='URL',
81 help='prefix for image download cache' +
82 (' (implies --with-cover)' if cls.cover_optional else ''))
83 for option in cls.parser_options + cls.transform_options + cls.transform_flags:
86 options, input_filenames = parser.parse_args()
88 if len(input_filenames) < 1:
92 # Prepare additional args for parser.
94 for option in cls.parser_options:
95 parser_args[option.name()] = option.value(options)
96 # Prepare additional args for transform method.
98 for option in cls.transform_options:
99 transform_args[option.name()] = option.value(options)
100 # Add flags to transform_args, if any.
101 transform_flags = [flag.name() for flag in cls.transform_flags
102 if flag.value(options)]
104 transform_args['flags'] = transform_flags
106 transform_args['verbose'] = True
107 if options.html_toc and cls.ext == 'epub':
108 transform_args['html_toc'] = True
109 if options.use_kindlegen and cls.ext == 'mobi':
110 transform_args['use_kindlegen'] = True
111 # Add cover support, if any.
113 if options.image_cache:
114 def cover_class(*args, **kwargs):
115 return DefaultEbookCover(image_cache=options.image_cache, *args, **kwargs)
116 transform_args['cover'] = cover_class
117 elif not cls.cover_optional or options.with_cover:
118 transform_args['cover'] = DefaultEbookCover
123 for main_input in input_filenames:
127 # Where to find input?
128 if cls.uses_provider:
129 path, fname = os.path.realpath(main_input).rsplit('/', 1)
130 provider = DirDocProvider(path)
134 # Where to write output?
135 if not (options.output_file or options.output_dir):
136 output_file = os.path.splitext(main_input)[0] + '.' + cls.ext
140 # Do the transformation.
141 doc = WLDocument.from_file(main_input, provider=provider, **parser_args)
142 transform = cls.transform
143 if transform is None:
144 transform = getattr(WLDocument, 'as_%s' % cls.ext)
145 output = transform(doc, **transform_args)
147 doc.save_output_file(output,
148 output_file, options.output_dir, options.make_dir, cls.ext)
150 except ParseError, e:
151 print '%(file)s:%(name)s:%(message)s' % {
153 'name': e.__class__.__name__,