text = fragment.to_string()
short_text = ''
- if (len(MarkupString(text)) > 240):
- short_text = unicode(MarkupString(text)[:160])
+ markup = MarkupString(text)
+ if (len(markup) > 240):
+ short_text = unicode(markup[:160])
new_fragment = Fragment.objects.create(anchor=fragment.id, book=self,
text=text, short_text=short_text)
audiences = sorted(set([self._audiences_pl[a] for a in audiences]))
return [a[1] for a in audiences]
+ def choose_fragment(self):
+ tag = self.book_tag()
+ fragments = Fragment.tagged.with_any([tag])
+ if fragments.exists():
+ return fragments.order_by('?')[0]
+ elif self.parent:
+ return self.parent.choose_fragment()
+ else:
+ return None
+
def _has_factory(ftype):
has = lambda self: bool(getattr(self, "%s_file" % ftype))
# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-import feedparser
import datetime
+import feedparser
+import re
from django import template
from django.template import Node, Variable
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.db.models import Q
from django.conf import settings
+from django.template.defaultfilters import stringfilter
from django.utils.translation import ugettext as _
from catalogue.utils import split_tags
+from catalogue.models import Book, Fragment
register = template.Library()
'formats': formats,
'extra_info': book.get_extra_info_value(),
'request': context.get('request'),
+ 'fragment': book.choose_fragment(),
}
request = context.get('request')
if object_list:
object_type = type(object_list[0]).__name__
- return locals()
\ No newline at end of file
+ return locals()
+
+
+@register.inclusion_tag('catalogue/chosen_fragment.html')
+def promo_fragment(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.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
width: 41.5em;
}
-.book-wide-box blockquote.cite-body {
- /* @ 18pt */
- width: 100%; /*23.055em;*/
- height: 7.222em;
- background-color: #f7f7f7;
- margin: 0;
- position: relative;
- top: -0.444em;
- right: -0.555em;
- vertical-align: center;
-}
-
-.book-wide-box blockquote div.cite-text {
- padding: 0.888em;
-}
-
-.book-wide-box blockquote p.cite-more {
- display: inline;
- font-size: 0.611em;
- float: right;
-}
-
ul.inline-items, ul.inline-items li {
margin: 0;
padding: 0;
.unlike .if-like {
display: none;
+}
.snippets {
width: 44em;
margin: 1.083em 0em;
}
-.snipptes .anchor {
+.snippets .anchor {
display: none;
}
+
+
+.cite blockquote p {
+ margin: 0;
+}
+
+.cite blockquote {
+ padding: 0;
+ margin: 0;
+}
+
+.book-wide-box .cite {
+ /* @ 18pt */
+ width: 100%; /*23.055em;*/
+ height: 7.222em;
+ background-color: #f7f7f7;
+ margin: 0;
+ position: relative;
+ top: -0.444em;
+ right: -0.555em;
+ vertical-align: center;
+}
+
+.abook-wide-box blockquote div.cite-text {
+ padding: 0.888em;
+}
+
+.abook-wide-box blockquote p.cite-more {
+ display: inline;
+ font-size: 0.611em;
+ float: right;
+}
{% extends "catalogue/book_short.html" %}
{% load i18n %}
+{% load promo_fragment from catalogue_tags %}
{% block box-class %}book-wide-box{% endblock %}
{% block right-column %}
<div class="right-column">
- <blockquote id="quote" class="cite-body">
- {% block quote %}
- <div>Ten, który walczy z potworami powinien zadbać, by sam nie stał się potworem.
- Gdy długo spoglądamy w otchłań, otchłań spogląda również w nas.</div>
- {% endblock %}
- </blockquote>
+ <div class="quote">
+ {% promo_fragment book %}
+ </div>
<div class="other-tools">
<h2 class="mono">{% trans "See" %}</h2>
--- /dev/null
+{% load i18n %}
+{% load removewholetags from catalogue_tags %}
+
+{% if fragment %}
+<a href="{{ fragment.get_absolute_url }}" class="cite">
+ <blockquote class="cite-body">
+ {{ fragment.text|removewholetags:"a"|truncatewords_html:15|safe }}
+ </blockquote>
+ <p class="mono">Adam Mickiewicz, Dziady część III</p>
+</a>
+{% endif %}
<div class="right-column">
- <a href="" class="cite">
- <div class="cite-body">
- Dobranoc, obróć jeszcze raz na mnie oczęta,<br/>
- (…) Daj mi pierś ucałować<br/>
- Dobranoc, zapięta.
- </div>
- <p class="mono">Adam Mickiewicz, Dziady część III</p>
- </a>
+ {% promo_fragment tags %}
<div class="see-also">
<h2 class='mono'>{% trans "See also" %}:</h2>