<commit_before>from django import template

register = template.Library()


class FragmentNode(template.Node):
    def __init__(self, nodelist, request, identifier, mode='append'):
        self.nodelist = nodelist
        self.request_var = template.Variable(request)
        self.identifier_var = template.Variable(identifier)
        self.mode = mode

    def render(self, context):
        request = self.request_var.resolve(context)
        identifier = self.identifier_var.resolve(context)
        rendered = self.nodelist.render(context)

        if not hasattr(request, '_feincms_fragments'):
            request._feincms_fragments = {}

        old = request._feincms_fragments.get(identifier, u'')

        if self.mode == 'prepend':
            request._feincms_fragments[identifier] = rendered + old
        elif self.mode == 'replace':
            request._feincms_fragments[identifier] = rendered
        else: # append
            request._feincms_fragments[identifier] = old + rendered

        return u''


@register.tag
def fragment(parser, token):
    """
    {% fragment request "title" %} content ... {% endfragment %}

    or

    {% fragment request "title" (prepend|replace|append) %} content ... {% endfragment %}
    """

    nodelist = parser.parse(('endfragment'),)
    parser.delete_first_token()

    return FragmentNode(nodelist, *token.contents.split()[1:])


class GetFragmentNode(template.Node):
    def __init__(self, request, fragment, as_var=None):
        self.request = template.Variable(request)
        self.fragment = template.Variable(fragment)
        self.as_var = as_var

    def render(self, context):
        request = self.request.resolve(context)
        fragment = self.fragment.resolve(context)

        try:
            value = request._feincms_fragments[fragment]
        except (AttributeError, KeyError):
            value = u''

        if self.as_var:
            context[as_var] = value
            return u''
        return value


@register.tag
def get_fragment(parser, token):
    """
    {% get_fragment request "title" %}
    or
    {% get_fragment request "title" as title %}
    """

    fragments = token.contents.split()

    if len(fragments) == 3:
        return GetFragmentNode(fragments[1], fragments[2])
    elif len(fragments) == 5 and fragments[3] == 'as':
        return GetFragmentNode(fragments[1], fragments[2], fragments[4])<commit_msg>Fix typo in rewritten get_fragment node<commit_after>from django import template

register = template.Library()


class FragmentNode(template.Node):
    def __init__(self, nodelist, request, identifier, mode='append'):
        self.nodelist = nodelist
        self.request_var = template.Variable(request)
        self.identifier_var = template.Variable(identifier)
        self.mode = mode

    def render(self, context):
        request = self.request_var.resolve(context)
        identifier = self.identifier_var.resolve(context)
        rendered = self.nodelist.render(context)

        if not hasattr(request, '_feincms_fragments'):
            request._feincms_fragments = {}

        old = request._feincms_fragments.get(identifier, u'')

        if self.mode == 'prepend':
            request._feincms_fragments[identifier] = rendered + old
        elif self.mode == 'replace':
            request._feincms_fragments[identifier] = rendered
        else: # append
            request._feincms_fragments[identifier] = old + rendered

        return u''


@register.tag
def fragment(parser, token):
    """
    {% fragment request "title" %} content ... {% endfragment %}

    or

    {% fragment request "title" (prepend|replace|append) %} content ... {% endfragment %}
    """

    nodelist = parser.parse(('endfragment'),)
    parser.delete_first_token()

    return FragmentNode(nodelist, *token.contents.split()[1:])


class GetFragmentNode(template.Node):
    def __init__(self, request, fragment, as_var=None):
        self.request = template.Variable(request)
        self.fragment = template.Variable(fragment)
        self.as_var = as_var

    def render(self, context):
        request = self.request.resolve(context)
        fragment = self.fragment.resolve(context)

        try:
            value = request._feincms_fragments[fragment]
        except (AttributeError, KeyError):
            value = u''

        if self.as_var:
            context[self.as_var] = value
            return u''
        return value


@register.tag
def get_fragment(parser, token):
    """
    {% get_fragment request "title" %}
    or
    {% get_fragment request "title" as title %}
    """

    fragments = token.contents.split()

    if len(fragments) == 3:
        return GetFragmentNode(fragments[1], fragments[2])
    elif len(fragments) == 5 and fragments[3] == 'as':
        return GetFragmentNode(fragments[1], fragments[2], fragments[4])
{'fraction_non_alphanumeric': 0.11746554206953302, 'fraction_numerical': 0.003702941781526435, 'mean_word_length': 2.8133333333333335, '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': '7572942', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1336, 'n_words': 296}