add skips between lines in drama
[librarian.git] / librarian / packagers.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import os
7 from librarian import pdf, epub, mobi, DirDocProvider, ParseError
8 from librarian.parser import WLDocument
9
10 from util import makedirs
11
12
13 class Packager(object):
14     cover = None
15     flags = None
16
17     @classmethod
18     def transform(cls, *args, **kwargs):
19         return cls.converter.transform(*args, **kwargs)
20
21     @classmethod
22     def prepare_file(cls, main_input, output_dir, verbose=False, overwrite=False):
23         path, fname = os.path.realpath(main_input).rsplit('/', 1)
24         provider = DirDocProvider(path)
25         slug, ext = os.path.splitext(fname)
26
27         if output_dir != '':
28             makedirs(output_dir)
29         outfile = os.path.join(output_dir, slug + '.' + cls.ext)
30         if os.path.exists(outfile) and not overwrite:
31             return
32
33         doc = WLDocument.from_file(main_input, provider=provider)
34         output_file = cls.transform(doc, cover=cls.cover, flags=cls.flags)
35         doc.save_output_file(output_file, output_path=outfile)
36
37     @classmethod
38     def prepare(cls, input_filenames, output_dir='', verbose=False, overwrite=False):
39         try:
40             for main_input in input_filenames:
41                 if verbose:
42                     print main_input
43                 cls.prepare_file(main_input, output_dir, verbose, overwrite)
44         except ParseError, e:
45             print '%(file)s:%(name)s:%(message)s' % {
46                 'file': main_input,
47                 'name': e.__class__.__name__,
48                 'message': e.message
49             }
50
51
52 class EpubPackager(Packager):
53     converter = epub
54     ext = 'epub'
55
56
57 class MobiPackager(Packager):
58     converter = mobi
59     ext = 'mobi'
60
61
62 class PdfPackager(Packager):
63     converter = pdf
64     ext = 'pdf'
65
66     @classmethod
67     def transform(cls, *args, **kwargs):
68         return cls.converter.transform(*args, morefloats='new', **kwargs)