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.
6 from copy import deepcopy
9 from tempfile import NamedTemporaryFile
11 from librarian import OutputFile
12 from librarian.cover import WLCover
13 from librarian import get_resource
16 def transform(wldoc, verbose=False,
17 sample=None, cover=None, flags=None):
18 """ produces a MOBI file
21 sample=n: generate sample e-book (with at least n paragraphs)
22 cover: a cover.Cover object
23 flags: less-advertising,
26 document = deepcopy(wldoc)
28 book_info = document.book_info
30 # provide a cover by default
33 cover_file = NamedTemporaryFile(suffix='.png', delete=False)
37 if cover.uses_dc_cover:
38 if document.book_info.cover_by:
39 document.edoc.getroot().set('data-cover-by', document.book_info.cover_by)
40 if document.book_info.cover_source:
41 document.edoc.getroot().set('data-cover-source', document.book_info.cover_source)
45 flags = list(flags) + ['without-fonts']
46 epub = document.as_epub(verbose=verbose, sample=sample, html_toc=True,
47 flags=flags, style=get_resource('mobi/style.css'))
52 devnull = open("/dev/null", 'w')
53 kwargs = {"stdout": devnull, "stderr": devnull}
55 output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi', delete=False)
57 subprocess.check_call(['ebook-convert', epub.get_filename(), output_file.name,
58 '--no-inline-toc', '--cover=%s' % cover_file.name], **kwargs)
59 os.unlink(cover_file.name)
60 return OutputFile.from_filename(output_file.name)