Allow using HTML in book list template

This commit is contained in:
Kovid Goyal 2017-07-18 11:03:48 +05:30
parent 08da31ba3c
commit c50ed74f83
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,5 +1,6 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
# globals: NodeFilter
from __python__ import bound_methods, hash_literals from __python__ import bound_methods, hash_literals
from elementmaker import E from elementmaker import E
@ -40,7 +41,7 @@ def default_template():
'comments_fields': v"['comments']", 'comments_fields': v"['comments']",
'lines': [ 'lines': [
_('<b>{title}</b> by {authors}'), _('<b>{title}</b> by {authors}'),
_('{series_index} of {series}') + '|||{rating}', _('{series_index} of <i>{series}</i>') + '|||{rating}',
'{tags}', '{tags}',
_('Date: {timestamp} Published: {pubdate}'), _('Date: {timestamp} Published: {pubdate}'),
] ]
@ -172,17 +173,29 @@ def render_field(field, mi, book_id): # {{{
def render_part(part, template, book_id, metadata): def render_part(part, template, book_id, metadata):
count = rendered_count = 0 count = rendered_count = 0
ans = E.div() ans = E.div()
for field in part.split(/({[_a-z0-9]+})/): ans.innerHTML = part
if field[0] is '{' and field[-1] is '}': walk = document.createTreeWalker(ans, NodeFilter.SHOW_TEXT, None, False)
count += 1 replacements = v'[]'
val = render_field(field[1:-1], metadata, book_id) while True:
if val: n = walk.nextNode()
rendered_count += 1 if not n:
if jstype(val) is 'string': break
val = document.createTextNode(val) rendered = E.span()
ans.appendChild(val) for field in n.nodeValue.split(/({[_a-z0-9]+})/):
else: if field[0] is '{' and field[-1] is '}':
ans.appendChild(document.createTextNode(field)) count += 1
val = render_field(field[1:-1], metadata, book_id)
if val:
rendered_count += 1
if jstype(val) is 'string':
val = document.createTextNode(val)
rendered.appendChild(val)
else:
rendered.appendChild(document.createTextNode(field))
replacements.push(v'[rendered, n]')
for new_child, old_child in replacements:
old_child.parentNode.replaceChild(new_child, old_child)
if count and not rendered_count: if count and not rendered_count:
return return