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.
8 from tempfile import NamedTemporaryFile
10 from librarian import OutputFile
11 from librarian.cover import WLCover
12 from librarian import get_resource
15 def transform(wldoc, verbose=False,
16 sample=None, cover=None, flags=None):
17 """ produces a MOBI file
20 sample=n: generate sample e-book (with at least n paragraphs)
21 cover: a cover.Cover object
22 flags: less-advertising,
25 book_info = wldoc.book_info
27 # provide a cover by default
30 cover_file = NamedTemporaryFile(suffix='.png', delete=False)
31 c = cover(book_info.author.readable(), book_info.title)
36 flags = list(flags) + ['without-fonts']
37 epub = wldoc.as_epub(verbose=verbose, sample=sample, html_toc=True,
38 flags=flags, style=get_resource('mobi/style.css'))
43 devnull = open("/dev/null", 'w')
44 kwargs = {"stdout": devnull, "stderr": devnull}
46 output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi', delete=False)
48 subprocess.check_call(['ebook-convert', epub.get_filename(), output_file.name,
49 '--no-inline-toc', '--cover=%s' % cover_file.name], **kwargs)
50 os.unlink(cover_file.name)
51 return OutputFile.from_filename(output_file.name)