<commit_before>"""Permalink processing template tags."""


import re

from django.core.exceptions import ObjectDoesNotExist
from django.forms.fields import slug_re

from cms.apps.pages.models import Page
from cms.apps.pages.templatetags import Library
from cms.apps.utils import permalinks


register = Library()


@register.filter
def permalink(obj):
    """Generates a permalink for the given object."""
    return permalinks.create(obj)


RE_ANCHOR = re.compile(ur"""(<a.*?\shref=["'])(.+?)(["'].*?>)""", re.IGNORECASE)


@register.filter
def expand_permalinks(text):
    """
    Expands all the permalinks found in anchor tags in the given HTML text.
    """
    def replacement(match):
        href = match.group(2)
        obj = None
        # Try to match a generic permalink.
        try:
            obj = permalinks.resolve(href)
        except permalinks.PermalinkError:
            pass
        except ObjectDoesNotExist:
            pass
        # Try to match a page permalink.
        if obj is None and slug_re.search(href):
            try:
                obj = Page.objects.get_page(href)
            except Page.DoesNotExist:
                pass
        # If an object was found, substitute it's href.
        if obj is not None:
            try:
                href = obj.get_absolute_url()
            except AttributeError:
                pass
        return u"".join((match.group(1), href, match.group(3)))
    return RE_ANCHOR.sub(replacement, text)
<commit_msg>Using the get_url method in permalink substitution.<commit_after>"""Permalink processing template tags."""


import re

from django.core.exceptions import ObjectDoesNotExist
from django.forms.fields import slug_re

from cms.apps.pages.models import Page
from cms.apps.pages.templatetags import Library
from cms.apps.utils import permalinks


register = Library()


@register.filter
def permalink(obj):
    """Generates a permalink for the given object."""
    return permalinks.create(obj)


RE_ANCHOR = re.compile(ur"""(<a.*?\shref=["'])(.+?)(["'].*?>)""", re.IGNORECASE)


@register.filter
def expand_permalinks(text):
    """
    Expands all the permalinks found in anchor tags in the given HTML text.
    """
    def replacement(match):
        href = match.group(2)
        obj = None
        # Try to match a generic permalink.
        try:
            obj = permalinks.resolve(href)
        except permalinks.PermalinkError:
            # Try to match a page permalink.
            href = Page.objects.get_url(href)
        except ObjectDoesNotExist:
            pass
        else:
            # If an object was found, substitute it's href.
            if obj is not None:
                try:
                    href = obj.get_absolute_url()
                except AttributeError:
                    pass
        return u"".join((match.group(1), href, match.group(3)))
    return RE_ANCHOR.sub(replacement, text)

{'fraction_non_alphanumeric': 0.10329444063143445, 'fraction_numerical': 0.002059025394646534, 'mean_word_length': 2.3505747126436782, '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': '666003', 'n_tokens_mistral': 874, 'n_tokens_neox': 853, 'n_words': 244}