diff --git a/src/calibre/ebooks/mobi/reader/headers.py b/src/calibre/ebooks/mobi/reader/headers.py index f7ce8e2086..51d56bc71f 100644 --- a/src/calibre/ebooks/mobi/reader/headers.py +++ b/src/calibre/ebooks/mobi/reader/headers.py @@ -15,6 +15,7 @@ from calibre.ebooks.metadata import MetaInformation, check_isbn from calibre.ebooks.mobi.langcodes import main_language, sub_language, mobi2iana from calibre.utils.cleantext import clean_ascii_chars, clean_xml_chars from calibre.utils.localization import canonicalize_lang +from calibre.utils.config_base import tweaks NULL_INDEX = 0xffffffff @@ -90,9 +91,18 @@ class EXTHHeader(object): # {{{ if self.mi.is_null('authors'): self.mi.authors = [] au = clean_xml_chars(self.decode(content).strip()) - self.mi.authors.append(au) - if self.mi.is_null('author_sort') and re.match(r'\S+?\s*,\s+\S+', au.strip()): - self.mi.author_sort = au.strip() + # Author names in Amazon MOBI files are usually in LN, FN format, + # try to detect and auto-correct that. + m = re.match(r'([^,]+?)\s*,\s+([^,]+)$', au.strip()) + if m is not None: + if tweaks['author_sort_copy_method'] != 'copy': + self.mi.authors.append(m.group(2) + ' ' + m.group(1)) + else: + self.mi.authors.append(m.group()) + if self.mi.is_null('author_sort'): + self.mi.author_sort = m.group() + else: + self.mi.authors.append(au) elif idx == 101: self.mi.publisher = clean_xml_chars(self.decode(content).strip()) if self.mi.publisher in {'Unknown', _('Unknown')}: