From 5346868df70227da455355c6730c39829c76c033 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 28 Mar 2014 15:12:04 +0530 Subject: [PATCH] Make MOBI/AZW3 output a little closer to that of current kindlegen --- src/calibre/ebooks/mobi/writer8/exth.py | 11 ++++++++--- src/calibre/ebooks/mobi/writer8/main.py | 8 ++++---- src/calibre/ebooks/mobi/writer8/skeleton.py | 2 ++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/calibre/ebooks/mobi/writer8/exth.py b/src/calibre/ebooks/mobi/writer8/exth.py index 31792d2156..81a0c7669d 100644 --- a/src/calibre/ebooks/mobi/writer8/exth.py +++ b/src/calibre/ebooks/mobi/writer8/exth.py @@ -51,7 +51,8 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False, nrecs = 0 for term in metadata: - if term not in EXTH_CODES: continue + if term not in EXTH_CODES: + continue code = EXTH_CODES[term] items = metadata[term] if term == 'creator': @@ -148,7 +149,7 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False, nrecs += 1 if be_kindlegen2: - vals = {204:201, 205:2, 206:5, 207:0} + vals = {204:202, 205:2, 206:9, 207:0} elif is_periodical: # Pretend to be amazon's super secret periodical generator vals = {204:201, 205:2, 206:0, 207:101} @@ -158,6 +159,10 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False, for code, val in vals.iteritems(): exth.write(pack(b'>III', code, 12, val)) nrecs += 1 + if be_kindlegen2: + revnum = b'0730-890adc2' + exth.write(pack(b'>II', 535, 8 + len(revnum)) + revnum) + nrecs += 1 if cover_offset is not None: exth.write(pack(b'>III', EXTH_CODES['coveroffset'], 12, @@ -200,7 +205,7 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False, exth = exth.getvalue() trail = len(exth) % 4 - pad = b'\0' * (4 - trail) # Always pad w/ at least 1 byte + pad = b'\0' * (4 - trail) # Always pad w/ at least 1 byte exth = [b'EXTH', pack(b'>II', len(exth) + 12, nrecs), exth, pad] return b''.join(exth) diff --git a/src/calibre/ebooks/mobi/writer8/main.py b/src/calibre/ebooks/mobi/writer8/main.py index 781fdec779..b291691d62 100644 --- a/src/calibre/ebooks/mobi/writer8/main.py +++ b/src/calibre/ebooks/mobi/writer8/main.py @@ -254,12 +254,12 @@ class KF8Writer(object): if id_ is not None: tag.attrib['id'] = id_ if id_ is not None or barename(tag.tag).lower() in aid_able_tags: - aid = aidbase + j - tag.attrib['aid'] = to_base(aid, base=32) + aid = to_base(aidbase + j, base=32) + tag.set('aid', aid) if tag.tag == XHTML('body'): - self.id_map[(item.href, '')] = tag.attrib['aid'] + self.id_map[(item.href, '')] = aid if id_ is not None: - self.id_map[(item.href, id_)] = tag.attrib['aid'] + self.id_map[(item.href, id_)] = aid j += 1 diff --git a/src/calibre/ebooks/mobi/writer8/skeleton.py b/src/calibre/ebooks/mobi/writer8/skeleton.py index 3bf56c24d6..26106e4665 100644 --- a/src/calibre/ebooks/mobi/writer8/skeleton.py +++ b/src/calibre/ebooks/mobi/writer8/skeleton.py @@ -165,6 +165,8 @@ class Chunker(object): for i, item in enumerate(self.oeb.spine): root = self.remove_namespaces(self.data(item)) + for child in root.xpath('//*[@aid]'): + child.set('aid', child.attrib.pop('aid')) # kindlegen always puts the aid last body = root.xpath('//body')[0] body.tail = '\n'