import re from django.template import base as template from django.conf import settings from django.template.defaultfilters import stringfilter register = template.Library() STATIC_RE = re.compile(r'(\{\{\s*STATIC_URL\s*\}\})|(/static/)') @register.filter @stringfilter def static_url(value): """Searches for {{ STATIC_URL }} or /static/ and replaces it with the STATIC_URL from settings.py""" value = STATIC_RE.sub(settings.STATIC_URL, value) return value static_url.is_safe = True
try: from django.template import base as template_base except ImportError: from django import template as template_base register = template_base.Library() def raw(parser, token): # Whatever is between {% raw %} and {% endraw %} will be preserved as # raw, unrendered template code. text = [] parse_until = 'endraw' tag_mapping = { template_base.TOKEN_TEXT: ('', ''), template_base.TOKEN_VAR: ('{{', '}}'), template_base.TOKEN_BLOCK: ('{%', '%}'), template_base.TOKEN_COMMENT: ('{#', '#}'), } # By the time this template tag is called, the template system has already # lexed the template into tokens. Here, we loop over the tokens until # {% endraw %} and parse them to TextNodes. We have to add the start and # end bits (e.g. "{{" for variables) because those have already been # stripped off in a previous part of the template-parsing process. while parser.tokens: token = parser.next_token() if token.token_type == template_base.TOKEN_BLOCK and token.contents == parse_until: return template_base.TextNode(u''.join(text)) start, end = tag_mapping[token.token_type] text.append(u'%s%s%s' % (start, token.contents, end)) parser.unclosed_block_tag(parse_until)
# vim: tabstop=4 shiftwidth=4 softtabstop=4 # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from django.template import base from django.template import defaultfilters from django.utils import safestring register = base.Library() @register.filter(is_safe=True) @defaultfilters.stringfilter def shellfilter(value): """Replace HTML chars for shell usage.""" replacements = {'\\': '\\\\', '`': '\`', "'": "\\'", '"': '\\"'} for search, repl in replacements.items(): value = value.replace(search, repl) return safestring.mark_safe(value)
from hashlib import md5 from django.template import base as tmp from django.utils.safestring import mark_safe register = tmp.Library() class ComponentNode(tmp.Node): def __init__(self, component_key, kwargs): self.component_key = component_key self.ignore_missing = kwargs.pop('ignore_missing', False) self.kwargs = kwargs def render(self, context): from django.core.urlresolvers import reverse, NoReverseMatch if 'url_kwargs_dict' in self.kwargs and len(self.kwargs) == 1: kwargs = self.kwargs['url_kwargs_dict'].resolve(context) else: if self.kwargs: kwargs = { key: unicode(value.resolve(context)) for key, value in self.kwargs.iteritems() } else: kwargs = {} component_key = self.component_key.resolve(context) if not component_key: