Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #17 from relekang/django-filters
Browse files Browse the repository at this point in the history
Add django filters for markdown and html
  • Loading branch information
relekang committed Mar 29, 2015
2 parents ad8ccf1 + 9c0c7ee commit 74857fb
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/creating-thumbnails.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,46 @@ name for the thumbnail with an ``as`` keyword as shown in the example below.
{% get_thumbnail "image.jpg" "400x400" crop="center" as thumbnail %}
<img src="{{ thumbnail.url }}" alt="The thumbnail" style="width: {{ thumbnail.width }} />


Filters
^^^^^^^

.. autofunction:: thumbnails.templatetags.thumbnails.markdown_thumbnails

.. code-block:: html+django

{% load thumbnails %}

{{ content|markdown_thumbnails }}

.. autofunction:: thumbnails.templatetags.thumbnails.html_thumbnails

.. code-block:: html+django

{% load thumbnails %}

{{ content|html_thumbnails }}

.. autofunction:: thumbnails.templatetags.thumbnails.safe_html_thumbnails

.. code-block:: html+django

{% load thumbnails %}

{{ content|safe_html_thumbnails }}

**Creating custom text filters**

It is possible to create custom text filters by utilizing the ``text_filter`` function described
below.

.. autofunction:: thumbnails.templatetags.thumbnails.text_filter

Below is the code for the ``html_thumbnails``-filter shown as an example of how to use
``text_filter``.

.. code-block:: python
@register.filter
def html_thumbnails(value):
return mark_safe(text_filter('<img(?: alt="(%(caption)s)?")? src="(%(image)s)"', value))
12 changes: 12 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ Image options
an alternative version with the number as a proportions-factor.
| **Default:** ``[2]``

Templatetags and filters
------------------------

.. attribute:: THUMBNAIL_FILTER_OPTIONS
:noindex:

| The options passed into ``get_thumbnail`` by the Markdown and HTML filter. It can contain
all options that is supported by ``get_thumbnails``, however size is required.
| **Default:** ``{'size': '500'}``

Dummy thumbnails
----------------

Expand Down
54 changes: 54 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import unittest

from thumbnails import settings

from .compat import mock
from .utils import has_installed

Expand Down Expand Up @@ -55,3 +57,55 @@ def test_get_thumbnail_templatetag_with_FileField_as_source(self, mock_get_thumb
}))

mock_get_thumbnail.assert_called_once_with(_file, '200x200')


@mock.patch('{}.get_thumbnail'.format(settings.THUMBNAIL_ENGINE), lambda *args: None)
@mock.patch('thumbnails.images.Thumbnail.save', lambda *args: None)
@mock.patch('thumbnails.images.Thumbnail.save_alternative_resolution', lambda *args: None)
@unittest.skipIf(not has_installed('django'), 'Django not installed')
class FilterTestCase(unittest.TestCase):

def setUp(self):
self.MARKDOWN_TEMPLATE = Template('{% load thumbnails %}{{ text|markdown_thumbnails }}')
self.HTML_TEMPLATE = Template('{% load thumbnails %}{{ text|html_thumbnails }}')
self.SAFE_HTML_TEMPLATE = Template('{% load thumbnails %}{{ text|safe_html_thumbnails }}')

def test_markdown_filter(self):
self.assertEqual(self.MARKDOWN_TEMPLATE.render(Context({
'text': 'image: ![Title](/image.jpg)'
})), 'image: ![Title](thumbnails-cache/09a/e5965fd6bcafba14e3e696059acfa3db6ca8e.jpg)')

self.assertEqual(self.MARKDOWN_TEMPLATE.render(Context({
'text': 'image: ![](/image.jpg)'
})), 'image: ![](thumbnails-cache/09a/e5965fd6bcafba14e3e696059acfa3db6ca8e.jpg)')

def test_html_filter(self):
self.assertEqual(self.HTML_TEMPLATE.render(Context({
'text': 'image: <img src="/image.jpg" alt="Title" />'
})), 'image: <img src="thumbnails-cache/09a/e5965fd6bcafba14e3e696059acfa3db6ca8e.jpg"'
' alt="Title" />')

self.assertEqual(self.HTML_TEMPLATE.render(Context({
'text': 'image: <img src="/image.jpg" alt="" />'
})), 'image: <img src="thumbnails-cache/09a/e5965fd6bcafba14e3e696059acfa3db6ca8e.jpg"'
' alt="" />')

self.assertEqual(self.HTML_TEMPLATE.render(Context({
'text': 'image: <img src="/image.jpg" />'
})), 'image: <img src="thumbnails-cache/09a/e5965fd6bcafba14e3e696059acfa3db6ca8e.jpg" />')

def test_safe_html_filter(self):
self.assertEqual(self.SAFE_HTML_TEMPLATE.render(Context({
'text': 'image: <img src="/image.jpg" alt="Title" />'
})), 'image: &lt;img src=&quot;thumbnails-cache/09a/e5965fd6bcafba14e3e696059acfa3db6ca8e'
'.jpg&quot; alt=&quot;Title&quot; /&gt;')

self.assertEqual(self.SAFE_HTML_TEMPLATE.render(Context({
'text': 'image: <img src="/image.jpg" alt="" />'
})), 'image: &lt;img src=&quot;thumbnails-cache/09a/e5965fd6bcafba14e3e696059acfa3db6ca8e'
'.jpg&quot; alt=&quot;&quot; /&gt;')

self.assertEqual(self.SAFE_HTML_TEMPLATE.render(Context({
'text': 'image: <img src="/image.jpg" />'
})), 'image: &lt;img src=&quot;thumbnails-cache/09a/e5965fd6bcafba14e3e696059acfa3db6ca8e'
'.jpg&quot; /&gt;')
3 changes: 3 additions & 0 deletions thumbnails/conf/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
THUMBNAIL_COLORMODE = 'RGB'
THUMBNAIL_ALTERNATIVE_RESOLUTIONS = [2]

THUMBNAIL_FILTER_SIZE = '500'
THUMBNAIL_FILTER_OPTIONS = {'size': '500'}

THUMBNAIL_DUMMY = False
THUMBNAIL_DUMMY_URL = 'http://puppies.lkng.me/{}x{}'
55 changes: 55 additions & 0 deletions thumbnails/templatetags/thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from django import template
from django.template import Library
from django.template.base import TemplateSyntaxError
from django.utils.safestring import mark_safe

from thumbnails import settings

register = Library()

Expand Down Expand Up @@ -49,3 +52,55 @@ def render(self, context):
raise TemplateSyntaxError()

return ''


def text_filter(regex_base, value):
"""
A text-filter helper, used in ``markdown_thumbnails``-filter and ``html_thumbnails``-filter.
It can be used to build custom thumbnail text-filters.
:param regex_base: A string with a regex that contains ``%(captions)s`` and ``%(image)s`` where
the caption and image should be.
:param value: String of text in which the source URLs can be found.
:return: A string ready to be put in a template.
"""
from thumbnails import get_thumbnail
regex = regex_base % {
'caption': '[a-zA-Z0-9\.\,:;/_ \(\)\-\!\?\"]+',
'image': '[a-zA-Z0-9\.:/_\-\% ]+'
}
images = re.findall(regex, value)

for i in images:
image_url = i[1]
image = get_thumbnail(
image_url,
**settings.THUMBNAIL_FILTER_OPTIONS
)
value = value.replace(i[1], image.url)

return value


@register.filter
def markdown_thumbnails(value):
"""
Markdown filter that replaces all images with thumbnails.
"""
return text_filter('!\[(%(caption)s)?\][ ]?\((%(image)s)\)', value)


@register.filter
def safe_html_thumbnails(value):
"""
HTML filter that replaces all images with thumbnails, the returned string is not marked as safe.
"""
return text_filter('<img(?: alt="(%(caption)s)?")? src="(%(image)s)"', value)


@register.filter
def html_thumbnails(value):
"""
HTML filter that replaces all images with thumbnails, the returned string is marked as safe.
"""
return mark_safe(text_filter('<img(?: alt="(%(caption)s)?")? src="(%(image)s)"', value))

0 comments on commit 74857fb

Please sign in to comment.