<commit_before>"""
jinja2 extensions that add django tags.
"""
from __future__ import unicode_literals

from django.conf import settings
from django.core.urlresolvers import reverse
from django.templatetags.static import static as django_static
from django.utils.encoding import force_text
from django.utils.formats import localize
from django.utils.translation import pgettext, ugettext
from jinja2 import lexer, nodes
from jinja2.ext import Extension


class DjangoCsrf(Extension):
    """
    Implements django's `{% csrf_token %}` tag.
    """
    tags = set(['csrf_token'])

    def parse(self, parser):
        lineno = parser.stream.expect('name:csrf_token').lineno
        call = self.call_method(
            '_csrf_token',
            [nodes.Name('csrf_token', 'load', lineno=lineno)],
            lineno=lineno
        )
        return nodes.Output([nodes.MarkSafe(call)])

    def _csrf_token(self, csrf_token):
        if not csrf_token or csrf_token == 'NOTPROVIDED':
            return ''
        else:
            return '<input type="hidden" name="csrfmiddlewaretoken" value="{}" />' \
                   .format(csrf_token)


class DjangoI18n(Extension):
    """
    Implements django's `{% trans %}` and `{% blocktrans %}` tags.
    It also tries to localize variables (such as dates and numbers) if
    `USE_L10N` is set in django settings.

    `{% trans %}` works as it does in django::

        Simple example: {% trans 'Hello World' %}

        {% trans "I was saved to a variable!" as translated_var %}
        Save to a variable: {{ translated_var }}

        Translation with context:
        {% trans 'Hello World' context 'second hello world example' %}

        Noop translation: {% trans "Please don't translate me!" noop %}


    `{% blocktrans %}` currently doesn't support the `count` argument, but
    everything else works::

        Simple example: {% blocktrans %}Hello World!{% endblocktrans %}

        Variables:
        {% url 'my_view' as my_url %}
        {% blocktrans with my_upper_url=my_url|upper %}
            Normal url: {{ my_url }}
            Upper url: {{ my_upper_url }}
        {% endblocktrans %}

        Trim whitespace:
        {% blocktrans trimmed %}
            Trim those
            pescy newlines.
        {% endblocktrans %}

    You also can use `_`, `gettext` and `pgettext` directly::

        Simple example: {{ _('Hello World') }}
        More verbose: {{ gettext('Hello World') }}
        With context: {{ pgettext('Hello World', 'another example') }}

    """
    tags = set(['trans', 'blocktrans'])

    def __init__(self, environment):
        super(DjangoI18n, self).__init__(environment)
        environment.globals['_'] = ugettext
        environment.globals['gettext'] = ugettext
        environment.globals['pgettext'] = pgettext
        if settings.USE_L10N:
            if environment.finalize is None:
                environment.finalize = localize
            else:
                old_finalize = environment.finalize

                def wrapper(var):<commit_msg>Removed unicode non-breaking space<commit_after>"""
jinja2 extensions that add django tags.
"""
from __future__ import unicode_literals

from django.conf import settings
from django.core.urlresolvers import reverse
from django.templatetags.static import static as django_static
from django.utils.encoding import force_text
from django.utils.formats import localize
from django.utils.translation import pgettext, ugettext
from jinja2 import lexer, nodes
from jinja2.ext import Extension


class DjangoCsrf(Extension):
    """
    Implements django's `{% csrf_token %}` tag.
    """
    tags = set(['csrf_token'])

    def parse(self, parser):
        lineno = parser.stream.expect('name:csrf_token').lineno
        call = self.call_method(
            '_csrf_token',
            [nodes.Name('csrf_token', 'load', lineno=lineno)],
            lineno=lineno
        )
        return nodes.Output([nodes.MarkSafe(call)])

    def _csrf_token(self, csrf_token):
        if not csrf_token or csrf_token == 'NOTPROVIDED':
            return ''
        else:
            return '<input type="hidden" name="csrfmiddlewaretoken" value="{}" />' \
                   .format(csrf_token)


class DjangoI18n(Extension):
    """
    Implements django's `{% trans %}` and `{% blocktrans %}` tags.
    It also tries to localize variables (such as dates and numbers) if
    `USE_L10N` is set in django settings.

    `{% trans %}` works as it does in django::

        Simple example: {% trans 'Hello World' %}

        {% trans "I was saved to a variable!" as translated_var %}
        Save to a variable: {{ translated_var }}

        Translation with context:
        {% trans 'Hello World' context 'second hello world example' %}

        Noop translation: {% trans "Please don't translate me!" noop %}


    `{% blocktrans %}` currently doesn't support the `count` argument, but
    everything else works::

        Simple example: {% blocktrans %}Hello World!{% endblocktrans %}

        Variables:
        {% url 'my_view' as my_url %}
        {% blocktrans with my_upper_url=my_url|upper %}
            Normal url: {{ my_url }}
            Upper url: {{ my_upper_url }}
        {% endblocktrans %}

        Trim whitespace:
        {% blocktrans trimmed %}
            Trim those
            pescy newlines.
        {% endblocktrans %}

    You also can use `_`, `gettext` and `pgettext` directly::

        Simple example: {{ _('Hello World') }}
        More verbose: {{ gettext('Hello World') }}
        With context: {{ pgettext('Hello World', 'another example') }}

    """
    tags = set(['trans', 'blocktrans'])

    def __init__(self, environment):
        super(DjangoI18n, self).__init__(environment)
        environment.globals['_'] = ugettext
        environment.globals['gettext'] = ugettext
        environment.globals['pgettext'] = pgettext
        if settings.USE_L10N:
            if environment.finalize is None:
                environment.finalize = localize
            else:
                old_finalize = environment.finalize

                def wrapper(var):
{'fraction_non_alphanumeric': 0.1121999020088192, 'fraction_numerical': 0.003593009962436714, 'mean_word_length': 2.5666860803727434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4402696', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1683, 'n_words': 496}