mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
MOBI: Add support for the new language EXTH header field in MOBI files generated by kindlegen 2.5
This commit is contained in:
parent
177e20f3d4
commit
e49bc057e8
@ -163,6 +163,7 @@ class EXTHRecord(object):
|
|||||||
501 : 'cdetype', # 4 chars (PDOC or EBOK)
|
501 : 'cdetype', # 4 chars (PDOC or EBOK)
|
||||||
502 : 'lastupdatetime',
|
502 : 'lastupdatetime',
|
||||||
503 : 'updatedtitle',
|
503 : 'updatedtitle',
|
||||||
|
524 : 'language',
|
||||||
}.get(self.type, repr(self.type))
|
}.get(self.type, repr(self.type))
|
||||||
|
|
||||||
if (self.name in {'coveroffset', 'thumboffset', 'hasfakecover',
|
if (self.name in {'coveroffset', 'thumboffset', 'hasfakecover',
|
||||||
|
@ -13,6 +13,7 @@ from calibre.utils.date import parse_date
|
|||||||
from calibre.ebooks.mobi import MobiError
|
from calibre.ebooks.mobi import MobiError
|
||||||
from calibre.ebooks.metadata import MetaInformation, check_isbn
|
from calibre.ebooks.metadata import MetaInformation, check_isbn
|
||||||
from calibre.ebooks.mobi.langcodes import main_language, sub_language, mobi2iana
|
from calibre.ebooks.mobi.langcodes import main_language, sub_language, mobi2iana
|
||||||
|
from calibre.utils.localization import canonicalize_lang
|
||||||
|
|
||||||
NULL_INDEX = 0xffffffff
|
NULL_INDEX = 0xffffffff
|
||||||
|
|
||||||
@ -68,6 +69,14 @@ class EXTHHeader(object): # {{{
|
|||||||
title = content.decode(codec)
|
title = content.decode(codec)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
elif idx == 524: # Lang code
|
||||||
|
try:
|
||||||
|
lang = content.decode(codec)
|
||||||
|
lang = canonicalize_lang(lang)
|
||||||
|
if lang:
|
||||||
|
self.mi.language = lang
|
||||||
|
except:
|
||||||
|
pass
|
||||||
#else:
|
#else:
|
||||||
# print 'unknown record', idx, repr(content)
|
# print 'unknown record', idx, repr(content)
|
||||||
if title:
|
if title:
|
||||||
@ -201,6 +210,7 @@ class BookHeader(object):
|
|||||||
self.exth = EXTHHeader(raw[16 + self.length:], self.codec,
|
self.exth = EXTHHeader(raw[16 + self.length:], self.codec,
|
||||||
self.title)
|
self.title)
|
||||||
self.exth.mi.uid = self.unique_id
|
self.exth.mi.uid = self.unique_id
|
||||||
|
if self.exth.mi.is_null('language'):
|
||||||
try:
|
try:
|
||||||
self.exth.mi.language = mobi2iana(langid, sublangid)
|
self.exth.mi.language = mobi2iana(langid, sublangid)
|
||||||
except:
|
except:
|
||||||
|
@ -12,6 +12,7 @@ from struct import pack
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from calibre.ebooks.mobi.utils import utf8_text
|
from calibre.ebooks.mobi.utils import utf8_text
|
||||||
|
from calibre.utils.localization import lang_as_iso639_1
|
||||||
|
|
||||||
EXTH_CODES = {
|
EXTH_CODES = {
|
||||||
'creator': 100,
|
'creator': 100,
|
||||||
@ -35,6 +36,7 @@ EXTH_CODES = {
|
|||||||
'hasfakecover': 203,
|
'hasfakecover': 203,
|
||||||
'lastupdatetime': 502,
|
'lastupdatetime': 502,
|
||||||
'title': 503,
|
'title': 503,
|
||||||
|
'language': 524,
|
||||||
}
|
}
|
||||||
|
|
||||||
COLLAPSE_RE = re.compile(r'[ \t\r\n\v]+')
|
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
|
pass
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
if term == 'language':
|
||||||
|
d2 = lang_as_iso639_1(data)
|
||||||
|
if d2:
|
||||||
|
data = d2
|
||||||
data = utf8_text(data)
|
data = utf8_text(data)
|
||||||
exth.write(pack(b'>II', code, len(data) + 8))
|
exth.write(pack(b'>II', code, len(data) + 8))
|
||||||
exth.write(data)
|
exth.write(data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user