1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
9 from tempfile import NamedTemporaryFile
11 from librarian.cover import WLCover
12 from librarian import epub, get_resource
15 def transform(provider, slug=None, file_path=None, output_file=None, output_dir=None, make_dir=False, verbose=False,
16 sample=None, cover=None, flags=None):
17 """ produces a MOBI file
19 provider: a DocProvider
20 slug: slug of file to process, available by provider
21 output_file: path to output file
22 output_dir: path to directory to save output file to; either this or output_file must be present
23 make_dir: writes output to <output_dir>/<author>/<slug>.mobi instead of <output_dir>/<slug>.mobi
24 sample=n: generate sample e-book (with at least n paragraphs)
25 cover: a cover.Cover object
26 flags: less-advertising,
29 # if output to dir, create the file
30 if output_dir is not None:
32 author = unicode(book_info.author)
33 output_dir = os.path.join(output_dir, author)
35 os.makedirs(output_dir)
39 output_file = os.path.join(output_dir, '%s.mobi' % slug)
41 output_file = os.path.join(output_dir, os.path.splitext(os.path.basename(file_path))[0] + '.mobi')
43 # provide a cover by default
47 epub_file = NamedTemporaryFile(suffix='.epub', delete=False)
50 flags = list(flags) + ['without-fonts']
51 epub.transform(provider, file_path=file_path, output_file=epub_file, verbose=verbose,
52 sample=sample, html_toc=True, cover=cover, flags=flags, style=get_resource('mobi/style.css'))
57 devnull = open("/dev/null", 'w')
58 kwargs = {"stdout": devnull, "stderr": devnull}
59 subprocess.check_call(['ebook-convert', epub_file.name, output_file,
60 '--no-inline-toc'], **kwargs)
61 os.unlink(epub_file.name)