<commit_before>import cgi
import urllib
import urlparse

from django.conf import settings

import jinja2

from jingo import register, env
from didyoumean import DidYouMean


@register.filter
def paginator(pager):
    return Paginator(pager).render()


@register.filter
def urlparams(url_, hash=None, **query):
    """
    Add a fragment and/or query paramaters to a URL.

    New query params will be appended to exising parameters, except duplicate
    names, which will be replaced.
    """
    url = urlparse.urlparse(url_)
    fragment = hash if hash is not None else url.fragment

    query_dict = dict(cgi.parse_qsl(url.query)) if url.query else {}
    query_dict.update((k, v) for k, v in query.items() if v is not None)

    query_string = urllib.urlencode(query_dict.items())
    new = urlparse.ParseResult(url.scheme, url.netloc, url.path, url.params,
                               query_string, fragment)
    return jinja2.Markup(new.geturl())


class Paginator(object):

    def __init__(self, pager):
        self.pager = pager

        self.max = 10
        self.span = (self.max - 1) / 2

        self.page = pager.number
        self.num_pages = pager.paginator.num_pages
        self.count = pager.paginator.count

        pager.page_range = self.range()
        pager.dotted_upper = self.count not in pager.page_range
        pager.dotted_lower = 1 not in pager.page_range

    def range(self):
        """Return a list of page numbers to show in the paginator."""
        page, total, span = self.page, self.num_pages, self.span
        if total < self.max:
            lower, upper = 0, total
        elif page < span + 1:
            lower, upper = 0, span * 2
        elif page > total - span:
            lower, upper = total - span * 2, total
        else:
            lower, upper = page - span, page + span - 1
        return range(max(lower + 1, 1), min(total, upper) + 1)

    def render(self):
        c = {'pager': self.pager, 'num_pages': self.num_pages,
             'count': self.count}
        t = env.get_template('layout/paginator.html').render(**c)
        return jinja2.Markup(t)


@register.function
def spellcheck(string, locale='en-US'):
    d = DidYouMean(locale, dict_dir=settings.DICT_DIR)
    return not d.check(string)


@register.filter
def suggestions(string, locale='en-US'):
    d = DidYouMean(locale, dict_dir=settings.DICT_DIR)
    words = d.suggest(string)
    newwords = []
    newquery = []
    for w in words:
        newquery.append(jinja2.escape(w.new))
        if w.corrected:
            newwords.append(u'<strong>%s</strong>' % jinja2.escape(w.new))
        else:
            newwords.append(jinja2.escape(w.new))
    return jinja2.Markup(u' '.join(newwords))<commit_msg>passing 6/7, missing multivaluedict support<commit_after>import cgi
import urllib
import urlparse

from django.conf import settings
from django.core.urlresolvers import reverse

import jinja2

from jingo import register, env
from didyoumean import DidYouMean


@register.filter
def paginator(pager):
    return Paginator(pager).render()


@register.filter
def urlparams(url_, hash=None, **query):
    """
    Add a fragment and/or query paramaters to a URL.

    New query params will be appended to exising parameters, except duplicate
    names, which will be replaced.
    """
    url = urlparse.urlparse(url_)
    fragment = hash if hash is not None else url.fragment

    query_dict = dict(cgi.parse_qsl(url.query)) if url.query else {}
    query_dict.update((k, v) for k, v in query.items() if v is not None)

    query_string = urllib.urlencode(query_dict.items())
    new = urlparse.ParseResult(url.scheme, url.netloc, url.path, url.params,
                               query_string, fragment)
    return jinja2.Markup(new.geturl())


class Paginator(object):

    def __init__(self, pager):
        self.pager = pager

        self.max = 10
        self.span = (self.max - 1) / 2

        self.page = pager.number
        self.num_pages = pager.paginator.num_pages
        self.count = pager.paginator.count

        pager.page_range = self.range()
        pager.dotted_upper = self.count not in pager.page_range
        pager.dotted_lower = 1 not in pager.page_range

    def range(self):
        """Return a list of page numbers to show in the paginator."""
        page, total, span = self.page, self.num_pages, self.span
        if total < self.max:
            lower, upper = 0, total
        elif page < span + 1:
            lower, upper = 0, span * 2
        elif page > total - span:
            lower, upper = total - span * 2, total
        else:
            lower, upper = page - span, page + span - 1
        return range(max(lower + 1, 1), min(total, upper) + 1)

    def render(self):
        c = {'pager': self.pager, 'num_pages': self.num_pages,
             'count': self.count}
        t = env.get_template('layout/paginator.html').render(**c)
        return jinja2.Markup(t)


@register.function
def spellcheck(string, locale='en-US'):
    d = DidYouMean(locale, dict_dir=settings.DICT_DIR)
    return not d.check(string)


@register.filter
@jinja2.contextfilter
def suggestions(context, string, locale='en-US'):
    d = DidYouMean(locale, dict_dir=settings.DICT_DIR)
    words = d.suggest(string)

    newwords = []
    newquery = []
    for w in words:
        newquery.append(jinja2.escape(w.new))
        if w.corrected:
            newwords.append(u'<strong>%s</strong>' % jinja2.escape(w.new))
        else:
            newwords.append(jinja2.escape(w.new))

    markup = '<a href="{url}">{text}</a>'

    q=u' '.join(newquery)
    text = u' '.join(newwords)
    query_dict = context['request'].GET.copy()
    query_dict['q'] = q
    if 'page' in query_dict:
        query_dict['page'] = 1

    query_string = urllib.urlencode(query_dict.items())

    url = u'?'.join([reverse('search'), query_string])

    return jinja2.Markup(markup.format(url=jinja2.escape(url), text=text))
{'fraction_non_alphanumeric': 0.11751849361129792, 'fraction_numerical': 0.007901815736381977, 'mean_word_length': 2.6186131386861313, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '5407167', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1891, 'n_words': 500}