Skip to content

Commit

Permalink
[#2375] ckan jinja2 truncate filter override
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Jul 18, 2012
1 parent 49ed93a commit 38340ae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions ckan/config/environment.py
Expand Up @@ -305,6 +305,7 @@ def genshi_lookup_attr(cls, obj, key):
env.install_gettext_callables(_, N_, newstyle=True)
# custom filters
env.filters['empty_and_escape'] = lib.jinja_tags.empty_and_escape
env.filters['truncate'] = lib.jinja_tags.truncate
config['pylons.app_globals'].jinja_env = env

# CONFIGURATION OPTIONS HERE (note: all config options will override
Expand Down
12 changes: 12 additions & 0 deletions ckan/lib/jinja_tags.py
Expand Up @@ -5,6 +5,7 @@
from jinja2.ext import Extension
from jinja2.exceptions import TemplateNotFound
from jinja2.utils import open_if_exists, escape
from jinja2.filters import do_truncate

import lib.base as base
import lib.helpers as h
Expand All @@ -19,6 +20,17 @@ def empty_and_escape(value):
else:
return escape(value)

def truncate(value, length=255, killwords=None, end='...'):
''' A more clever truncate. If killwords is supplied we use the default
truncate. Otherwise we try to truncate using killwords=False, if this
truncates the whole value we try again with killwords=True '''
if killwords is not None:
return do_truncate(value, length=length, killwords=killwords, end=end)
result = do_truncate(value, length=length, killwords=False, end=end)
if result != end:
return result
return do_truncate(value, length=length, killwords=True, end=end)

### Tags

class CkanExtend(Extension):
Expand Down

0 comments on commit 38340ae

Please sign in to comment.