1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
8 from django import template
9 from django.template import Node, Variable
10 from django.utils.encoding import smart_str
11 from django.core.urlresolvers import reverse
12 # from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
13 # from django.db.models import Q
14 from django.conf import settings
15 # from django.utils.translation import ugettext as _
16 from catalogue.templatetags.catalogue_tags import book_wide
17 from catalogue.models import Book
18 # from catalogue.forms import SearchForm
19 # from catalogue.utils import split_tags
22 register = template.Library()
25 @register.inclusion_tag('catalogue/book_searched.html', takes_context=True)
26 def book_searched(context, result):
27 book = Book.objects.get(pk=result.book_id)
30 # for hit in result.hits:
32 # snippets.append(hit['snippets'])
33 # elif hit['fragment']:
34 # snippets.append(hit['fragment'].short_text)
36 # We don't need hits which lead to sections but do not have
38 hits = filter(lambda (idx, h):
39 result.snippets[idx] is not None
40 or 'fragment' in h, enumerate(result.hits))
41 print "[tmpl: from %d hits selected %d]" % (len(result.hits), len(hits))
43 for (idx, hit) in hits:
44 # currently we generate one snipper per hit though.
45 if result.snippets[idx] is None:
47 snip = result.snippets[idx]
48 # fix some formattting
49 snip = re.subn(r"(^[ \t\n]+|[ \t\n]+$)", u"",
50 re.subn(r"[ \t\n]*\n[ \t\n]*", u"\n", snip))
52 snip = snip.replace("\n", "<br />").replace('---', '—')
56 'related': book.related_info(),
58 'main_link': book.get_absolute_url(),
59 'request': context.get('request'),
60 'hits': hits and zip(*hits)[1] or [],
61 'main_link': book.get_absolute_url(),