Sort identifiers when displaying in book details panel

This commit is contained in:
Kovid Goyal 2021-03-18 08:07:23 +05:30
parent 23be5b369e
commit 56c0bca8f9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 31 additions and 17 deletions

View File

@ -203,7 +203,7 @@ def mi_to_html(
for x in data] for x in data]
ans.append((field, row % (name, ', '.join(fmts)))) ans.append((field, row % (name, ', '.join(fmts))))
elif field == 'identifiers': elif field == 'identifiers':
urls = urls_from_identifiers(mi.identifiers) urls = urls_from_identifiers(mi.identifiers, sort_results=True)
links = [ links = [
'<a href="%s" title="%s:%s">%s</a>' % ( '<a href="%s" title="%s:%s">%s</a>' % (
action('identifier', url=url, name=namel, id_type=id_typ, value=id_val, field='identifiers', book_id=book_id), action('identifier', url=url, name=namel, id_type=id_typ, value=id_val, field='identifiers', book_id=book_id),

View File

@ -6,26 +6,26 @@ __license__ = 'GPL v3'
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import time, re import re
import time
from datetime import datetime from datetime import datetime
from threading import Thread
from io import StringIO from io import StringIO
from operator import attrgetter from operator import attrgetter
from polyglot.urllib import urlparse, quote from threading import Thread
from calibre.customize.ui import metadata_plugins, all_metadata_plugins from calibre.customize.ui import all_metadata_plugins, metadata_plugins
from calibre.ebooks.metadata import check_issn, authors_to_sort_string from calibre.ebooks.metadata import authors_to_sort_string, check_issn
from calibre.ebooks.metadata.book.base import Metadata
from calibre.ebooks.metadata.sources.base import create_log from calibre.ebooks.metadata.sources.base import create_log
from calibre.ebooks.metadata.sources.prefs import msprefs from calibre.ebooks.metadata.sources.prefs import msprefs
from calibre.ebooks.metadata.xisbn import xisbn from calibre.ebooks.metadata.xisbn import xisbn
from calibre.ebooks.metadata.book.base import Metadata from calibre.utils.date import UNDEFINED_DATE, as_utc, utc_tz
from calibre.utils.date import utc_tz, as_utc
from calibre.utils.html2text import html2text
from calibre.utils.icu import lower
from calibre.utils.date import UNDEFINED_DATE
from calibre.utils.formatter import EvalFormatter from calibre.utils.formatter import EvalFormatter
from calibre.utils.html2text import html2text
from calibre.utils.icu import lower, primary_sort_key
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues, unicode_type
from polyglot.queue import Queue, Empty from polyglot.queue import Empty, Queue
from polyglot.urllib import quote, urlparse
# Download worker {{{ # Download worker {{{
@ -505,7 +505,7 @@ def identify(log, abort, # {{{
from calibre.ebooks.metadata.tag_mapper import map_tags from calibre.ebooks.metadata.tag_mapper import map_tags
am_rules = msprefs['author_map_rules'] am_rules = msprefs['author_map_rules']
if am_rules: if am_rules:
from calibre.ebooks.metadata.author_mapper import map_authors, compile_rules from calibre.ebooks.metadata.author_mapper import compile_rules, map_authors
am_rules = compile_rules(am_rules) am_rules = compile_rules(am_rules)
max_tags = msprefs['max_tags'] max_tags = msprefs['max_tags']
@ -539,7 +539,7 @@ def identify(log, abort, # {{{
# }}} # }}}
def urls_from_identifiers(identifiers): # {{{ def urls_from_identifiers(identifiers, sort_results=False): # {{{
identifiers = {k.lower():v for k, v in iteritems(identifiers)} identifiers = {k.lower():v for k, v in iteritems(identifiers)}
ans = [] ans = []
keys_left = set(identifiers) keys_left = set(identifiers)
@ -605,6 +605,12 @@ def urls_from_identifiers(identifiers): # {{{
parts = urlparse(url) parts = urlparse(url)
name = parts.netloc or parts.path name = parts.netloc or parts.path
add(name, k, url, url) add(name, k, url, url)
if sort_results:
def url_key(x):
return primary_sort_key(str(x[0]))
ans = sorted(ans, key=url_key)
return ans return ans
# }}} # }}}
@ -612,8 +618,9 @@ def urls_from_identifiers(identifiers): # {{{
if __name__ == '__main__': # tests {{{ if __name__ == '__main__': # tests {{{
# To run these test use: calibre-debug -e # To run these test use: calibre-debug -e
# src/calibre/ebooks/metadata/sources/identify.py # src/calibre/ebooks/metadata/sources/identify.py
from calibre.ebooks.metadata.sources.test import (test_identify, from calibre.ebooks.metadata.sources.test import (
title_test, authors_test) authors_test, test_identify, title_test
)
tests = [ tests = [
( (
{'title':'Magykal Papers', {'title':'Magykal Papers',

View File

@ -252,12 +252,19 @@ def render_metadata(mi, table, book_id, iframe_css): # {{{
add_row(name, stars, is_searchable=field, search_text=val/2 + '') add_row(name, stars, is_searchable=field, search_text=val/2 + '')
def process_identifiers(field, fm, name, val): def process_identifiers(field, fm, name, val):
def ids_sorter(url_map, k):
x = url_map[k]
if not x:
return ''
return (x[0] or '').toLowerCase()
if val: if val:
keys = Object.keys(val) keys = Object.keys(val)
if keys.length: if keys.length:
td = E.td() td = E.td()
url_map = {k:v'[text, url]' for text, k, val, url in mi.urls_from_identifiers or v'[]'} url_map = {k:v'[text, url]' for text, k, val, url in mi.urls_from_identifiers or v'[]'}
for k in keys: for k in sorted(keys, key=ids_sorter.bind(None, url_map)):
idval = val[k] idval = val[k]
x = url_map[k] x = url_map[k]
if x: if x: