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 __future__ import unicode_literals
8 from copy import deepcopy
11 from tempfile import NamedTemporaryFile
13 from librarian import OutputFile
16 def transform(wldoc, verbose=False, sample=None, cover=None,
17 use_kindlegen=False, flags=None, hyphenate=True, base_url='',
19 """ produces a MOBI file
22 sample=n: generate sample e-book (with at least n paragraphs)
23 cover: a cover.Cover factory overriding default
24 flags: less-advertising,
25 converter_path: override path to MOBI converter,
26 either ebook-convert or kindlegen
29 document = deepcopy(wldoc)
32 epub = document.as_epub(verbose=verbose, sample=sample,
33 cover=cover or True, flags=flags,
34 hyphenate=hyphenate, base_url=base_url,
39 devnull = open("/dev/null", 'w')
40 kwargs = {"stdout": devnull, "stderr": devnull}
42 output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi',
47 output_file_basename = os.path.basename(output_file.name)
48 subprocess.check_call([converter_path or 'kindlegen',
49 '-c2', epub.get_filename(),
50 '-o', output_file_basename], **kwargs)
52 subprocess.check_call([converter_path or 'ebook-convert',
54 output_file.name, '--no-inline-toc',
55 '--mobi-file-type=both',
56 '--mobi-ignore-margins'], **kwargs)
57 return OutputFile.from_filename(output_file.name)