View Single Post
Old 11-15-2015, 03:10 AM   #3
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Buckskin View Post
If I was using C or other programming language I would just split the author field into words and iterate through each entry. In Calibres template languages I don't know how to do this.
You can write a User-defined template function in python to do things like this.

Look at calibre's source code "calibre.db.cache" see what is in the mi structure.

The built-in template function 'author_sorts' gets close to what you want and probably can be used as a starting point.
Code:
def evaluate(self, formatter, kwargs, mi, locals, val_sep):
    sort_data = mi.author_sort_map
    if not sort_data:
        return ''
    names = [sort_data.get(n) for n in mi.authors if n.strip()]
    return val_sep.join(n for n in names)
Instead of using a comprehension, iterate through the authors building the sort string from the author_sort_map and adding on the stuff in parenthesis.
chaley is offline   Reply With Quote