diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index ead9995eb3..f12121dd89 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -61,7 +61,7 @@ authors_completer_append_separator = False # selecting 'manage authors', and pressing 'Recalculate all author sort values'. # The author name suffixes are words that are ignored when they occur at the # end of an author name. The case of the suffix is ignored and trailing -# periods are automatically handled. +# periods are automatically handled. The same is true for prefixes. # The author name copy words are a set of words which if they occur in an # author name cause the automatically generated author sort string to be # identical to the author name. This means that the sort for a string like Acme diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py index c3a229fe3c..07fae187ba 100644 --- a/src/calibre/ebooks/metadata/__init__.py +++ b/src/calibre/ebooks/metadata/__init__.py @@ -65,20 +65,27 @@ def author_to_author_sort(author, method=None): suffixes = set([x.lower() for x in tweaks['author_name_suffixes']]) suffixes |= set([x+u'.' for x in suffixes]) - last = tokens[-1].lower() - suffix = None - if last in suffixes: - suffix = tokens[-1] - tokens = tokens[:-1] + suffix = u'' + while True: + if not tokens: + return author + last = tokens[-1].lower() + if last in suffixes: + suffix = tokens[-1] + ' ' + suffix + tokens = tokens[:-1] + else: + break + suffix = suffix.strip() if method == u'comma' and u',' in u''.join(tokens): return author atokens = tokens[-1:] + tokens[:-1] + num_toks = len(atokens) if suffix: atokens.append(suffix) - if method != u'nocomma' and len(atokens) > 1: + if method != u'nocomma' and num_toks > 1: atokens[0] += u',' return u' '.join(atokens)