When computing title sorts strip leading and trailing quotes, not just leading quotes

This commit is contained in:
Kovid Goyal 2023-02-24 07:50:56 +05:30
parent 109a845751
commit 815cd507b9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -13,7 +13,7 @@ from contextlib import suppress
from calibre import relpath, guess_type, prints, force_unicode from calibre import relpath, guess_type, prints, force_unicode
from calibre.utils.config_base import tweaks from calibre.utils.config_base import tweaks
from polyglot.builtins import codepoint_to_chr, iteritems, as_unicode from polyglot.builtins import iteritems, as_unicode
from polyglot.urllib import quote, unquote, urlparse from polyglot.urllib import quote, unquote, urlparse
@ -176,8 +176,25 @@ def get_title_sort_pat(lang=None):
return ans return ans
_ignore_starts = '\'"'+''.join(codepoint_to_chr(x) for x in quote_pairs = {
list(range(0x2018, 0x201e))+[0x2032, 0x2033]) # https://en.wikipedia.org/wiki/Quotation_mark
'"': ('"',),
"'": ("'",),
'': ('',''),
'': ('',''),
'': ('',''),
'': ('',''),
'': ('',''),
'': ('',''),
'': ('',),
'': ('',),
'': ('',),
'': ('',),
'»': ('«', '»'),
'«': ('«', '»'),
'': ('',),
'': ('',),
}
def title_sort(title, order=None, lang=None): def title_sort(title, order=None, lang=None):
@ -186,8 +203,11 @@ def title_sort(title, order=None, lang=None):
title = title.strip() title = title.strip()
if order == 'strictly_alphabetic': if order == 'strictly_alphabetic':
return title return title
if title and title[0] in _ignore_starts: if title and title[0] in quote_pairs:
q = title[0]
title = title[1:] title = title[1:]
if title and title[-1] in quote_pairs[q]:
title = title[:-1]
match = get_title_sort_pat(lang).search(title) match = get_title_sort_pat(lang).search(title)
if match: if match:
try: try:
@ -197,8 +217,11 @@ def title_sort(title, order=None, lang=None):
else: else:
if prep: if prep:
title = title[len(prep):] + ', ' + prep title = title[len(prep):] + ', ' + prep
if title[0] in _ignore_starts: if title[0] in quote_pairs:
q = title[0]
title = title[1:] title = title[1:]
if title and title[-1] in quote_pairs[q]:
title = title[:-1]
return title.strip() return title.strip()