mirror of
				https://github.com/kovidgoyal/calibre.git
				synced 2025-11-04 03:27:00 -05:00 
			
		
		
		
	New template function urls_from_identifiers()
This commit is contained in:
		
							parent
							
								
									0f2e921ff1
								
							
						
					
					
						commit
						ad3f6b0258
					
				@ -581,6 +581,7 @@ In `GPM` the functions described in `Single Function Mode` all require an additi
 | 
				
			|||||||
* ``subtract(x, y)`` -- returns ``x - y``. Throws an exception if either ``x`` or ``y`` are not numbers. This function can usually be replaced by the ``-`` operator.
 | 
					* ``subtract(x, y)`` -- returns ``x - y``. Throws an exception if either ``x`` or ``y`` are not numbers. This function can usually be replaced by the ``-`` operator.
 | 
				
			||||||
* ``today()`` -- return a date+time string for today (now). This value is designed for use in `format_date` or `days_between`, but can be manipulated like any other string. The date is in `ISO <https://en.wikipedia.org/wiki/ISO_8601>`_ date/time format.
 | 
					* ``today()`` -- return a date+time string for today (now). This value is designed for use in `format_date` or `days_between`, but can be manipulated like any other string. The date is in `ISO <https://en.wikipedia.org/wiki/ISO_8601>`_ date/time format.
 | 
				
			||||||
* ``template(x)`` -- evaluates ``x`` as a template. The evaluation is done in its own context, meaning that variables are not shared between the caller and the template evaluation.
 | 
					* ``template(x)`` -- evaluates ``x`` as a template. The evaluation is done in its own context, meaning that variables are not shared between the caller and the template evaluation.
 | 
				
			||||||
 | 
					* ``urls_from_identifiers(identifiers, sort_results)`` -- given a comma-separated list of ``identifiers``, where an `identifier` is a colon-separated pair of values (``id_name:id_value``), returns a comma-separated list of HTML URLs generated from the identifiers. The list not sorted if sort_results is ``0`` (character or number), otherwise it is sorted alphabetically by the identifier name. The URLs are generated in the same way as the built-in identifiers column when shown in :guilabel:`Book details`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. _template_mode:
 | 
					.. _template_mode:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,10 +13,11 @@ __docformat__ = 'restructuredtext en'
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import inspect, re, traceback, numbers
 | 
					import inspect, re, traceback, numbers
 | 
				
			||||||
from datetime import datetime, timedelta
 | 
					from datetime import datetime, timedelta
 | 
				
			||||||
 | 
					from functools import partial
 | 
				
			||||||
from math import trunc, floor, ceil, modf
 | 
					from math import trunc, floor, ceil, modf
 | 
				
			||||||
from contextlib import suppress
 | 
					from contextlib import suppress
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from calibre import human_readable, prints
 | 
					from calibre import human_readable, prints, prepare_string_for_xml
 | 
				
			||||||
from calibre.constants import DEBUG
 | 
					from calibre.constants import DEBUG
 | 
				
			||||||
from calibre.ebooks.metadata import title_sort
 | 
					from calibre.ebooks.metadata import title_sort
 | 
				
			||||||
from calibre.utils.config import tweaks
 | 
					from calibre.utils.config import tweaks
 | 
				
			||||||
@ -2192,6 +2193,41 @@ class BuiltinCharacter(BuiltinFormatterFunction):
 | 
				
			|||||||
        raise NotImplementedError()
 | 
					        raise NotImplementedError()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class BuiltinUrlsFromIdentifiers(BuiltinFormatterFunction):
 | 
				
			||||||
 | 
					    name = 'urls_from_identifiers'
 | 
				
			||||||
 | 
					    arg_count = 2
 | 
				
			||||||
 | 
					    category = 'Formatting values'
 | 
				
			||||||
 | 
					    __doc__ = doc = _('urls_from_identifiers(identifiers, sort_results) -- given '
 | 
				
			||||||
 | 
					                      'a comma-separated list of identifiers, where an identifier '
 | 
				
			||||||
 | 
					                      'is a colon-separated pair of values (name:id_value), returns a '
 | 
				
			||||||
 | 
					                      'comma-separated list of HTML URLs generated from the '
 | 
				
			||||||
 | 
					                      'identifiers. The list not sorted if sort_results is 0 '
 | 
				
			||||||
 | 
					                      '(character or number), otherwise it is sorted alphabetically '
 | 
				
			||||||
 | 
					                      'by the identifier name. The URLs are generated in the same way '
 | 
				
			||||||
 | 
					                      'as the built-in identifiers column when shown in book details.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def evaluate(self, formatter, kwargs, mi, locals, identifiers, sort_results):
 | 
				
			||||||
 | 
					        from calibre.ebooks.metadata.sources.identify import urls_from_identifiers
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            v = dict()
 | 
				
			||||||
 | 
					            for id_ in identifiers.split(','):
 | 
				
			||||||
 | 
					                if id_:
 | 
				
			||||||
 | 
					                    pair = id_.split(':', maxsplit=1)
 | 
				
			||||||
 | 
					                    if len(pair) == 2:
 | 
				
			||||||
 | 
					                        l = pair[0].strip()
 | 
				
			||||||
 | 
					                        r = pair[1].strip()
 | 
				
			||||||
 | 
					                        if l and r:
 | 
				
			||||||
 | 
					                            v[l] = r
 | 
				
			||||||
 | 
					            urls = urls_from_identifiers(v, sort_results=sort_results != '0')
 | 
				
			||||||
 | 
					            p = prepare_string_for_xml
 | 
				
			||||||
 | 
					            a = partial(prepare_string_for_xml, attribute=True)
 | 
				
			||||||
 | 
					            links = [f'<a href="{a(url)}" title="{a(id_typ)}:{a(id_val)}">{p(name)}</a>'
 | 
				
			||||||
 | 
					                for name, id_typ, id_val, url in urls]
 | 
				
			||||||
 | 
					            return  ', '.join(links)
 | 
				
			||||||
 | 
					        except Exception as e:
 | 
				
			||||||
 | 
					            return str(e)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_formatter_builtins = [
 | 
					_formatter_builtins = [
 | 
				
			||||||
    BuiltinAdd(), BuiltinAnd(), BuiltinApproximateFormats(), BuiltinArguments(),
 | 
					    BuiltinAdd(), BuiltinAnd(), BuiltinApproximateFormats(), BuiltinArguments(),
 | 
				
			||||||
    BuiltinAssign(),
 | 
					    BuiltinAssign(),
 | 
				
			||||||
@ -2222,7 +2258,7 @@ _formatter_builtins = [
 | 
				
			|||||||
    BuiltinSublist(),BuiltinSubstr(), BuiltinSubtract(), BuiltinSwapAroundArticles(),
 | 
					    BuiltinSublist(),BuiltinSubstr(), BuiltinSubtract(), BuiltinSwapAroundArticles(),
 | 
				
			||||||
    BuiltinSwapAroundComma(), BuiltinSwitch(),
 | 
					    BuiltinSwapAroundComma(), BuiltinSwitch(),
 | 
				
			||||||
    BuiltinTemplate(), BuiltinTest(), BuiltinTitlecase(),
 | 
					    BuiltinTemplate(), BuiltinTest(), BuiltinTitlecase(),
 | 
				
			||||||
    BuiltinToday(), BuiltinTransliterate(), BuiltinUppercase(),
 | 
					    BuiltinToday(), BuiltinTransliterate(), BuiltinUppercase(), BuiltinUrlsFromIdentifiers(),
 | 
				
			||||||
    BuiltinUserCategories(), BuiltinVirtualLibraries(), BuiltinAnnotationCount()
 | 
					    BuiltinUserCategories(), BuiltinVirtualLibraries(), BuiltinAnnotationCount()
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user