Refactor book details web search link generating code to make it re-useable in server

This commit is contained in:
Kovid Goyal 2025-02-08 10:37:19 +05:30
parent cc8b7ad47b
commit ae15a85e73
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 36 additions and 12 deletions

View File

@ -67,16 +67,21 @@ def search_action_with_data(search_term, value, book_id, field=None, **k):
return search_action(search_term, value, field=field, book_id=book_id, **k)
def web_search_link(template, mi, value):
formatter = SafeFormat()
mi.set('item_value', value)
u = formatter.safe_format(template, mi, 'BOOK DETAILS WEB LINK', mi)
if u:
return u, prepare_string_for_xml(_('Click to browse to: {}').format(u), attribute=True)
return '', ''
def cc_search_action_with_data(search_term, value, book_id, fm, mi, field=None, **k):
if mi is not None and fm is not None:
if template := fm.get('display', {}).get('web_search_template'):
try:
formatter = SafeFormat()
mi.set('item_value', value)
u = formatter.safe_format(template, mi, 'BOOK DETAILS WEB LINK', mi)
if u:
v = prepare_string_for_xml(_('Click to browse to {}').format(u), attribute=True)
return action('cc_url', url=u), v
u, v = web_search_link(template, mi, value)
return action('cc_url', url=u), v
except Exception:
import traceback
traceback.print_exc()
@ -91,6 +96,13 @@ def notes_action(**keys):
DEFAULT_AUTHOR_LINK = f'search-{DEFAULT_AUTHOR_SOURCE}'
def resolve_default_author_link(ans: str | None = None) -> str:
if ans == 'https://en.wikipedia.org/w/index.php?search={author}':
# The old default value for this setting
ans = ''
return ans or DEFAULT_AUTHOR_LINK
def author_search_href(which, title=None, author=None):
if which == 'calibre':
return 'calibre', _('Search the calibre library for books by %s') % author

View File

@ -630,12 +630,8 @@ QSettings.setDefaultFormat(QSettings.Format.IniFormat)
def default_author_link():
from calibre.ebooks.metadata.book.render import DEFAULT_AUTHOR_LINK
ans = gprefs.get('default_author_link')
if ans == 'https://en.wikipedia.org/w/index.php?search={author}':
# The old default value for this setting
ans = DEFAULT_AUTHOR_LINK
return ans or DEFAULT_AUTHOR_LINK
from calibre.ebooks.metadata.book.render import resolve_default_author_link
return resolve_default_author_link(gprefs.get('default_author_link'))
def available_heights():

View File

@ -165,6 +165,22 @@ def get_gpref(name: str, defval=None):
return gprefs.get(name, defval)
def web_search_link(db, book_id: int, field: str, item_val: str) -> tuple[str, str]:
from calibre.ebooks.metadata.book.render import render_author_link, resolve_default_author_link, web_search_link
if field == 'authors':
default_author_link = resolve_default_author_link(get_gpref('default_author_link'))
author_sort = db.author_sort_from_authors((item_val,))
url, tooltip = render_author_link(default_author_link, item_val, author_sort, db.field_for('title', book_id))
else:
metadata = db.field_metadata.get(field)
if metadata is None or not (template := metadata.get('display', {}).get('web_search_template')):
return '', ''
url, tooltip = web_search_link(template, db.get_metadata(book_id), item_val)
if url.partition(':')[0].lower() in ('http', 'https'):
return url, tooltip
return '', ''
def get_icon_for_node(node, parent, node_to_tag_map, tag_map, eval_formatter, db):
category = node['category']
if category in ('search', 'formats') or category.startswith('@'):