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 | ||||
from inspect import cleandoc | |||||
import json | import json | ||||
import re | import re | ||||
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 docutils.core import publish_parts | from docutils.core import publish_parts | ||||
from docutils.writers.html4css1 import Writer, HTMLTranslator | from docutils.writers.html4css1 import Writer, HTMLTranslator | ||||
from inspect import cleandoc | |||||
import sentry_sdk | |||||
from swh.web.common.origin_save import get_savable_visit_types | from swh.web.common.origin_save import get_savable_visit_types | ||||
register = template.Library() | register = template.Library() | ||||
class NoHeaderHTMLTranslator(HTMLTranslator): | class NoHeaderHTMLTranslator(HTMLTranslator): | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | def urlize_links_and_mails(text): | ||||
try: | try: | ||||
if 'href="' not in text: | if 'href="' not in text: | ||||
text = re.sub(r'(/api/[^"<]*|/browse/[^"<]*|http.*$)', | text = re.sub(r'(/api/[^"<]*|/browse/[^"<]*|http.*$)', | ||||
r'<a href="\1">\1</a>', | r'<a href="\1">\1</a>', | ||||
text) | text) | ||||
return re.sub(r'([^ <>"]+@[^ <>"]+)', | return re.sub(r'([^ <>"]+@[^ <>"]+)', | ||||
r'<a href="mailto:\1">\1</a>', | r'<a href="mailto:\1">\1</a>', | ||||
text) | text) | ||||
except Exception: | except Exception as exc: | ||||
pass | 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 102 Lines • Show Last 20 Lines |