From 93baaf261a3c78c3fb3f43e67958cc4191eb7c4e Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 31 Oct 2020 14:18:56 -0700 Subject: [PATCH] Show/hide toggle for long book descriptions --- .../templates/snippets/book_description.html | 22 +++++++++++++++++-- bookwyrm/templatetags/fr_display.py | 19 ++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templates/snippets/book_description.html b/bookwyrm/templates/snippets/book_description.html index 12c9ccfb0..839a80986 100644 --- a/bookwyrm/templates/snippets/book_description.html +++ b/bookwyrm/templates/snippets/book_description.html @@ -1,2 +1,20 @@ -
{% if book.description %}{{ book.description }}{% elif book.parent_work.description %}{{ book.parent_work.description }}{% endif %}
- +{% load fr_display %} +{% with book|book_description as full %} + {% with full|text_overflow as trimmed %} + {% if trimmed != full %} +
+ + +
+
+ + +
+ {% else %} +
{{ full }} +
+ {% endif %} + {% endwith %} +{% endwith %} diff --git a/bookwyrm/templatetags/fr_display.py b/bookwyrm/templatetags/fr_display.py index cb4ee4198..9e6e35cd5 100644 --- a/bookwyrm/templatetags/fr_display.py +++ b/bookwyrm/templatetags/fr_display.py @@ -106,6 +106,25 @@ def get_edition_info(book): return ', '.join(i for i in items if i) +@register.filter(name='book_description') +def get_book_description(book): + ''' use the work's text if the book doesn't have it ''' + return book.description or book.parent_work.description + + +@register.filter(name='text_overflow') +def text_overflow(text): + ''' dont' let book descriptions run for ages ''' + char_max = 500 + if len(text) < char_max: + return text + + trimmed = text[:char_max] + # go back to the last space + trimmed = ' '.join(trimmed.split(' ')[:-1]) + return trimmed + '...' + + @register.simple_tag(takes_context=True) def shelve_button_identifier(context, book): ''' check what shelf a user has a book on, if any '''