+
+@register.inclusion_tag('catalogue/book_info.html')
+def book_info(book):
+ return locals()
+
+
+@register.inclusion_tag('catalogue/book_wide.html', takes_context=True)
+def book_wide(context, book):
+ formats = {}
+ # files generated during publication
+ for ebook_format in book.ebook_formats:
+ if book.has_media(ebook_format):
+ formats[ebook_format] = book.get_media(ebook_format)
+
+ return {
+ 'related': book.related_info(),
+ 'book': book,
+ 'formats': formats,
+ 'extra_info': book.get_extra_info_value(),
+ 'request': context.get('request'),
+ 'fragment': book.choose_fragment(),
+ }
+
+
+@register.inclusion_tag('catalogue/book_short.html', takes_context=True)
+def book_short(context, book):
+ return {
+ 'related': book.related_info(),
+ 'book': book,
+ 'request': context.get('request'),
+ }
+
+
+@register.inclusion_tag('catalogue/book_mini_box.html')
+def book_mini(book):
+ return {
+ 'related': book.related_info(),
+ 'book': book,
+ }
+
+
+@register.inclusion_tag('catalogue/work-list.html', takes_context=True)
+def work_list(context, object_list):
+ request = context.get('request')
+ if object_list:
+ object_type = type(object_list[0]).__name__
+ return locals()
+
+
+@register.inclusion_tag('catalogue/fragment_promo.html')
+def fragment_promo(arg=None):
+ if arg is None:
+ fragments = Fragment.objects.all().order_by('?')
+ fragment = fragments[0] if fragments.exists() else None
+ elif isinstance(arg, Book):
+ fragment = arg.choose_fragment()
+ else:
+ fragments = Fragment.tagged.with_all(arg).order_by('?')
+ fragment = fragments[0] if fragments.exists() else None
+
+ return {
+ 'fragment': fragment,
+ }
+
+
+@register.inclusion_tag('catalogue/related_books.html')
+def related_books(book, limit=6):
+ related = list(Book.objects.filter(
+ common_slug=book.common_slug).exclude(pk=book.pk)[:limit])
+ limit -= len(related)
+ if limit:
+ related += Book.tagged.related_to(book,
+ Book.objects.exclude(common_slug=book.common_slug),
+ ignore_by_tag=book.book_tag())[:limit]
+ return {
+ 'books': related,
+ }
+
+
+@register.filter
+@stringfilter
+def removewholetags(value, tags):
+ """Removes a space separated list of [X]HTML tags from the output.
+
+ FIXME: It makes the assumption the removed tags aren't nested.
+
+ """
+ tags = [re.escape(tag) for tag in tags.split()]
+ tags_re = u'(%s)' % u'|'.join(tags)
+ tag_re = re.compile(ur'<%s[^>]*>.*?</\s*\1\s*>' % tags_re, re.U)
+ value = tag_re.sub(u'', value)
+ return value