Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "metablock" template tag #35

Merged
merged 2 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from datetime import timedelta
from django.template import engines
from django.utils import timezone

from wagtail.core.models import Page
Expand Down Expand Up @@ -27,6 +28,19 @@ def page_tree():
return (root, pages)


@pytest.fixture
def render_template():
"""
Returns a helper function that takes a template string, and returns the
rendered output.
"""
template_engine = engines['django']
def func(template_string):
load_tags_string = '{% load wagtailextensions_tags %}'
return template_engine.from_string(load_tags_string + template_string).render()
return func


def test_bleanclean_cleandata():
cleaned = bleachclean('Hello')

Expand Down Expand Up @@ -117,3 +131,21 @@ def test_menu_tag(page_tree, rf):
assert out['menuitems'][1].slug == 'test_2'
assert len(out['menuitems'][0].children) == 1
assert out['menuitems'][0].children[0].slug == 'child_1'


def test_metablock_with_no_modifications(render_template):
template_string = '{% metablock %}Hello{% endmetablock %}'
expected_output = 'Hello'
output = render_template(template_string)
assert output == expected_output


def test_metablock_with_modifications(render_template):
template_string = """{% metablock %}
The world's "fastest"

<a href="">supercomputers</a> , period <-> Jane & John Doe.
{% endmetablock %}"""
expected_output = 'The world&#39;s &quot;fastest&quot; supercomputers, period &lt;-&gt; Jane &amp; John Doe.'
output = render_template(template_string)
assert output == expected_output
35 changes: 32 additions & 3 deletions wagtail_extensions/templatetags/wagtailextensions_tags.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from datetime import datetime
from urllib.parse import urlsplit

from django.template import Library
from django.template.defaultfilters import stringfilter
from django.utils import timezone
from django.template import Library, Node
from django.template.defaultfilters import escape, stringfilter
from django.utils import html, timezone

import bleach
from wagtailgeowidget.app_settings import (
Expand Down Expand Up @@ -99,3 +99,32 @@ def menu(context, parent, calling_page=None):
# required by the pageurl tag that we want to use within this template
'request': context['request'],
}


@register.tag
def metablock(parser, token):
"""
Remove newlines, excessive whitespace, and HTML tags; and escape the
content of meta blocks.
"""
nodelist = parser.parse(('endmetablock',))
parser.delete_first_token()
return MetaBlockNode(nodelist)


class MetaBlockNode(Node):
def __init__(self, nodelist):
self.nodelist = nodelist

def render(self, context):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for the render and unescape logic.

output = self.nodelist.render(context)
output = output.replace("\n", "")
output = " ".join(output.split()).replace(" ,", ",").replace(" .", ".")
output = html.strip_tags(unescape(output))
return escape(output)


def unescape(text):
return text.replace(
"&amp;", "&").replace("&lt;", "<").replace("&gt;", ">").replace(
"&quot;", '"').replace("&#39;", "'")