diff --git a/src/calibre/ebooks/mobi/writer8/main.py b/src/calibre/ebooks/mobi/writer8/main.py index c9334b22a3..430d695fd1 100644 --- a/src/calibre/ebooks/mobi/writer8/main.py +++ b/src/calibre/ebooks/mobi/writer8/main.py @@ -175,7 +175,7 @@ class KF8Writer(object): self.link_map = {} count = 0 hrefs = {item.href for item in self.oeb.spine} - for i, item in enumerate(self.oeb.spine): + for item in self.oeb.spine: root = self.data(item) for a in XPath('//h:a[@href]')(root): @@ -184,8 +184,7 @@ class KF8Writer(object): href, _, frag = ref.partition('#') href = urlnormalize(href) if href in hrefs: - placeholder = 'kindle:pos:fid:%04d:off:%s'%(i, - to_href(count)) + placeholder = 'kindle:pos:fid:0000:off:%s'%to_href(count) self.link_map[placeholder] = (href, frag) a.set('href', placeholder) @@ -201,9 +200,9 @@ class KF8Writer(object): aid = aidbase + j tag.attrib['aid'] = to_base(aid, base=32) if tag.tag == XHTML('body'): - self.id_map[(item.href, '')] = tag.attrib['aid'] + self.id_map[(item.href, '')] = (i, tag.attrib['aid']) if id_ is not None: - self.id_map[(item.href, id_)] = tag.attrib['aid'] + self.id_map[(item.href, id_)] = (i, tag.attrib['aid']) j += 1 diff --git a/src/calibre/ebooks/mobi/writer8/skeleton.py b/src/calibre/ebooks/mobi/writer8/skeleton.py index 4b39d0cb15..494aa30def 100644 --- a/src/calibre/ebooks/mobi/writer8/skeleton.py +++ b/src/calibre/ebooks/mobi/writer8/skeleton.py @@ -341,7 +341,12 @@ class Chunker(object): for match in re.finditer(br'<[^>]+? aid=[\'"]([A-Z0-9]+)[\'"]', text): aid_map[match.group(1)] = match.start() self.aid_offset_map = aid_map - placeholder_map = {bytes(k):bytes(to_href(aid_map[v])) for k, v in + + def to_placeholder(x): + file_number, aid = x + return bytes('%04d:%s'%(file_number, to_href(aid_map[aid]))) + + placeholder_map = {bytes(k):to_placeholder(v) for k, v in self.placeholder_map.iteritems()} # Now update the links @@ -349,7 +354,7 @@ class Chunker(object): raw = match.group() pl = match.group(1) try: - return raw[:-10] + placeholder_map[pl] + return raw[:-15] + placeholder_map[pl] except KeyError: pass return raw