MOBI: Add support for the new language EXTH header field in MOBI files generated by kindlegen 2.5

This commit is contained in:
Kovid Goyal 2012-07-14 10:07:21 +05:30
parent 177e20f3d4
commit e49bc057e8
3 changed files with 21 additions and 4 deletions

View File

@ -163,6 +163,7 @@ class EXTHRecord(object):
501 : 'cdetype', # 4 chars (PDOC or EBOK)
502 : 'lastupdatetime',
503 : 'updatedtitle',
524 : 'language',
}.get(self.type, repr(self.type))
if (self.name in {'coveroffset', 'thumboffset', 'hasfakecover',

View File

@ -13,6 +13,7 @@ from calibre.utils.date import parse_date
from calibre.ebooks.mobi import MobiError
from calibre.ebooks.metadata import MetaInformation, check_isbn
from calibre.ebooks.mobi.langcodes import main_language, sub_language, mobi2iana
from calibre.utils.localization import canonicalize_lang
NULL_INDEX = 0xffffffff
@ -68,6 +69,14 @@ class EXTHHeader(object): # {{{
title = content.decode(codec)
except:
pass
elif idx == 524: # Lang code
try:
lang = content.decode(codec)
lang = canonicalize_lang(lang)
if lang:
self.mi.language = lang
except:
pass
#else:
# print 'unknown record', idx, repr(content)
if title:
@ -201,6 +210,7 @@ class BookHeader(object):
self.exth = EXTHHeader(raw[16 + self.length:], self.codec,
self.title)
self.exth.mi.uid = self.unique_id
if self.exth.mi.is_null('language'):
try:
self.exth.mi.language = mobi2iana(langid, sublangid)
except:

View File

@ -12,6 +12,7 @@ from struct import pack
from io import BytesIO
from calibre.ebooks.mobi.utils import utf8_text
from calibre.utils.localization import lang_as_iso639_1
EXTH_CODES = {
'creator': 100,
@ -35,6 +36,7 @@ EXTH_CODES = {
'hasfakecover': 203,
'lastupdatetime': 502,
'title': 503,
'language': 524,
}
COLLAPSE_RE = re.compile(r'[ \t\r\n\v]+')
@ -68,6 +70,10 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False,
pass
else:
continue
if term == 'language':
d2 = lang_as_iso639_1(data)
if d2:
data = d2
data = utf8_text(data)
exth.write(pack(b'>II', code, len(data) + 8))
exth.write(data)