1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django import template
7 register = template.Library()
10 @register.inclusion_tag('catalogue/book_searched.html', takes_context=True)
11 def book_searched(context, result):
12 # We don't need hits which lead to sections but do not have
15 for (idx, h) in enumerate(result.hits)
16 if result.snippets[idx] is not None or ('fragment' in h and h['themes_hit'])]
18 for (idx, hit) in hits:
19 # currently we generate one snipper per hit though.
20 if len(result.snippets) <= idx:
22 if result.snippets[idx] is None:
24 snip = result.snippets[idx]
25 # fix some formattting
26 snip = re.sub(r"[ \t\n]*\n[ \t\n]*", "\n", snip)
27 snip = re.sub(r"(^[ \t\n]+|[ \t\n]+$)", "", snip)
29 snip = snip.replace("\n", "<br />").replace('---', '—')
33 'request': context['request'],
35 'hits': list(zip(*hits))[1] if hits else []