From 3d6204db86212ac14ab20743357da931cdf4e8a0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 22 Apr 2014 10:30:41 +0530 Subject: [PATCH] When adding MOBI files if the author name is in LN, FN format, auto change it to FN LN format. This behavior can be disabled by setting Preferences->Tweaks->Author sort name algorithm to copy. Fixes #1306748 [Author attribute is imported incorrectly when importing Kindle e-book files](https://bugs.launchpad.net/calibre/+bug/1306748) --- src/calibre/ebooks/mobi/reader/headers.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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')}: