View Single Post
Old Yesterday, 03:08 PM   #11
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
Posts: 7,305
Karma: 5007213
Join Date: Dec 2011
Location: Midwest USA
Device: Kobo Clara Colour running KOReader
I think I've figured out how to call Calibre template/formatter instead of Python's. And how to add the pre-existing Catalog template values, assuming I'm not abusing the privilege.

Code:
            mi = self.db.get_metadata(book['id'],index_is_id=True)
            generated_html = P('catalog/template.xhtml', data=True).decode('utf-8')

            ## brute force copy any old catalog values such as {xmlns}
            for k in args:
                if not mi.has_key(k):
                    self.opts.log.error("Setting %s -> %s"%(k,args[k]))
                    mi.__setattr__(k,args[k])

            from calibre.ebooks.metadata.book.formatter import SafeFormat
            generated_html = SafeFormat().unsafe_format(generated_html,
                                                        {},
                                                        mi)
Is this an acceptable approach?


The best (calibre) template expression I've found so far that will allow a template.xhtml to use a custom column only if it exists in the library is:

Code:
{title:'cmp(field_exists("#wordcount"),1,'',template("[[#wordcount]]"),'')'}
Or with prefix & suffix:

Code:
{title:'cmp(field_exists("#wordcount"),1,'',template("[[#wordcount]]"),'')'|<tr><td>Words: |</td></tr>}
...where title is arbitrary because I didn't see another way.

Any suggestions for a better expression? I'm trying to avoid needing the user to add template functions.
JimmXinu is offline   Reply With Quote