From ae0c68f6206a3d33e9a718e6a01acf0b9025d6a2 Mon Sep 17 00:00:00 2001 From: Erik Stein Date: Mon, 29 Jan 2018 10:10:04 +0100 Subject: [PATCH] List to html functions. --- shared/utils/templates/utils/_text_list.html | 1 + shared/utils/templatetags/text_tags.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/shared/utils/templates/utils/_text_list.html b/shared/utils/templates/utils/_text_list.html index 7f09137..4c8ac38 100644 --- a/shared/utils/templates/utils/_text_list.html +++ b/shared/utils/templates/utils/_text_list.html @@ -1,5 +1,6 @@ {% load i18n %} {# List of items, where items have a (potentially empty) get_absolute_url and a name attribute #} +{# Usage: {% include "utils/_text_list.html" with items=page.get_authors %} #} {% for item in items %} {% if forloop.last and forloop.counter > 1 %}{% trans "und" %}{% endif %} diff --git a/shared/utils/templatetags/text_tags.py b/shared/utils/templatetags/text_tags.py index 891a324..2e1541d 100644 --- a/shared/utils/templatetags/text_tags.py +++ b/shared/utils/templatetags/text_tags.py @@ -53,3 +53,23 @@ def html_entities_to_unicode(text): @register.filter(needs_autoescape=False) def slimdown(text): return mark_safe(text_utils.slimdown(text)) + + +@register.filter(is_safe=True) +@stringfilter +def html_lines_to_list(value): + """ + Replaces all
tags with ", " + """ + rv = [] + lines = value.split("
") + for i in range(0, len(lines)): + line = lines[i].strip() + rv.append(line) + if i < len(lines) - 1: + if line[-1] not in ";:,.-–—": + rv.append(", ") + else: + rv.append(" ") + return "".join(rv) + return ", ".join([l.strip() for l in value.split("
")])