Login

Enhanced "avoid widows" template filters

Author:
SmileyChris
Posted:
July 25, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

Building on jcroft's snippet, here's a slightly more advanced version which has two filters, one for basic text and the other for html snippets.

Usage is like so:

<h2>{{ blog_entry.headline|escape|widont }}</h2>
{{ blog_entry.html|widont_html }}

On top of Jeff's reasons for using these filters, they are important because they help keep one of God's commandments. ;)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from django.template import Library
from django.utils.encoding import force_unicode
import re

register = Library()

re_widont = re.compile(r'\s+(\S+\s*)$')
def widont(value, count=1):
    """
    Adds an HTML non-breaking space between the final two words of the string to
    avoid "widowed" words.

    Examples:

    >>> print widont('Test   me   out')
    Test   me&nbsp;out

    >>> widont('It works with trailing spaces too  ')
    u'It works with trailing spaces&nbsp;too  '

    >>> print widont('NoEffect')
    NoEffect
    """
    def replace(matchobj):
        return u'&nbsp;%s' % matchobj.group(1)
    for i in range(count):
        value = re_widont.sub(replace, force_unicode(value))
    return value

re_widont_html = re.compile(r'([^<>\s])\s+([^<>\s]+\s*)(</?(?:address|blockquote|br|dd|div|dt|fieldset|form|h[1-6]|li|noscript|p|td|th)[^>]*>|$)', re.IGNORECASE)
def widont_html(value):
    """
    Adds an HTML non-breaking space between the final two words at the end of
    (and in sentences just outside of) block level tags to avoid "widowed"
    words.

    Examples:

    >>> print widont_html('<h2>Here is a simple  example  </h2> <p>Single</p>')
    <h2>Here is a simple&nbsp;example  </h2> <p>Single</p>

    >>> print widont_html('<p>test me<br /> out</p><h2>Ok?</h2>Not in a p<p title="test me">and this</p>')
    <p>test&nbsp;me<br /> out</p><h2>Ok?</h2>Not in a&nbsp;p<p title="test me">and&nbsp;this</p>

    >>> print widont_html('leading text  <p>test me out</p>  trailing text')
    leading&nbsp;text  <p>test me&nbsp;out</p>  trailing&nbsp;text
    """
    def replace(matchobj):
        return u'%s&nbsp;%s%s' % matchobj.groups()
    return re_widont_html.sub(replace, force_unicode(value))

register.filter(widont)
register.filter(widont_html)

def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 9 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
  5. Help text hyperlinks by sa2812 1 year, 5 months ago

Comments

Please login first before commenting.