diff --git a/manual/template_lang.rst b/manual/template_lang.rst index aa9241d0fd..b885f2f7fe 100644 --- a/manual/template_lang.rst +++ b/manual/template_lang.rst @@ -129,6 +129,7 @@ The functions available are listed below. Note that the definitive documentation * ``language_strings(lang_codes, localize)`` -- return the strings for the language codes passed in `lang_codes`. If `localize` is zero, return the strings in English. If localize is not zero, return the strings in the language of the current locale. `Lang_codes` is a comma-separated list. * ``list_item(index, separator)`` -- interpret the field as a list of items separated by `separator`, returning the `index`th item. The first item is number zero. The last item can be returned using `list_item(-1,separator)`. If the item is not in the list, then the empty value is returned. The separator has the same meaning as in the `count` function. * ``lookup(pattern, field, pattern, field, ..., else_field)`` -- like switch, except the arguments are field (metadata) names, not text. The value of the appropriate field will be fetched and used. Note that because composite columns are fields, you can use this function in one composite field to use the value of some other composite field. This is extremely useful when constructing variable save paths (more later). + * ``rating_to_stars(use_half_stars)`` -- Returns the rating as string of star characters. The source value must be a number between 0 and 5. Set use_half_stars to 1 if you want half star characters for custom ratings columns that are not integers, for example 2.5. * ``re(pattern, replacement)`` -- return the field after applying the regular expression. All instances of `pattern` are replaced with `replacement`. As in all of calibre, these are Python-compatible regular expressions. * ``select(key)`` -- interpret the field as a comma-separated list of items, with the items being of the form "id:value". Find the pair with the id equal to key, and return the corresponding value. This function is particularly useful for extracting a value such as an ISBN from the set of identifiers for a book. * ``shorten(left chars, middle text, right chars)`` -- Return a shortened version of the field, consisting of `left chars` characters from the beginning of the field, followed by `middle text`, followed by `right chars` characters from the end of the string. `Left chars` and `right chars` must be integers. For example, assume the title of the book is `Ancient English Laws in the Times of Ivanhoe`, and you want it to fit in a space of at most 15 characters. If you use ``{title:shorten(9,-,5)}``, the result will be `Ancient E-nhoe`. If the field's length is less than ``left chars`` + ``right chars`` + the length of ``middle text``, then the field will be used intact. For example, the title `The Dome` would not be changed. diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 6f92cc6cfc..7077d4a468 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -1634,6 +1634,28 @@ class BuiltinCheckYesNo(BuiltinFormatterFunction): return 'yes' return "" +class BuiltinRatingToStars(BuiltinFormatterFunction): + name = 'rating_to_stars' + arg_count = 2 + category = 'Formatting values' + __doc__ = doc = _('rating_to_stars(value, use_half_stars) ' + '-- Returns the rating as string of star characters. ' + 'The value is a number between 0 and 5. Set use_half_stars ' + 'to 1 if you want half star characters for custom ratings ' + 'columns that support non-integer ratings, for example 2.5.') + + def evaluate(self, formatter, kwargs, mi, locals, value, use_half_stars): + if not value: + return '' + err_msg = _('The rating must be a number between 0 and 5') + try: + v = float(value) * 2 + except: + raise ValueError(err_msg) + if v < 0 or v > 10: + raise ValueError(err_msg) + from calibre.ebooks.metadata import rating_to_stars + return rating_to_stars(v, use_half_stars == '1') _formatter_builtins = [ BuiltinAdd(), BuiltinAnd(), BuiltinApproximateFormats(), BuiltinAssign(), @@ -1650,7 +1672,7 @@ _formatter_builtins = [ BuiltinListIntersection(), BuiltinListitem(), BuiltinListRe(), BuiltinListReGroup(), BuiltinListSort(), BuiltinListUnion(), BuiltinLookup(), BuiltinLowercase(), BuiltinMultiply(), BuiltinNot(), BuiltinOndevice(), - BuiltinOr(), BuiltinPrint(), BuiltinRawField(), BuiltinRawList(), + BuiltinOr(), BuiltinPrint(), BuiltinRatingToStars(), BuiltinRawField(), BuiltinRawList(), BuiltinRe(), BuiltinReGroup(), BuiltinSelect(), BuiltinSeriesSort(), BuiltinShorten(), BuiltinStrcat(), BuiltinStrcatMax(), BuiltinStrcmp(), BuiltinStrInList(), BuiltinStrlen(), BuiltinSubitems(),