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 DefaultEbookCover
13 from librarian import get_resource
16 def transform(wldoc, verbose=False, sample=None, cover=None,
17 use_kindlegen=False, flags=None):
18 """ produces a MOBI file
21 sample=n: generate sample e-book (with at least n paragraphs)
22 cover: a cover.Cover factory overriding default
23 flags: less-advertising,
26 document = deepcopy(wldoc)
28 book_info = document.book_info
34 epub = document.as_epub(verbose=verbose, sample=sample,
35 html_toc=True, cover=True, flags=flags)
39 devnull = open("/dev/null", 'w')
40 kwargs = {"stdout": devnull, "stderr": devnull}
42 output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi', delete=False)
46 output_file_basename = os.path.basename(output_file.name)
47 subprocess.check_call(
48 ['kindlegen', '-c2', epub.get_filename(), '-o', output_file_basename],
52 subprocess.check_call(
53 ['ebook-convert', epub.get_filename(), output_file.name,
55 '--mobi-file-type=both',
56 '--mobi-ignore-margins'], **kwargs
58 return OutputFile.from_filename(output_file.name)