From 8c190b3415461d3bd0afe415981fb66f31ccc06b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 29 Jul 2011 00:00:54 -0600 Subject: [PATCH] ... --- src/calibre/ebooks/mobi/kindlegen.py | 27 +++++++++++++++---------- src/calibre/ebooks/mobi/output.py | 2 -- src/calibre/ebooks/mobi/writer2/main.py | 5 ++--- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/calibre/ebooks/mobi/kindlegen.py b/src/calibre/ebooks/mobi/kindlegen.py index 7c626a2978..9696b82971 100644 --- a/src/calibre/ebooks/mobi/kindlegen.py +++ b/src/calibre/ebooks/mobi/kindlegen.py @@ -7,7 +7,7 @@ __license__ = 'GPL v3' __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import os, subprocess, tempfile, shutil +import os, subprocess, shutil from lxml import etree @@ -22,6 +22,12 @@ exe = 'kindlegen.exe' if iswindows else 'kindlegen' def refactor_opf(opf, is_periodical, toc): with open(opf, 'rb') as f: root = etree.fromstring(f.read()) + ''' + for spine in root.xpath('//*[local-name() = "spine" and @toc]'): + # Do not use the NCX toc as kindlegen requires the section structure + # in the TOC to be duplicated in the HTML, asinine! + del spine.attrib['toc'] + ''' if is_periodical: metadata = root.xpath('//*[local-name() = "metadata"]')[0] xm = etree.SubElement(metadata, 'x-metadata') @@ -29,7 +35,7 @@ def refactor_opf(opf, is_periodical, toc): xm.text = '\n\t' mobip = etree.SubElement(xm, 'output', attrib={'encoding':"utf-8", 'content-type':"application/x-mobipocket-subscription-magazine"}) - mobip.tail = '\n' + mobip.tail = '\n\t' with open(opf, 'wb') as f: f.write(etree.tostring(root, method='xml', encoding='utf-8', xml_declaration=True)) @@ -43,14 +49,13 @@ def refactor_guide(oeb): def run_kindlegen(opf, log): log.info('Running kindlegen on MOBIML created by calibre') oname = os.path.splitext(opf)[0] + '.mobi' - with tempfile.NamedTemporaryFile(suffix='_kindlegen_output.txt') as tfile: - p = subprocess.Popen([exe, opf, '-c1', '-verbose', '-o', oname], - stderr=subprocess.STDOUT, stdout=tfile) - returncode = p.wait() - tfile.seek(0) - log.debug('kindlegen verbose output:') - log.debug(tfile.read()) - log.info('kindlegen returned returncode: %d'%returncode) + p = subprocess.Popen([exe, opf, '-c1', '-verbose', '-o', oname], + stderr=subprocess.STDOUT, stdout=subprocess.PIPE) + ko = p.stdout.read() + returncode = p.wait() + log.debug('kindlegen verbose output:') + log.debug(ko.decode('utf-8', 'replace')) + log.info('kindlegen returned returncode: %d'%returncode) if not os.path.exists(oname) or os.stat(oname).st_size < 100: raise RuntimeError('kindlegen did not produce any output. ' 'kindlegen return code: %d'%returncode) @@ -59,7 +64,7 @@ def run_kindlegen(opf, log): def kindlegen(oeb, opts, input_plugin, output_path): is_periodical = detect_periodical(oeb.toc, oeb.log) refactor_guide(oeb) - with TemporaryDirectory('_epub_output') as tdir: + with TemporaryDirectory('_kindlegen_output') as tdir: oeb_output = plugin_for_output_format('oeb') oeb_output.convert(oeb, tdir, input_plugin, opts, oeb.log) opf = [x for x in os.listdir(tdir) if x.endswith('.opf')][0] diff --git a/src/calibre/ebooks/mobi/output.py b/src/calibre/ebooks/mobi/output.py index 35da368155..ba88aa7779 100644 --- a/src/calibre/ebooks/mobi/output.py +++ b/src/calibre/ebooks/mobi/output.py @@ -50,14 +50,12 @@ class MOBIOutput(OutputFormatPlugin): help=_('When adding the Table of Contents to the book, add it at the start of the ' 'book instead of the end. Not recommended.') ), - ''' OptionRecommendation(name='kindlegen', recommended_value=False, help=('Use kindlegen (must be in your PATH) to generate the' ' binary wrapper for the MOBI format. Useful to debug ' ' the calibre MOBI output.') ), - ''' ]) diff --git a/src/calibre/ebooks/mobi/writer2/main.py b/src/calibre/ebooks/mobi/writer2/main.py index e3f4081670..971578f5a0 100644 --- a/src/calibre/ebooks/mobi/writer2/main.py +++ b/src/calibre/ebooks/mobi/writer2/main.py @@ -366,7 +366,7 @@ class MobiWriter(object): # 0x70 - 0x73 : EXTH flags # Bit 6 (0b1000000) being set indicates the presence of an EXTH header # The purpose of the other bits is unknown - exth_flags = 0b1011000 + exth_flags = 0b1010000 if self.is_periodical: exth_flags |= 0b1000 record0.write(pack(b'>I', exth_flags)) @@ -506,7 +506,6 @@ class MobiWriter(object): if datestr is not None: datestr = bytes(datestr) - datestr = datestr.replace(b'+00:00', b'Z') exth.write(pack(b'>II', EXTH_CODES['pubdate'], len(datestr) + 8)) exth.write(datestr) nrecs += 1 @@ -514,7 +513,7 @@ class MobiWriter(object): raise NotImplementedError("missing date or timestamp needed for mobi_periodical") # Write the same creator info as kindlegen 1.2 - for code, val in [(204, 202), (205, 1), (206, 2), (207, 33307)]: + for code, val in [(204, 201), (205, 1), (206, 2), (207, 33307)]: exth.write(pack(b'>II', code, 12)) exth.write(pack(b'>I', val)) nrecs += 1