~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/mobi/writer8/skeleton.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-05-14 18:17:50 UTC
  • mto: This revision was merged to the branch mainline in revision 75.
  • Revision ID: package-import@ubuntu.com-20140514181750-efj1wymey2vb4cao
Tags: upstream-1.36.0+dfsg
ImportĀ upstreamĀ versionĀ 1.36.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
165
165
 
166
166
        for i, item in enumerate(self.oeb.spine):
167
167
            root = self.remove_namespaces(self.data(item))
 
168
            for child in root.xpath('//*[@aid]'):
 
169
                child.set('aid', child.attrib.pop('aid'))  # kindlegen always puts the aid last
168
170
            body = root.xpath('//body')[0]
169
171
            body.tail = '\n'
170
172
 
199
201
 
200
202
        # Set internal links
201
203
        text = b''.join(x.raw_text for x in self.skeletons)
202
 
        self.text = self.set_internal_links(text)
 
204
        self.text = self.set_internal_links(text, b''.join(x.rebuild() for x in self.skeletons))
203
205
 
204
206
    def remove_namespaces(self, root):
205
207
        lang = None
250
252
        first_chunk_idx = len(chunks)
251
253
 
252
254
        # First handle any text
253
 
        if tag.text and tag.text.strip(): # Leave pure whitespace in the skel
 
255
        if tag.text and tag.text.strip():  # Leave pure whitespace in the skel
254
256
            chunks.extend(self.chunk_up_text(tag.text))
255
257
            tag.text = None
256
258
 
265
267
            raw = close_self_closing_tags(raw)
266
268
            if len(raw) > CHUNK_SIZE and child.get('aid', None):
267
269
                self.step_into_tag(child, chunks)
268
 
                if child.tail and child.tail.strip(): # Leave pure whitespace
 
270
                if child.tail and child.tail.strip():  # Leave pure whitespace
269
271
                    chunks.extend(self.chunk_up_text(child.tail))
270
272
                    child.tail = None
271
273
            else:
313
315
        for chunk in chunks[1:]:
314
316
            prev = ans[-1]
315
317
            if (
316
 
                    chunk.starts_tags or # Starts a tag in the skel
317
 
                    len(chunk) + len(prev) > CHUNK_SIZE or # Too large
318
 
                    prev.ends_tags # Prev chunk ended a tag
 
318
                    chunk.starts_tags or  # Starts a tag in the skel
 
319
                    len(chunk) + len(prev) > CHUNK_SIZE or  # Too large
 
320
                    prev.ends_tags  # Prev chunk ended a tag
319
321
                    ):
320
322
                ans.append(chunk)
321
323
            else:
344
346
                cp += len(chunk.raw)
345
347
                num += 1
346
348
 
347
 
    def set_internal_links(self, text):
 
349
    def set_internal_links(self, text, rebuilt_text):
348
350
        ''' Update the internal link placeholders to point to the correct
349
351
        location, based on the chunk table.'''
350
 
        # A kindle:pos:fid link contains two base 32 numbers of the form
 
352
        # A kindle:pos:fid:off link contains two base 32 numbers of the form
351
353
        # XXXX:YYYYYYYYYY
352
354
        # The first number is an index into the chunk table and the second is
353
355
        # an offset from the start of the chunk to the start of the tag pointed
354
356
        # to by the link.
355
 
        aid_map = {} # Map of aid to (pos, fid)
356
 
        for match in re.finditer(br'<[^>]+? aid=[\'"]([A-Z0-9]+)[\'"]', text):
 
357
        aid_map = {}  # Map of aid to (fid, offset_from_start_of_chunk, offset_from_start_of_text)
 
358
        for match in re.finditer(br'<[^>]+? aid=[\'"]([A-Z0-9]+)[\'"]', rebuilt_text):
357
359
            offset = match.start()
358
360
            pos_fid = None
359
361
            for chunk in self.chunk_table:
367
369
                    pos_fid = (chunk.sequence_number, 0, offset)
368
370
                    break
369
371
                if chunk is self.chunk_table[-1]:
370
 
                    # This can happen for aids very close to the end of the the
371
 
                    # end of the text (https://bugs.launchpad.net/bugs/1011330)
 
372
                    # This can happen for aids very close to the end of the
 
373
                    # text (https://bugs.launchpad.net/bugs/1011330)
372
374
                    pos_fid = (chunk.sequence_number, offset-chunk.insert_pos,
373
375
                            offset)
374
376
            if pos_fid is None: