Make MOBI/AZW3 output a little closer to that of current kindlegen

This commit is contained in:
Kovid Goyal 2014-03-28 15:12:04 +05:30
parent 9b6cbd3c58
commit 5346868df7
3 changed files with 14 additions and 7 deletions

View File

@ -51,7 +51,8 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False,
nrecs = 0 nrecs = 0
for term in metadata: for term in metadata:
if term not in EXTH_CODES: continue if term not in EXTH_CODES:
continue
code = EXTH_CODES[term] code = EXTH_CODES[term]
items = metadata[term] items = metadata[term]
if term == 'creator': if term == 'creator':
@ -148,7 +149,7 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False,
nrecs += 1 nrecs += 1
if be_kindlegen2: if be_kindlegen2:
vals = {204:201, 205:2, 206:5, 207:0} vals = {204:202, 205:2, 206:9, 207:0}
elif is_periodical: elif is_periodical:
# Pretend to be amazon's super secret periodical generator # Pretend to be amazon's super secret periodical generator
vals = {204:201, 205:2, 206:0, 207:101} 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(): for code, val in vals.iteritems():
exth.write(pack(b'>III', code, 12, val)) exth.write(pack(b'>III', code, 12, val))
nrecs += 1 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: if cover_offset is not None:
exth.write(pack(b'>III', EXTH_CODES['coveroffset'], 12, 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() exth = exth.getvalue()
trail = len(exth) % 4 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] exth = [b'EXTH', pack(b'>II', len(exth) + 12, nrecs), exth, pad]
return b''.join(exth) return b''.join(exth)

View File

@ -254,12 +254,12 @@ class KF8Writer(object):
if id_ is not None: if id_ is not None:
tag.attrib['id'] = id_ tag.attrib['id'] = id_
if id_ is not None or barename(tag.tag).lower() in aid_able_tags: if id_ is not None or barename(tag.tag).lower() in aid_able_tags:
aid = aidbase + j aid = to_base(aidbase + j, base=32)
tag.attrib['aid'] = to_base(aid, base=32) tag.set('aid', aid)
if tag.tag == XHTML('body'): if tag.tag == XHTML('body'):
self.id_map[(item.href, '')] = tag.attrib['aid'] self.id_map[(item.href, '')] = aid
if id_ is not None: if id_ is not None:
self.id_map[(item.href, id_)] = tag.attrib['aid'] self.id_map[(item.href, id_)] = aid
j += 1 j += 1

View File

@ -165,6 +165,8 @@ class Chunker(object):
for i, item in enumerate(self.oeb.spine): for i, item in enumerate(self.oeb.spine):
root = self.remove_namespaces(self.data(item)) 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 = root.xpath('//body')[0]
body.tail = '\n' body.tail = '\n'