Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/swh_templatetags.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import json | import json | ||||
import re | import re | ||||
import sentry_sdk | |||||
from django import template | from django import template | ||||
from django.core.serializers.json import DjangoJSONEncoder | from django.core.serializers.json import DjangoJSONEncoder | ||||
from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||
from swh.web.common.origin_save import get_savable_visit_types | from swh.web.common.origin_save import get_savable_visit_types | ||||
from swh.web.common.utils import rst_to_html | from swh.web.common.utils import rst_to_html | ||||
register = template.Library() | register = template.Library() | ||||
Show All 16 Lines | Args: | ||||
text: whose content matching links should be transformed into | text: whose content matching links should be transformed into | ||||
contextual API or Browse html links. | contextual API or Browse html links. | ||||
Returns | Returns | ||||
The text transformed if any link is found. | The text transformed if any link is found. | ||||
The text as is otherwise. | The text as is otherwise. | ||||
""" | """ | ||||
try: | |||||
if 'href="' not in text: | if 'href="' not in text: | ||||
text = re.sub(r"(http.*)", r'<a href="\1">\1</a>', text) | text = re.sub(r"(http.*)", r'<a href="\1">\1</a>', text) | ||||
return re.sub(r'([^ <>"]+@[^ <>"]+)', r'<a href="mailto:\1">\1</a>', text) | return re.sub(r'([^ <>"]+@[^ <>"]+)', r'<a href="mailto:\1">\1</a>', text) | ||||
except Exception as exc: | |||||
sentry_sdk.capture_exception(exc) | |||||
return text | return text | ||||
@register.filter | @register.filter | ||||
def urlize_header_links(text): | def urlize_header_links(text): | ||||
"""Utility function for decorating headers links in browsable api. | """Utility function for decorating headers links in browsable api. | ||||
▲ Show 20 Lines • Show All 101 Lines • Show Last 20 Lines |