<commit_before>"""
Misc. filter tags for templates
"""

from flask import url_for

from . import PUBLISHED
from . import app
from . import utils


def date_string(dt, fmt_str):
    """
    Format dt object with given format string

    :param fmt_str: Format string to be used with datetime.datetime.strftime
    :returns: Date formatted as string
    """

    return dt.strftime(fmt_str)


def url_for_article(article, base_url=app.config['DOMAIN'], branch=u'master',
                    **kwargs):
    """
    Get URL for article object

    :param article: Article object
    :param base_url: Base URL i.e domain, etc. to use
    :param branch: Branch
    :param kwargs: Passed directly into flask.url_for
    :returns: URL as string

    Note this filter is directly linked to the views.article_view URL.  These
    must be changed together!

    Also note the branch argument is optional even though it's included in the
    article object.  This is for extra flexibility so callers can generate urls
    for several branches without having to read that branch specifically and
    creating an article object.

    This filter only exists to centralize the ability to create a url for an
    article so we can store the url in a file or render in templates.
    """

    title = utils.slugify(article.title)
    stack = utils.slugify_stack(article.stacks[0])

    url = u'%s%s' % (base_url,
                     url_for(u'article_view', title=title, stack=stack,
                             **kwargs))

    if article.publish_status != PUBLISHED:
        query_str_arg = '&' if '?' in url else '?'
        url = u'%s%sstatus=%s' % (url, query_str_arg, article.publish_status)


    if branch != u'master':
        query_str_arg = '&' if '?' in url else '?'
        url = u'%s%sbranch=%s' % (url, query_str_arg, branch)

    return url


def url_for_user(user, base_url=app.config['DOMAIN']):
    """
    Get URL for user object

    :param user: User object or username
    :param base_url: Base URL i.e domain, etc. to use
    :returns: URL as string

    Note this filter is directly linked to the views.user_profile URL.  These
    must be changed together!

    This filter only exists to centralize the ability to create a url for an
    user so we can store the url in a file or render in templates.
    """

    try:
        username = user.login
    except AttributeError:
        username = user

    return u'%s%s' % (base_url, url_for('user_profile', author_name=username))<commit_msg>Fix bug with URLs involving a branch name that has special characters in it<commit_after>"""
Misc. filter tags for templates
"""

from urllib import urlencode

from flask import url_for

from . import PUBLISHED
from . import app
from . import utils


def date_string(dt, fmt_str):
    """
    Format dt object with given format string

    :param fmt_str: Format string to be used with datetime.datetime.strftime
    :returns: Date formatted as string
    """

    return dt.strftime(fmt_str)


def url_for_article(article, base_url=app.config['DOMAIN'], branch=u'master',
                    **kwargs):
    """
    Get URL for article object

    :param article: Article object
    :param base_url: Base URL i.e domain, etc. to use
    :param branch: Branch
    :param kwargs: Passed directly into flask.url_for
    :returns: URL as string

    Note this filter is directly linked to the views.article_view URL.  These
    must be changed together!

    Also note the branch argument is optional even though it's included in the
    article object.  This is for extra flexibility so callers can generate urls
    for several branches without having to read that branch specifically and
    creating an article object.

    This filter only exists to centralize the ability to create a url for an
    article so we can store the url in a file or render in templates.
    """

    title = utils.slugify(article.title)
    stack = utils.slugify_stack(article.stacks[0])

    url = u'%s%s' % (base_url,
                     url_for(u'article_view', title=title, stack=stack,
                             **kwargs))

    if article.publish_status != PUBLISHED:
        query_str_arg = '&' if '?' in url else '?'
        url = u'%s%sstatus=%s' % (url, query_str_arg, article.publish_status)


    if branch != u'master':
        query_str_arg = '&' if '?' in url else '?'
        # Must encode branch name b/c it could have special characters like '+'
        # for stacks like c++
        url = u'%s%s%s' % (url, query_str_arg, urlencode({'branch': branch}))

    return url


def url_for_user(user, base_url=app.config['DOMAIN']):
    """
    Get URL for user object

    :param user: User object or username
    :param base_url: Base URL i.e domain, etc. to use
    :returns: URL as string

    Note this filter is directly linked to the views.user_profile URL.  These
    must be changed together!

    This filter only exists to centralize the ability to create a url for an
    user so we can store the url in a file or render in templates.
    """

    try:
        username = user.login
    except AttributeError:
        username = user

    return u'%s%s' % (base_url, url_for('user_profile', author_name=username))
{'fraction_non_alphanumeric': 0.08310991957104558, 'fraction_numerical': 0.00038299502106472615, 'mean_word_length': 2.8319882611885547, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4518102', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1483, 'n_words': 602}