<commit_before>import re

SECTION_RE = re.compile(r"{{\#([^\}]*)}}\s*(.+?)\s*{{/\1}}", re.M | re.S)
TAG_RE = re.compile(r"{{(#|=|!|<|>|\{)?(.+?)\1?}}+")

class Template(object):
    tag_types = {
        None: 'tag',
        '!': 'comment'
    }

    def __init__(self, template, context=None):
        self.template = template
        self.context = context or {}

    def render(self, template=None, context=None):
        """Turns a Mustache template into something wonderful."""
        template = template or self.template
        context = context or self.context

        template = self.render_sections(template, context)
        return self.render_tags(template, context)

    def render_sections(self, template, context):
        """Expands sections."""
        while 1:
            match = SECTION_RE.search(template)
            if match is None:
                break

            section, section_name, inner = match.group(0, 1, 2)

            it = context.get(section_name, None)
            replacer = ''
            if it and not hasattr(it, '__iter__'):
                replacer = inner
            elif it:
                insides = []
                for item in it:
                    insides.append(self.render(inner, item))
                replacer = ''.join(insides)

            template = template.replace(section, replacer)

        return template

    def render_tags(self, template, context):
        """Renders all the tags in a template for a context."""
        while 1:
            match = TAG_RE.search(template)
            if match is None:
                break

            tag, tag_type, tag_name = match.group(0, 1, 2)
            func = 'render_' + self.tag_types[tag_type]

            if hasattr(self, func):
                replacement = getattr(self, func)(tag_name, context)
                template = template.replace(tag, replacement)

        return template

    def render_tag(self, tag_name, context):
        """Given a tag name and context, finds and renders the tag."""
        return context.get(tag_name, '')

    def render_comment(self, tag_name=None, context=None):
        """Rendering a comment always returns nothing."""
        return ''<commit_msg>implement unescape tag, make escape tag actually escape<commit_after>import re
import cgi

SECTION_RE = re.compile(r"{{\#([^\}]*)}}\s*(.+?)\s*{{/\1}}", re.M | re.S)
TAG_RE = re.compile(r"{{(#|=|!|<|>|\{)?(.+?)\1?}}+")

class Template(object):
    tag_types = {
        None: 'tag',
        '!': 'comment',
        '{': 'unescaped'
    }

    def __init__(self, template, context=None):
        self.template = template
        self.context = context or {}

    def render(self, template=None, context=None):
        """Turns a Mustache template into something wonderful."""
        template = template or self.template
        context = context or self.context

        template = self.render_sections(template, context)
        return self.render_tags(template, context)

    def render_sections(self, template, context):
        """Expands sections."""
        while 1:
            match = SECTION_RE.search(template)
            if match is None:
                break

            section, section_name, inner = match.group(0, 1, 2)

            it = context.get(section_name, None)
            replacer = ''
            if it and not hasattr(it, '__iter__'):
                replacer = inner
            elif it:
                insides = []
                for item in it:
                    insides.append(self.render(inner, item))
                replacer = ''.join(insides)

            template = template.replace(section, replacer)

        return template

    def render_tags(self, template, context):
        """Renders all the tags in a template for a context."""
        while 1:
            match = TAG_RE.search(template)
            if match is None:
                break

            tag, tag_type, tag_name = match.group(0, 1, 2)
            func = 'render_' + self.tag_types[tag_type]

            if hasattr(self, func):
                replacement = getattr(self, func)(tag_name, context)
                template = template.replace(tag, replacement)

        return template

    def render_tag(self, tag_name, context):
        """Given a tag name and context, finds and renders the tag."""
        return cgi.escape(context.get(tag_name, ''))

    def render_comment(self, tag_name=None, context=None):
        """Rendering a comment always returns nothing."""
        return ''

    def render_unescaped(self, tag_name=None, context=None):
        """Render a tag without escaping it."""
        return context.get(tag_name, '')
{'fraction_non_alphanumeric': 0.12558836114676936, 'fraction_numerical': 0.004278990158322636, 'mean_word_length': 2.0416395575797006, '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': '6919139', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1285, 'n_words': 360}