14
14
from struct import pack
16
16
from urlparse import urldefrag
18
17
from cStringIO import StringIO
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
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]
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:]
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')
1822
1823
text = "(none)".encode('utf-8')
2255
2256
return sectionIndices, sectionParents
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
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) )
2267
ctoc_map_index = i + j + 1
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
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) )
2268
ctoc_map_index = i + j + 1
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 )
2275
2276
def _add_book_chapters(self, myDoc, indxt, indices):
2276
2277
chapterCount = myDoc.documentStructure.chapterCount()