def hashtag(algo, lib=False): def inner(value): return hexd(algo, value) return filter(function(inner), doc='Returns %s hexadecimal hash of the value. %s' % \ (algo.upper(), lib and 'Requires the :mod:`hashlib` module' or ''), name=algo, test={'args':('wtf',),'result':hexd(algo, 'wtf')})
def codectag(encoding, codec=True): codec = 'b%d%s' % (encoding, codec and 'encode' or 'decode') def inner(s, *args, **kwargs): return getattr(base64, codec)(s, *args, **kwargs) inner.__name__ = codec inner.__doc__ = getattr(base64, codec).__doc__ + """ Syntax:: {%% %s [string] [options] %%} """ % codec return filter(function(inner))
def func_factory(method): try: func = getattr(math, method) except AttributeError: return def inner(arg1, arg2=None): try: return func(arg1, arg2) except TypeError: return func(arg1) inner.__name__ = method doc = func.__doc__.splitlines() if len(doc) > 1 and not doc[1]: doc = doc[2:] inner.__doc__ = '\n'.join(doc) if method.startswith('is'): return comparison(inner) return filter(function(inner))
from native_tags.decorators import function, block, filter from django.template import Template, Context def document(o): 'Returns the docstring for a given object' try: return o.__doc__ or '' except AttributeError: return '' document = filter(function(document)) def do_set(context, **kwargs): 'Updates the context with the keyword arguments' for k, v in kwargs.items(): context[k] = v return '' do_set = function(do_set, takes_context=1, name='set') def do_del(context, *args): 'Deletes template variables from the context' for name in args: del context[name] return '' do_del = function(do_del, resolve=0, takes_context=1, name='del') def render_block(context, nodelist): 'Simply renders the nodelist with the current context' return nodelist.render(context)
from django.template import TemplateSyntaxError from native_tags.decorators import filter from _markup import formatter def apply_markup(value, arg=None): """ Applies text-to-HTML conversion. Takes an optional argument to specify the name of a filter to use. """ if arg is not None: return formatter(value, filter_name=arg) return formatter(value) apply_markup = filter(apply_markup, is_safe=True) def smartypants(value): """ Applies SmartyPants to a piece of text, applying typographic niceties. Requires the Python SmartyPants library to be installed; see http://web.chad.org/projects/smartypants.py/ """ try: from smartypants import smartyPants except ImportError: if settings.DEBUG: raise TemplateSyntaxError("Error in smartypants filter: the Python smartypants module is not installed or could not be imported")
prefixes = ['http://', 'https://', 'ftp://'] replacers = {} for chunk in value.split(): for prefix in prefixes: if chunk.lower().startswith(prefix): newchunk = ''.join(( chunk[:len(prefix)], user.username, '@', chunk[len(prefix):])) replacers[chunk] = newchunk break for chunk, replacer in replacers.items(): value = value.replace(chunk, replacer, 1) return value put_username_into_url = filter(put_username_into_url) def is_watched(watchable, by_bit=None, user=None): """ Usage:: {% is_watched WATCHABLE by USER [as "my_var"] %} Provided context contains ``task`` and ``user`` objects we can use it as follows:: {% is_watched task by user %} """ assert watchable and isinstance(watchable, Watchable) assert by_bit == 'by' assert user
from native_tags.decorators import function, comparison, filter from datetime import datetime def dynamic(*a, **kw): return list(a) + sorted(kw.items()) dynamic = function(dynamic) def no_render(*a, **kw): return list(a) + sorted(kw.items()) no_render = function(no_render, resolve=False) def myfilter(value, arg):# a, b, c): return value + arg myfilter = filter(myfilter) def adder(x, y): return x + y adder = function(adder, name='add') def cmp_kwargs(**kw): return len(kw) cmp_kwargs = comparison(cmp_kwargs) def myinc(noun): return 'unittest.html', {'noun': noun} myinc = function(myinc, inclusion=True) def ifsomething(): return True ifsomething = comparison(ifsomething)
return value prefixes = ['http://', 'https://', 'ftp://'] replacers = {} for chunk in value.split(): for prefix in prefixes: if chunk.lower().startswith(prefix): newchunk = ''.join((chunk[:len(prefix)], user.username, '@', chunk[len(prefix):])) replacers[chunk] = newchunk break for chunk, replacer in replacers.items(): value = value.replace(chunk, replacer, 1) return value put_username_into_url = filter(put_username_into_url) def is_watched(watchable, by_bit=None, user=None): """ Usage:: {% is_watched WATCHABLE by USER [as "my_var"] %} Provided context contains ``task`` and ``user`` objects we can use it as follows:: {% is_watched task by user %} """ assert watchable and isinstance(watchable, Watchable) assert by_bit == 'by'
def hashtag(algo, lib=False): def inner(value): return hexd(algo, value) return filter(function(inner),doc='Returns %s hexadecimal hash of the value. %s' % (algo.upper(), lib and 'Requires the :mod:`hashlib` module' or ''),name=algo)
from native_tags.decorators import function, comparison, filter from datetime import datetime def dynamic(*a, **kw): return list(a) + sorted(kw.items()) dynamic = function(dynamic) def no_render(*a, **kw): return list(a) + sorted(kw.items()) no_render = function(no_render, resolve=False) def myfilter(value, arg): return value + arg myfilter = filter(myfilter, test={'args':(1,1),'result':2}) def adder(x, y): return x + y adder = function(adder, name='add', test={'args':(1,1),'result':2}) def cmp_kwargs(**kw): return len(kw) cmp_kwargs = comparison(cmp_kwargs) def myinc(noun): return 'unittest.html', {'noun': noun} myinc = function(myinc, inclusion=True) def ifsomething(): return True ifsomething = comparison(ifsomething)
dynamic = function(dynamic) def no_render(*a, **kw): return list(a) + sorted(kw.items()) no_render = function(no_render, resolve=False) def myfilter(value, arg): return value + arg myfilter = filter(myfilter, test={'args': (1, 1), 'result': 2}) def adder(x, y): return x + y adder = function(adder, name='add', test={'args': (1, 1), 'result': 2}) def cmp_kwargs(**kw): return len(kw) cmp_kwargs = comparison(cmp_kwargs)
from django.template import TemplateSyntaxError from native_tags.decorators import filter from _markup import formatter def apply_markup(value, arg=None): """ Applies text-to-HTML conversion. Takes an optional argument to specify the name of a filter to use. """ if arg is not None: return formatter(value, filter_name=arg) return formatter(value) apply_markup = filter(apply_markup, is_safe=True) apply_markup.test = {'args':('wtf',),'result':'<p>wtf</p>'} def smartypants(value): """ Applies SmartyPants to a piece of text, applying typographic niceties. Requires the Python SmartyPants library to be installed; see http://web.chad.org/projects/smartypants.py/ """ try: from smartypants import smartyPants except ImportError: if settings.DEBUG: