Example #1
0
def patchdjango():
    patch('django.http.request.QueryDict.get', TAINT_SOURCE)
    patch('django.http.request.QueryDict.__getitem__', TAINT_SOURCE)
    patch('django.db.models.sql.compiler.SQLCompiler.patch', TAINT_SOURCE)
    patch(
        'django.utils.html.escape', TAINT_SANITIZER,
        set([
            contexts.HTML_TEXT, contexts.HTML_QUOT_ATTR,
            contexts.HTML_APOS_ATTR
        ]))
    patch(
        'django.utils.html.escapejs', TAINT_SANITIZER,
        set([
            contexts.HTML_JS_DATA, contexts.HTML_QUOT_JS, contexts.HTML_APOS_JS
        ]))
    patch(
        'urllib.urlencode', TAINT_SANITIZER,
        set([
            contexts.HTML_QUOT_URI, contexts.HTML_APOS_URI,
            contexts.CSS_QUOT_URI, contexts.CSS_APOS_URI
        ]))
    patch(
        'django.utils.six.moves.urllib.parse.urlencode', TAINT_SANITIZER,
        set([
            contexts.HTML_QUOT_URI, contexts.HTML_APOS_URI,
            contexts.CSS_QUOT_URI, contexts.CSS_APOS_URI
        ]))
    patch('django.utils.html.conditional_escape', TAINT_SINK)
    defaultfilters.register.filter(name='safe',
                                   filter_func=stringfilter(safe),
                                   is_safe=True)
    patch('django.template.debug.DebugVariableNode.render', TAINT_SINK)
    patch('django.core.handlers.base.BaseHandler.get_response', PARSER)
    patch('django.template.base.FilterExpression.resolve', RESOLVER)
    patch('django.template.base.VariableNode.render', TAINT_SINK)
Example #2
0
            '>': '>',
            '\\': (escape_highlight % '\\\\'),
            }
        try:
            return submap[match.group()]
        except KeyError:
            return html_highlight %  match.group()[1:-1]
    return ESCAPE_RE.sub(replace, text)

WHITESPACE_RE = re.compile('^ +| +$|[\r\n\t] +| {2,}')
def fancy_spaces(text):
    """Highlight spaces to make them easily visible"""
    def replace(match):
        fancy_space = '<span class="translation-space"> </span>'
        if match.group().startswith(' '):
            return fancy_space * len(match.group())
        return match.group()[0] + fancy_space * (len(match.group()) - 1)
    return WHITESPACE_RE.sub(replace, text)

def clean_wrapper(text):
    """wrapper around lxml's html cleaner that returns SafeStrings for
    immediate rendering in templates"""
    return mark_safe(clean_html(text))

def fancy_highlight(text):
    return mark_safe(fancy_spaces(fancy_escape(text)))

register = template.Library()
register.filter('clean', stringfilter(clean_wrapper))
register.filter('fancy_highlight', stringfilter(fancy_highlight))
Example #3
0
def truncate_chars(s, num):
    s = force_unicode(s)
    length = int(num)
    if len(s) > length:
        length = length - 3
        s = s[:length].strip()
        s += '...'
    return s
truncate_chars = allow_lazy(truncate_chars, unicode)


@register.filter
def truncatechars(value, arg):
    """
    Truncates a string after a certain number of characters, but respects word boundaries.

    Argument: Number of characters to truncate after.
    """
    try:
        length = int(arg)
    except ValueError: # If the argument is not a valid integer.
        return value # Fail silently.
    return truncate_chars(value, length)
truncatechars.is_safe = True
truncatechars = stringfilter(truncatechars)


@register.filter(name='get_class')
def get_class(value):
  return value.__class__.__name__
Example #4
0
from localeurl import utils

register = template.Library()


def chlocale(url, locale):
    """
    Changes the URL's locale prefix if the path is not locale-independent.
    Otherwise removes locale prefix.
    """
    _, path = utils.strip_script_prefix(url)
    _, path = utils.strip_path(path)
    return utils.locale_url(path, locale)


chlocale = stringfilter(chlocale)
register.filter('chlocale', chlocale)


def rmlocale(url):
    """Removes the locale prefix from the URL."""
    script_prefix, path = utils.strip_script_prefix(url)
    _, path = utils.strip_path(path)
    return ''.join([script_prefix, path])


rmlocale = stringfilter(rmlocale)
register.filter('rmlocale', rmlocale)


def locale_url(parser, token):
Example #5
0
from django import template
from django.template.defaultfilters import stringfilter
from film20.oembed.core import replace

register = template.Library()

def oembed(input):
    return replace(input)
oembed.is_safe = True
oembed = stringfilter(oembed)

register.filter('oembed', oembed)

def do_oembed(parser, token):
    """
    A node which parses everything between its two nodes, and replaces any links
    with OEmbed-provided objects, if possible.
    
    Supports one optional argument, which is the maximum width and height, 
    specified like so:
    
        {% oembed 640x480 %}http://www.viddler.com/explore/SYSTM/videos/49/{% endoembed %}
    """
    args = token.contents.split()
    if len(args) > 2:
        raise template.TemplateSyntaxError("Oembed tag takes only one (option" \
            "al) argument: WIDTHxHEIGHT, where WIDTH and HEIGHT are positive " \
            "integers.")
    if len(args) == 2:
        width, height = args[1].lower().split('x')
        if not width and height:
Example #6
0
        )
    def remove_js_events(match):
        return "".join((
            match.group(1),
            re_attrs.sub('', match.group(2)),
            match.group(3),
            ))
    html = re_comments.sub("", html)
    html = re_tags.sub(remove_js_events, html)
    if "</a>" not in html:
        html = defaultfilters.urlizetrunc(html, "30")
    if "</p>" not in html:
        html = defaultfilters.linebreaks(html)
    html = defaultfilters.safe(html)
    return html
disarm_user_input = defaultfilters.stringfilter(disarm_user_input)
register.filter('disarm_user_input', disarm_user_input, is_safe=True)

def humanize_url(url, letter_count):
    letter_count = int(letter_count)
    re_start = re.compile(r'^https?://')
    re_end = re.compile(r'/$')
    url = re_end.sub("", re_start.sub("", url))
    if len(url) > letter_count:
        url = url[:letter_count - 1] + u"…"
    return url
register.filter('humanize_url', humanize_url)

def truncated_multiply(value, arg):
    """
    Multiplies the arg with the value and returns 
'''
Created on 2011-11-20

@author: liwenjian
'''
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
def truncatestring(value, arg):
    try:
        length = int(arg)
    except ValueError:
        return value
    
    if len(value) < length:
        return value
    else:
        return value[0:length] + '...'
truncatestring.is_safe = True
truncatestring = stringfilter(truncatestring)
    else:
        result = identifier
    return mark_safe(result)


register.filter("link_if_url", link_if_url)


def replaceunderscore(value):
    """
    Replaces underscore with spaces
    """
    return mark_safe(re.sub("_", " ", value))


slugify = stringfilter(replaceunderscore)
register.filter("replaceunderscore", replaceunderscore, is_safe=True)


def wrap(text, width):
    """
    A word-wrap function that preserves existing line breaks and most spaces in
    the text. Expects that existing line breaks are posix newlines.
    """
    text = force_unicode(text)

    def _lines(text):
        for index in range(0, len(text), width):
            yield text[index : index + width] + "\n"

    return u"".join(_lines(text))
    For example::

      {{ value|urlize_hashtags }}

    If value is "This is a #test.", the output will be "This is a
    <a href="[reversed url for hashtagged_item_list(request, hashtag='test')]">#test</a>.".

    Note that if ``urlize_hashtags`` is applied to text that already contains
    HTML markup, things won't work as expected. Prefer apply this filter to
    plain text.
    """
    from hashtags.utils import urlize_hashtags
    return mark_safe(urlize_hashtags(value))
urlize_hashtags.is_safe = True
urlize_hashtags = stringfilter(urlize_hashtags)

def urlize_and_track_hashtags(value, object_to_track):
    """
    Works like ``urlize_hashtags`` but you can pass a object parameter to
    link/relate hashtags on text with the object in question.

    Usage example::

        {{ value|urlize_and_track_hashtags:object_to_track }}

    Real world example::

        {{ flatpage.content|urlize_and_track_hashtags:flatpage }}

    **Important**: ``urlize_and_track_hashtags`` doesn't works property if your
Example #10
0
    ('<', r'\u003C'),
    ('&', r'\u0026'),
    ('=', r'\u003D'),
    ('-', r'\u002D'),
    (';', r'\u003B'),
    (u'\u2013', r'\u2013'),
    (u'\u2014', r'\u2014'),
    (u'\u2019', r'\u2019'),
    (u'\u201c', r'\u201c'),
    (u'\u201d', r'\u201d'),
    (u'\u2028', r'\u2028'),
    (u'\u2029', r'\u2029'),
    (u'\xbd', r'\xbd')
)

# Escape every ASCII character with a value less than 32.
_js_escapes = (_base_js_escapes +
               tuple([('%c' % z, '\\u%04X' % z) for z in range(32)]))

def escapejs(value):
    """
    Replaces Django's built-in escaping function with one that actually works.

    Take from "Changeset 12781":http://code.djangoproject.com/changeset/12781
    """
    for bad, good in _js_escapes:
        value = value.replace(bad, good)
    return value
escapejs = stringfilter(escapejs)
register.filter(escapejs)
        ordering = ('slug',)
        verbose_name = _(u'Tag')
        verbose_name_plural = _(u'Tags')

versioning.register(Tag)


def slugify(value):
    # normalize unicode
    import unicodedata
    value = unicodedata.normalize('NFKD', unicode(value))
    # remove non-word characters
    misc_characters = re.compile(('[^\w]'), re.UNICODE)
    value = re.sub(misc_characters, '', value)
    return value.lower()
slugify = stringfilter(slugify)


class PageTagSet(models.Model):
    page = models.OneToOneField('pages.Page')
    tags = models.ManyToManyField(Tag)
    region = models.ForeignKey(Region, null=True)

    objects = models.GeoManager()

    def __unicode__(self):
        return ', '.join(map(unicode, self.tags.all()))

    class Meta:
        ordering = ('page__slug',)
Example #12
0
            truncd_val = value[:max_length]
            truncd_val = truncd_val.rstrip()
            return truncd_val + "..."
        return value
    except:
        return ""


def stripentities(value):
    """Strips all HTML entities"""
    from django.utils.html import strip_entities
    return strip_entities(value)


stripentities.is_safe = True
stripentities = stringfilter(stripentities)
register.filter(stripentities)


@register.filter
def friendly_date(date, include_time=False):
    """
  Prints a human friendly date.
  """
    delta = datetime.now() - date

    if delta < timedelta(seconds=60):
        msg = _("Just now")
    elif delta < timedelta(seconds=60 * 60):
        minutes = delta.seconds / 60
        msg = ngettext('%(minutes)s minute ago', '%(minutes)s min ago',
Example #13
0
# Copyright 2008-2012 Zuza Software Foundation
#
# This file is part of Pootle.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from django import template
from django.template.defaultfilters import stringfilter

from pootle_misc.baseurl import l, m, s, abs_l, get_next


register = template.Library()

register.filter('l', stringfilter(l))
register.filter('m', stringfilter(m))
register.filter('s', stringfilter(s))
register.filter('abs_l', stringfilter(abs_l))
register.filter('get_next', get_next)
                if len(lines) > 1:
                    pos = len(lines[-1])
            yield word
    return u''.join(_generator())
    
    
def smartwordwrap(value, arg):
    """
    Wraps words at specified line length.

    Argument: number of characters to wrap the text at.
    """
    return wrap(value, int(arg))
        
smartwordwrap.is_safe = True
smartwordwrap = stringfilter(smartwordwrap)
register.filter(smartwordwrap)
    
@register.filter
def trunc( string, number, dots='...'):
  """ 
  truncate the {string} to {number} characters
  print {dots} on the end if truncated

  usage: {{ "some text to be truncated"|trunc:6 }}
  results: some te...
  """
  if not isinstance(string, str): string = str(string)
  if len(string) <= number:
    return string
  return string[0:number]+dots
Example #15
0
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

def trim(value, num):
    return value[:num]
trim = stringfilter(trim)

register.filter(trim)

@register.simple_tag
def no_params():
    """Expected no_params __doc__"""
    return "no_params - Expected result"
no_params.anything = "Expected no_params __dict__"

@register.simple_tag
def one_param(arg):
    """Expected one_param __doc__"""
    return "one_param - Expected result: %s" % arg
one_param.anything = "Expected one_param __dict__"

@register.simple_tag(takes_context=False)
def explicit_no_context(arg):
    """Expected explicit_no_context __doc__"""
    return "explicit_no_context - Expected result: %s" % arg
explicit_no_context.anything = "Expected explicit_no_context __dict__"

@register.simple_tag(takes_context=True)
def no_params_with_context(context):
from django import template
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter
from django.utils.text import normalize_newlines

register = template.Library()


@register.filter(is_safe=True)
def remove_newlines(text):
    """
    Removes all newline characters from a block of text.
    """
    # First normalize the newlines using Django's nifty utility
    normalized_text = normalize_newlines(text)
    # Then simply remove the newlines like so.
    return mark_safe(normalized_text.replace('\n', ' '))

remove_newlines = stringfilter(remove_newlines)
register.filter(remove_newlines)
Example #17
0
from pygments import highlight as hl
from pygments.lexers import PythonLexer, PythonConsoleLexer, BashSessionLexer, HtmlDjangoLexer
from pygments.formatters import HtmlFormatter

from xml.sax.saxutils import unescape

register = template.Library()

@register.filter
def highlight(value):
    """ Highlight the python code with pygments"""
    soup = BeautifulSoup(value)
    prespy = soup.findAll('pre', lang="python")
    prespyco = soup.findAll('pre', lang="python_console")
    presbs = soup.findAll('pre', lang="bash")
    presht = soup.findAll('pre', lang="html")

    for pre in prespy:
        pre.replaceWith(hl(unescape(pre.string), PythonLexer(), HtmlFormatter()))
    for pre in prespyco:
        pre.replaceWith(hl(unescape(pre.string), PythonConsoleLexer(), HtmlFormatter()))
    for pre in presbs:
        pre.replaceWith(hl(unescape(pre.string), BashSessionLexer(), HtmlFormatter()))
    for pre in presht:
        pre.replaceWith(hl(unescape(pre.string), HtmlDjangoLexer(), HtmlFormatter()))

    return soup
highlight.is_safe = True
highlight = stringfilter(highlight)
            yield word

    return u''.join(_generator())


def smartwordwrap(value, arg):
    """
    Wraps words at specified line length.

    Argument: number of characters to wrap the text at.
    """
    return wrap(value, int(arg))


smartwordwrap.is_safe = True
smartwordwrap = stringfilter(smartwordwrap)
register.filter(smartwordwrap)


@register.filter
def trunc(string, number, dots='...'):
    """ 
  truncate the {string} to {number} characters
  print {dots} on the end if truncated

  usage: {{ "some text to be truncated"|trunc:6 }}
  results: some te...
  """
    if not isinstance(string, str): string = str(string)
    if len(string) <= number:
        return string
Example #19
0
    # normalize unicode
    import unicodedata

    value = unicodedata.normalize("NFKD", unicode(value))

    # remove non-{word,space,keep} characters
    misc_characters = re.compile(("[^\w\s%s]" % keep), re.UNICODE)
    value = re.sub(misc_characters, "", value)
    value = value.strip()
    value = re.sub("[_\s]+", " ", value)

    return value.lower()


slugify = stringfilter(slugify)


def name_to_url(value):
    """Converts page name to its canonical URL path
    """
    # spaces to underscore
    value = re.sub("[\s]", "_", value.strip())
    # url-encode
    value = quote(value.encode("utf-8"))
    return mark_safe(value)


name_to_url.is_safe = True
name_to_url = stringfilter(name_to_url)
Example #20
0
        url = GRAVATAR_URL % gravatargs
        if "as" in self.other_kwargs:
            context[self.other_kwargs["as"]] = mark_safe(url)
            return ""
        return url


def gravatar(email):
    """
    Takes an e-mail address and returns a gravatar image URL, using properties
    from the django settings file.
    """
    hashed_email = md5.new(email).hexdigest()
    return mark_safe(
        GRAVATAR_URL
        % {
            "hash": hashed_email,
            "rating": GRAVATAR_MAX_RATING,
            "size": GRAVATAR_SIZE,
            "default": urllib.quote_plus(GRAVATAR_DEFAULT_IMG),
        }
    )


gravatar = stringfilter(gravatar)


register = template.Library()
register.filter("gravatar", gravatar)
register.tag("get_gravatar_url", get_gravatar_url)
Example #21
0
    """Converts URLs in html into clickable links."""
    from BeautifulSoup import BeautifulSoup
    ignored_tags = ['a', 'code', 'pre']
    soup = BeautifulSoup(value)
    tags = soup.findAll(True)
    text_all = soup.contents
    for text in text_all:
        if text not in tags:
            parsed_text = urlize(text, nofollow=True, autoescape=autoescape)
            text.replaceWith(parsed_text)
    for tag in tags:
        if not tag.name in ignored_tags:
            soup_text = BeautifulSoup(str(tag))
            if len(soup_text.findAll()) > 1:
                for child_tag in tag.contents:
                    child_tag.replaceWith(html_urlize(str(child_tag)))
            elif len(soup_text.findAll()) > 0:
                text_list = soup_text.findAll(text=True)
                for text in text_list:
                    parsed_text = urlize(text, nofollow=True, autoescape=autoescape)
                    text.replaceWith(parsed_text)
                try:
                    tag.replaceWith(str(soup_text))
                except:
                    pass
    return mark_safe(str(soup))

html_urlize.is_safe = True
html_urlize.needs_autoescape = True
html_urlize = stringfilter(html_urlize)
register.filter(html_urlize)
Example #22
0
#:coding=utf-8:

from django import template
from django.template.defaultfilters import stringfilter
from django.utils.text import normalize_newlines

register = template.Library()


def replace_newlines(text, replacement=' '):
    """
    Replaces all newline characters in a block of text.
    """
    normalized_text = normalize_newlines(text)
    return normalized_text.replace('\n', replacement)
replace_newlines.is_safe = True
replace_newlines = stringfilter(replace_newlines)
register.filter(replace_newlines)
Example #23
0
from django.template import Library, Node
from django.template.defaultfilters import stringfilter
import re

register = Library()


def list_items(value):
    """
    Turns paragraphs delineated with newline characters into
    list items wrapped in <li> and </li> HTML tags.
    """
    items = re.split(r'[\r\n]+', value)
    items = ['<li>%s</li>' % p.strip() for p in items]
    return '\n'.join(items)


paragraphs = stringfilter(list_items)

register.filter(paragraphs)
Example #24
0
    string = re.sub("(?<!>)(BCP ?)0{0,3}(\d+)",
                    "<a href=\"/doc/bcp\\2/\">\\1\\2</a>", string)
    string = re.sub("(?<!>)(STD ?)0{0,3}(\d+)",
                    "<a href=\"/doc/std\\2/\">\\1\\2</a>", string)
    string = re.sub("(?<!>)(FYI ?)0{0,3}(\d+)",
                    "<a href=\"/doc/fyi\\2/\">\\1\\2</a>", string)
    string = re.sub("(?<!>)(draft-[-0-9a-zA-Z._+]+)",
                    "<a href=\"/doc/\\1/\">\\1</a>", string)
    string = re.sub("(?<!>)(conflict-review-[-0-9a-zA-Z._+]+)",
                    "<a href=\"/doc/\\1/\">\\1</a>", string)
    string = re.sub("(?<!>)(status-change-[-0-9a-zA-Z._+]+)",
                    "<a href=\"/doc/\\1/\">\\1</a>", string)
    return mark_safe(string)


urlize_ietf_docs = stringfilter(urlize_ietf_docs)


@register.filter(name='dashify')
def dashify(string):
    """
    Replace each character in string with '-', to produce
    an underline effect for plain text files.
    """
    return re.sub('.', '-', string)


@register.filter
def underline(string):
    """Return string with an extra line underneath of dashes, for plain text underlining."""
    return string + "\n" + ("-" * len(string))
Example #25
0
    for ul_tag in soup.find_all("ul"):
        for li_tag in ul_tag.find_all("li"):
            # li_tag.smooth()
            li_tag.insert_before(f"* ")
            li_tag.unwrap()
        ul_tag.unwrap()
    for blockquote_tag in soup.find_all("blockquote"):
        quote_text = re.sub(r"\n*</?blockquote>\n?", "",
                            blockquote_tag.decode(formatter=None))
        quote_text = re.sub(r"\n", "\n>", quote_text)
        quote_text = "\n>" + quote_text + "\n"
        blockquote_tag.replace_with(quote_text)
    for pre_tag in soup.find_all("pre"):
        pre_tag.insert_before("```\n")
        pre_tag.insert_after("```\n")
        pre_tag.unwrap()
    output = soup.decode(formatter=None)
    output = re.sub(r"\n\n+", "\n\n", output)

    if len(links) > 0:
        output += "\n## Links\n\n"
    for link in links:
        output += f"=>{link[0]} {link[1]}\n"

    return output


register = template.Library()
register.filter("markdown", stringfilter(smartypants_markdown))
register.filter("gemtext", stringfilter(gemtext))
Example #26
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2008-2014 Zuza Software Foundation
#
# This file is part of Pootle.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from django import template
from django.template.defaultfilters import stringfilter

from pootle_misc.baseurl import s, get_next

register = template.Library()

register.filter('s', stringfilter(s))
register.filter('get_next', get_next)
Example #27
0
        unique_words.sort()
    return unique_words


def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha chars and
    converts spaces to hyphens.
    """
    import unicodedata
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
    value = unicode(re.sub('[^\w\s\-\+]', '', value).strip().lower())
    return re.sub('[\-\+\s]+', '-', value)


slugify = stringfilter(slugify)


def slugify_uniquely(model, value):
    """
    Generate a unique slug.
    Based on:
    http://code.djangoproject.com/wiki/SlugifyUniquely
    """
    suffix = 0
    potential = base = slugify(value)
    while True:
        if suffix:
            potential = "-".join([base, str(suffix)])
        if not model.objects.filter(slug=potential).count():
            return potential
Example #28
0
# -*- coding: utf-8 -*-
from django import template
from django.template.defaultfilters import stringfilter

from djangoutils.core import helpers

register = template.Library()

truncate = register.filter(stringfilter(helpers.truncate))
      {{ value|urlize_hashtags }}

    If value is "This is a #test.", the output will be "This is a
    <a href="[reversed url for hashtagged_item_list(request, hashtag='test')]">#test</a>.".

    Note that if ``urlize_hashtags`` is applied to text that already contains
    HTML markup, things won't work as expected. Prefer apply this filter to
    plain text.
    """
    from hashtags.utils import urlize_hashtags
    return mark_safe(urlize_hashtags(value, formatted_url))


urlize_hashtags.is_safe = True
urlize_hashtags = stringfilter(urlize_hashtags)


def urlize_and_track_hashtags(value, object_to_track):
    """
    Works like ``urlize_hashtags`` but you can pass a object parameter to
    link/relate hashtags on text with the object in question.

    Usage example::

        {{ value|urlize_and_track_hashtags:object_to_track }}

    Real world example::

        {{ flatpage.content|urlize_and_track_hashtags:flatpage }}
Example #30
0
#:coding=utf-8:

from django.template import Library
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe

register = Library()

def stripentities(value):
    """Strips all HTML entities"""
    from django.utils.html import strip_entities
    return strip_entities(value)
stripentities.is_safe = True
stripentities = stringfilter(stripentities)
register.filter(stripentities)

@register.filter
def to_anchor(text, autoescape=None):
    from beproud.utils.html import urlize
    return mark_safe(urlize(text, attrs={"rel": "nofollow", "target": "_blank"}, autoescape=autoescape))
to_anchor.is_safe=True
to_anchor.needs_autoescape = True
to_anchor = stringfilter(to_anchor)

@register.filter
def to_anchortrunc(text, limit, autoescape=None):
    from beproud.utils.html import urlize
    return mark_safe(urlize(text, attrs={"rel": "nofollow", "target": "_blank"}, 
                     trim_url_limit=limit, autoescape=autoescape))
to_anchortrunc.is_safe=True
to_anchortrunc.needs_autoescape = True
Example #31
0
#:coding=utf-8:

from django import template
from django.template.defaultfilters import stringfilter
from django.utils.text import normalize_newlines

register = template.Library()


def replace_newlines(text, replacement=' '):
    """
    Replaces all newline characters in a block of text.
    """
    normalized_text = normalize_newlines(text)
    return normalized_text.replace('\n', replacement)


replace_newlines.is_safe = True
replace_newlines = stringfilter(replace_newlines)
register.filter(replace_newlines)
Example #32
0
@register.filter
@stringfilter
def abbreviate_fqdn(value):
    return value.split(".")[0]


@register.filter
@stringfilter
def render_os(os):
    try:
        t, flavor = os.split("+", 1)
        flavor = " ".join(i.capitalize() for i in flavor.split("-"))
        return mark_safe("%s (<em>%s</em>)" % (flavor, t))
    except ValueError:
        return mark_safe(_("<em>Unknown or invalid OS</em>"))


@register.filter
def mult(value, arg):
    # pinched from http://code.djangoproject.com/ticket/361
    return int(value) * int(arg)


register.filter("hv", stringfilter(hv_prettify))


@register.filter
def hvs(l):
    return [hv_prettify(i) for i in l]
Example #33
0
@register.filter
@stringfilter
def abbreviate_fqdn(value):
    return value.split(".")[0]


@register.filter
@stringfilter
def render_os(os):
    try:
        t, flavor = os.split("+", 1)
        flavor = " ".join(i.capitalize() for i in flavor.split("-"))
        return mark_safe("%s (<em>%s</em>)" % (flavor, t))
    except ValueError:
        return mark_safe(_("<em>Unknown or invalid OS</em>"))


@register.filter
def mult(value, arg):
    # pinched from http://code.djangoproject.com/ticket/361
    return int(value) * int(arg)


register.filter("hv", stringfilter(hv_prettify))


@register.filter
def hvs(l):
    return [hv_prettify(i) for i in l]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()


def iso_flag(iso, flag_path=u""):
    """
	Returns a full path to the ISO 3166-1 alpha-2 country code flag image.
	
	Example usage::
		
		{{ user_profile.country.iso|iso_flag }}
		
		{{ user_profile.country.iso|iso_flag:"appmedia/flags/%s.png" }}
	
	"""
    from countries.utils.isoflag import iso_flag

    return iso_flag(iso, flag_path)


iso_flag = stringfilter(iso_flag)

# Syntax: register.filter(name of filter, callback)
register.filter("iso_flag", iso_flag)
Example #35
0
from django import template
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter

register = template.Library()


@register.filter(is_safe=True)
def add_break(text):
    """
    Add an extra break (\n) between paragraphs.
    Useful for additional formatting after markdown/striptags
    """
    return mark_safe(text.replace('\n', '\n\n'))


add_break = stringfilter(add_break)
register.filter(add_break)
Example #36
0
    Replaces True with true and False with false, so I can print JavaScript.
    """
    if bool == True:
        return 'true'
    elif bool == False:
        return 'false'
    elif bool == None:
        return 'null'
    else:
        return ''


truthjs.is_safe = True
register.filter(truthjs)


def trim_p(html, count=2):
    """
    Trims a html block to the requested number of paragraphs
    """
    grafs = [i.end() for i in re.finditer("</p>", html)]
    if len(grafs) < count:
        return html
    else:
        end = grafs[count - 1]
        return html[:end]


trim_p = stringfilter(trim_p)
register.filter(trim_p)
Example #37
0
    pee = re.sub(r'<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)', lambda m: m.group(1) if m.group(1) else "", pee)
    if (pee.find('<pre') != -1):
        def clean_pre(m):
            if m.group(1) and m.group(2):
                text = m.group(2)
                text = text.replace('<br />', '')
                text = text.replace('<p>', "\n")
                text = text.replace('</p>', '')
                text = m.group(1)+escape(text)+"</pre>"
            else:
                text = m.group(0)
                text = text.replace('<br />', '')
                text = text.replace('<p>', "\n")
                text = text.replace('</p>', '')

            return text
        pee = re.sub('(?is)(<pre[^>]*>)(.*?)</pre>', clean_pre, pee)
    pee = re.sub( r"\n</p>$", '</p>', pee)
    return pee
linebreaks_wp = allow_lazy(linebreaks_wp, unicode)

@register.filter("linebreaks_wp")
@stringfilter
def linebreaks_wp_filter(value, autoescape=None):
    """Straight up port of http://codex.wordpress.org/Function_Reference/wpautop"""
    autoescape = autoescape and not isinstance(value, SafeData)
    return mark_safe(linebreaks_wp(value, autoescape))
linebreaks_wp_filter.is_safe = True
linebreaks_wp_filter.needs_autoescape = True
linebreaks_wp = stringfilter(linebreaks_wp)
Example #38
0
        length = length - 3
        if s[length - 1] == ' ' or s[length] == ' ':
            s = s[:length].strip()
        else:
            words = s[:length].split()
            if len(words) > 1:
                del words[-1]
            s = u' '.join(words)
        s += '...'
    return s
truncate_chars = allow_lazy(truncate_chars, unicode)


def truncatechars(value, arg):
    """
    Truncates a string after a certain number of characters, but respects word
    boundaries.

    Argument: Number of characters to truncate after.
    """
    try:
        length = int(arg)
    except ValueError:
        # Fail silently if the argument is not a valid integer.
        return value
    return truncate_chars(value, length)
truncatechars.is_safe = True
truncatechars = stringfilter(truncatechars)

register.filter(truncatechars)
Example #39
0
    bracket_end = marker_end % marker
    start = rendered.find(bracket_start)
    if not start == -1:
        start = start + len(bracket_start)
        end = rendered.find(bracket_end, start)
        return rendered[start:end]
    return ""


def hyde_thumbnail(url):
    postfix = getattr(settings, 'THUMBNAIL_FILENAME_POSTFIX', '-thumb')
    path, ext = url.rsplit('.', 1)
    return ''.join([path, postfix, '.', ext])


register.filter(stringfilter(hyde_thumbnail))


@register.filter
def value_for_key(dictionary, key):
    if not dictionary:
        return ""
    if not dictionary.has_key(key):
        return ""
    value = dictionary[key]
    return value


@register.filter
def xmldatetime(dt):
    if not dt:
Example #40
0
    string = re.sub("(?<!>)(STD ?)0{0,3}(\d+)",
                    "<a href=\"/doc/std\\2/\">\\1\\2</a>", string)
    string = re.sub("(?<!>)(FYI ?)0{0,3}(\d+)",
                    "<a href=\"/doc/fyi\\2/\">\\1\\2</a>", string)
    string = re.sub("(?<!>)(draft-[-0-9a-zA-Z._+]+)",
                    "<a href=\"/doc/\\1/\">\\1</a>", string)
    string = re.sub("(?<!>)(conflict-review-[-0-9a-zA-Z._+]+)",
                    "<a href=\"/doc/\\1/\">\\1</a>", string)
    string = re.sub("(?<!>)(status-change-[-0-9a-zA-Z._+]+)",
                    "<a href=\"/doc/\\1/\">\\1</a>", string)
    return mark_safe(string)


urlize_ietf_docs.is_safe = True
urlize_ietf_docs.needs_autoescape = True
urlize_ietf_docs = stringfilter(urlize_ietf_docs)


@register.filter(name='dashify')
def dashify(string):
    """
    Replace each character in string with '-', to produce
    an underline effect for plain text files.
    """
    return re.sub('.', '-', string)


@register.filter
def underline(string):
    """Return string with an extra line underneath of dashes, for plain text underlining."""
    return string + "\n" + ("-" * len(string))
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

def trim(value, num):
    return value[:num]
trim = stringfilter(trim)

register.filter(trim)

Example #42
0
import re

from django.template import Library
from django.template.defaultfilters import stringfilter

register = Library()

TAG_RE = re.compile(r'<[^>]+>')

def remove_tags(value):
    """
    Removes all HTML tags from text.
    """
    TAG_RE = re.compile(r'<[^>]+>')
    return TAG_RE.sub('', value)

remove_tags = stringfilter(remove_tags)
register.filter(remove_tags)
Example #43
0
from django import template
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
from django.utils.text import capfirst
from django.utils.translation import ugettext as _, get_language

import atlcore.settings as atlsettings

register = template.Library()

def lang_flag_url(name):
    media_url = atlsettings.MEDIA_URL
    name = name.lower().strip()
    return u'/static/common/flags/languages/%s.gif' %name[:2]
lang_flag_url = stringfilter(lang_flag_url)
register.filter('lang_flag_url', lang_flag_url)
Example #44
0
            'size': size,
            'default': urllib.quote_plus(default),
        }
        url = GRAVATAR_URL % gravatargs
        if 'as' in self.other_kwargs:
            context[self.other_kwargs['as']] = mark_safe(url)
            return ''
        return url


def gravatar(email):
    """
    Takes an e-mail address and returns a gravatar image URL, using properties
    from the django settings file.
    """
    hashed_email = md5_constructor(email).hexdigest()
    return mark_safe(
        GRAVATAR_URL % {
            'hash': hashed_email,
            'rating': GRAVATAR_MAX_RATING,
            'size': GRAVATAR_SIZE,
            'default': urllib.quote_plus(GRAVATAR_DEFAULT_IMG),
        })


gravatar = stringfilter(gravatar)

register = template.Library()
register.filter('gravatar', gravatar)
register.tag('get_gravatar_url', get_gravatar_url)
Example #45
0
def hardwrap(value, line_length):
    val_len = len(value)
    if val_len > line_length:
        offset = val_len % line_length
        # loop backwards so as not to get tripped up by the changing string length
        for i in range(0, val_len, line_length):
            pos = val_len - i - offset
            if pos > 0:
                value = value[:pos] + "\n" + value[pos:]
    return value
hardwrap.is_safe = True


# splits string into a list of strings
@register.filter("split")  
def split(value, sep=None):
    return value.split(sep)
split.is_safe = True


"""
for flowplayer... it seems to take the safe output of escapejs and unescape it,
thus stuffing itself up. this removes problem chars instead of escaping them
"""
@register.filter("makesafe_for_js")  
def makesafe_for_js(value):
    for bad, good in _js_escapes:
        value = value.replace(bad, '')
    return value
makesafe_for_js = stringfilter(makesafe_for_js)
Example #46
0
register = template.Library()



def chlocale(url, locale):
    """
    Changes the URL's locale prefix if the path is not locale-independent.
    Otherwise removes locale prefix.
    """
    _, path = utils.strip_script_prefix(url)
    _, path = utils.strip_path(path)
    return utils.locale_url(path, locale)


chlocale = stringfilter(chlocale)
register.filter('chlocale', chlocale)



def rmlocale(url):
    """Removes the locale prefix from the URL."""
    script_prefix, path = utils.strip_script_prefix(url)
    _, path = utils.strip_path(path)
    return ''.join([script_prefix, path])


rmlocale = stringfilter(rmlocale)
register.filter('rmlocale', rmlocale)

Example #47
0
    unique_words.sort()
    return unique_words


def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha chars and
    converts spaces to hyphens.
    """
    import unicodedata
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
    value = unicode(re.sub('[^\w\s\-\+]', '', value).strip().lower())
    return re.sub('[\-\+\s]+', '-', value)


slugify = stringfilter(slugify)


def send_mails(subscribers, subject, message):
    # we don't use send_mass_mail as we don't want to leak other users email addresses
    for s in subscribers:
        if not s.bookmark:
            now = datetime.datetime.now()
            # only send out things once every 600 seconds
            if now - s.last_updated > datetime.timedelta(0, 600):
                send_mail(subject,
                          message,
                          settings.DEFAULT_FROM_EMAIL, [s.user.email],
                          fail_silently=True)
                s.last_updated = now
                s.save()
Example #48
0
from django import template
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter
from django.utils.text import normalize_newlines

import logging

logger = logging.getLogger(__file__)

register = template.Library()


def remove_newlines(text):
    """
    Removes all newline characters from a block of text.
    """
    # First normalize the newlines using Django's nifty utility
    normalized_text = normalize_newlines(text)
    logger.debug("NORMALIZED TEXT = %s" % normalized_text)
    # Then simply remove the newlines like so.
    return mark_safe(normalized_text.replace('\n', ' '))


remove_newlines.is_safe = True
remove_newlines = stringfilter(remove_newlines)
register.filter(remove_newlines)
Example #49
0
from django.utils.functional import allow_lazy
from django.utils.http import urlquote
from django.utils.html import word_split_re,punctuation_re, simple_email_re
from django.template.defaultfilters import stringfilter

import film20.settings as settings
MAX_URL_LENGTH = getattr(settings, "MAX_URL_LENGTH", None)

register = Library()

def urlimagize(value, autoescape=None):
    """Converts URLs in plain text into clickable links."""
    return mark_safe(do_urlimagize(value, nofollow=True, autoescape=autoescape))
urlimagize.is_safe=True
urlimagize.needs_autoescape = True
urlimagize = stringfilter(urlimagize)

def is_image(url):
    if url.endswith(".png") or url.endswith(".PNG") or url.endswith(".jpg") or url.endswith(".JPG") or url.endswith(".gif") or url.endswith(".GIF"):   
        return True
    else:
        return False

def do_urlimagize(text, trim_url_limit=MAX_URL_LENGTH, nofollow=False, autoescape=False):
    """
    Converts any URLs in text into clickable links.

    Works on http://, https://, www. links and links ending in .org, .net or
    .com. Links can have trailing punctuation (periods, commas, close-parens)
    and leading punctuation (opening parens) and it'll still do the right
    thing.
Example #50
0
        context['page'] = original_page
        bracket_start = marker_start % marker
        bracket_end = marker_end % marker
        start = rendered.find(bracket_start)
        if not start == -1:
            start = start + len(bracket_start)
            end = rendered.find(bracket_end, start)
            return rendered[start:end]
        return ""


def hyde_thumbnail(url):
    postfix = getattr(settings, 'THUMBNAIL_FILENAME_POSTFIX', '-thumb')
    path, ext = url.rsplit('.', 1)
    return ''.join([path, postfix, '.', ext])
register.filter(stringfilter(hyde_thumbnail))


@register.filter
def value_for_key(dictionary, key):
    if not dictionary:
        return ""
    if not dictionary.has_key(key):
        return ""
    value = dictionary[key]
    return value

@register.filter
def xmldatetime(dt):
    if not dt:
        dt = datetime.now()
import django
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()


def truncateletters(value, arg):
    """
    Truncates a string after a certain number of letters

    Argument: Number of letters to truncate after
    """
    from django_extensions.utils.text import truncate_letters
    try:
        length = int(arg)
    except ValueError:  # invalid literal for int()
        return value  # Fail silently
    return truncate_letters(value, length)


if django.get_version() >= "1.4":
    truncateletters = stringfilter(truncateletters)
    register.filter(truncateletters, is_safe=True)
else:
    truncateletters.is_safe = True
    truncateletters = stringfilter(truncateletters)
    register.filter(truncateletters)
Example #52
0
from sentry.models import Group
from sentry.web.helpers import group_is_public
from sentry.utils import to_unicode
from sentry.utils.avatar import get_gravatar_url
from sentry.utils.http import absolute_uri
from sentry.utils.javascript import to_json
from sentry.utils.safe import safe_execute
from sentry.utils.strings import truncatechars
from templatetag_sugar.register import tag
from templatetag_sugar.parser import Name, Variable, Constant, Optional

import datetime

register = template.Library()

truncatechars = register.filter(stringfilter(truncatechars))
truncatechars.is_safe = True

register.filter(to_json)

register.simple_tag(absolute_uri)


@register.filter
def pprint(value, break_after=10):
    """
    break_after is used to define how often a <span> is
    inserted (for soft wrapping).
    """

    value = to_unicode(value)
Example #53
0
            unique_words.append(cleanword)

    if is_sorted:
        unique_words.sort()
    return unique_words

def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha chars and
    converts spaces to hyphens.
    """
    import unicodedata
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
    value = unicode(re.sub('[^\w\s\-\+]', '', value).strip().lower())
    return re.sub('[\-\+\s]+', '-', value)
slugify = stringfilter(slugify)

def slugify_uniquely(model, value):
    """
    Generate a unique slug.
    Based on:
    http://code.djangoproject.com/wiki/SlugifyUniquely
    """
    suffix = 0
    potential = base = slugify(value)
    while True:
        if suffix:
            potential = "-".join([base, str(suffix)])
        if not model.objects.filter(slug=potential).count():
            return potential
        # we hit a conflicting slug, so bump the suffix & try again
Example #54
0
    value = url_to_name(value)

    # normalize unicode
    import unicodedata
    value = unicodedata.normalize('NFKD', unicode(value))

    # remove non-{word,space,keep} characters
    misc_characters = re.compile(('[^\w\s%s]' % keep), re.UNICODE)
    value = re.sub(misc_characters, '', value)
    value = value.strip()
    value = re.sub('[_\s]+', ' ', value)

    return value.lower()


slugify = stringfilter(slugify)


def name_to_url(value):
    """Converts page name to its canonical URL path
    """
    # spaces to underscore
    value = re.sub('[\s]', '_', value.strip())
    # url-encode
    value = quote(value.encode('utf-8'))
    return mark_safe(value)


name_to_url.is_safe = True
name_to_url = stringfilter(name_to_url)
Example #55
0
        downcoded = value

    return downcoded


def slugify2(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.

    It is similar to built-in :filter:`slugify` but it also handles special characters in variety of languages
    so that they are not simply removed but properly transliterated/downcoded.
    """
    try:
        value = unicodedata.normalize('NFC', value)
        value = downcode(value)
        value = unicodedata.normalize('NFD', value).encode('ascii', 'ignore')
        value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
        return safestring.mark_safe(re.sub('[-\s]+', '-', value))
    except:
        if settings.TEMPLATE_DEBUG:
            raise
        else:
            return u''


slugify2.is_safe = True
slugify2 = defaultfilters.stringfilter(slugify2)

register.filter(slugify2)
Example #56
0
from sentry.web.helpers import group_is_public
from sentry.utils import to_unicode
from sentry.utils.avatar import get_gravatar_url
from sentry.utils.http import absolute_uri
from sentry.utils.javascript import to_json
from sentry.utils.safe import safe_execute
from sentry.utils.strings import truncatechars
from templatetag_sugar.register import tag
from templatetag_sugar.parser import Name, Variable, Constant, Optional

SentryVersion = namedtuple('SentryVersion',
                           ['current', 'latest', 'update_available'])

register = template.Library()

truncatechars = register.filter(stringfilter(truncatechars))
truncatechars.is_safe = True

register.filter(to_json)

register.simple_tag(absolute_uri)


@register.filter
def pprint(value, break_after=10):
    """
    break_after is used to define how often a <span> is
    inserted (for soft wrapping).
    """

    value = to_unicode(value)
Example #57
0
def chmod_symbol(mod):
    """
    Transform a os.stat().st_mode octal value to a symbolic string.
    ignores meta infromation like SUID, SGID or the Sticky-Bit.
    e.g. 40755 -> rwxr-xr-x
    """
    try:
        mod = int(mod) # The django template engine gives always a unicode string
    except ValueError:
        return u""
    mod = mod & 0777 # strip "meta info"
    mod_string = u"%o" % mod

    return u''.join(CHMOD_TRANS_DATA[int(num)] for num in mod_string)
chmod_symbol.is_safe = True
chmod_symbol = stringfilter(chmod_symbol)


def get_oct(value):
    """
    Convert an integer number to an octal string.
    """
    try:
        return oct(value)
    except:
        return value
get_oct.is_safe = False


def human_duration(t):
    """