~stub/ubuntu/precise/calibre/devel

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/mobi/writer.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-04-12 11:29:25 UTC
  • mfrom: (42.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110412112925-c7171kt2bb5rmft4
Tags: 0.7.50+dfsg-2
* debian/control: Build with libpodofo-dev to enable PDF metadata.
  (Closes: #619632)
* debian/control: Add libboost1.42-dev build dependency. Apparently it is
  needed in some setups. (Closes: #619807)
* debian/rules: Call dh_sip to generate a proper sip API dependency, to
  prevent crashes like #616372 for partial upgrades.
* debian/control: Bump python-qt4 dependency to >= 4.8.3-2, which reportedly
  fixes crashes on startup. (Closes: #619701, #620125)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
from struct import pack
15
15
import time
16
16
from urlparse import urldefrag
17
 
 
18
17
from cStringIO import StringIO
 
18
 
 
19
from calibre.ebooks import normalize
19
20
from calibre.ebooks.mobi.langcodes import iana2mobi
20
21
from calibre.ebooks.mobi.mobiml import MBP_NS
21
22
from calibre.ebooks.oeb.base import OEB_DOCS
1365
1366
            self._text_length,
1366
1367
            self._text_nrecords-1, RECORD_SIZE, 0, 0)) # 0 - 15 (0x0 - 0xf)
1367
1368
        uid = random.randint(0, 0xffffffff)
1368
 
        title = unicode(metadata.title[0]).encode('utf-8')
 
1369
        title = normalize(unicode(metadata.title[0])).encode('utf-8')
1369
1370
        # The MOBI Header
1370
1371
 
1371
1372
        # 0x0 - 0x3
1523
1524
            items = oeb.metadata[term]
1524
1525
            if term == 'creator':
1525
1526
                if self._prefer_author_sort:
1526
 
                    creators = [unicode(c.file_as or c) for c in items]
 
1527
                    creators = [normalize(unicode(c.file_as or c)) for c in items]
1527
1528
                else:
1528
 
                    creators = [unicode(c) for c in items]
 
1529
                    creators = [normalize(unicode(c)) for c in items]
1529
1530
                items = ['; '.join(creators)]
1530
1531
            for item in items:
1531
 
                data = self.COLLAPSE_RE.sub(' ', unicode(item))
 
1532
                data = self.COLLAPSE_RE.sub(' ', normalize(unicode(item)))
1532
1533
                if term == 'identifier':
1533
1534
                    if data.lower().startswith('urn:isbn:'):
1534
1535
                        data = data[9:]
1542
1543
                nrecs += 1
1543
1544
            if term == 'rights' :
1544
1545
                try:
1545
 
                    rights = unicode(oeb.metadata.rights[0]).encode('utf-8')
 
1546
                    rights = normalize(unicode(oeb.metadata.rights[0])).encode('utf-8')
1546
1547
                except:
1547
1548
                    rights = 'Unknown'
1548
1549
                exth.write(pack('>II', EXTH_CODES['rights'], len(rights) + 8))
1817
1818
            text = text.strip()
1818
1819
            if not isinstance(text, unicode):
1819
1820
                text = text.decode('utf-8', 'replace')
1820
 
            text = text.encode('utf-8')
 
1821
            text = normalize(text).encode('utf-8')
1821
1822
        else :
1822
1823
            text = "(none)".encode('utf-8')
1823
1824
        return text
2255
2256
        return sectionIndices, sectionParents
2256
2257
 
2257
2258
    def _generate_section_article_indices(self, i, section, entries, sectionIndices, sectionParents):
2258
 
                sectionArticles = list(section.iter())[1:]
2259
 
                # Iterate over the section's articles
2260
 
 
2261
 
                for (j, article) in enumerate(sectionArticles):
2262
 
                    # Recompute offset and length for each article
2263
 
                    offset, length = self._compute_offset_length(i, article, entries)
2264
 
                    if self.opts.verbose > 2 :
2265
 
                        self._oeb.logger.info( "article %02d: offset = 0x%06X length = 0x%06X" % (j, offset, length) )
2266
 
 
2267
 
                    ctoc_map_index = i + j + 1
2268
 
 
2269
 
                    #hasAuthor = self._ctoc_map[ctoc_map_index].get('authorOffset')
2270
 
                    #hasDescription = self._ctoc_map[ctoc_map_index].get('descriptionOffset')
2271
 
                    mySectionParent = sectionParents[sectionIndices[i-1]]
2272
 
                    myNewArticle = MobiArticle(mySectionParent, offset, length, ctoc_map_index )
2273
 
                    mySectionParent.addArticle( myNewArticle )
 
2259
        sectionArticles = list(section.iter())[1:]
 
2260
        # Iterate over the section's articles
 
2261
 
 
2262
        for (j, article) in enumerate(sectionArticles):
 
2263
            # Recompute offset and length for each article
 
2264
            offset, length = self._compute_offset_length(i, article, entries)
 
2265
            if self.opts.verbose > 2 :
 
2266
                self._oeb.logger.info( "article %02d: offset = 0x%06X length = 0x%06X" % (j, offset, length) )
 
2267
 
 
2268
            ctoc_map_index = i + j + 1
 
2269
 
 
2270
            #hasAuthor = self._ctoc_map[ctoc_map_index].get('authorOffset')
 
2271
            #hasDescription = self._ctoc_map[ctoc_map_index].get('descriptionOffset')
 
2272
            mySectionParent = sectionParents[sectionIndices[i-1]]
 
2273
            myNewArticle = MobiArticle(mySectionParent, offset, length, ctoc_map_index )
 
2274
            mySectionParent.addArticle( myNewArticle )
2274
2275
 
2275
2276
    def _add_book_chapters(self, myDoc, indxt, indices):
2276
2277
        chapterCount = myDoc.documentStructure.chapterCount()