Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/swh_templatetags.py
Show First 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | def urlize_links_and_mails(text): | ||||
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: | try: | ||||
if 'href="' not in text: | if 'href="' not in text: | ||||
text = re.sub(r'(/api/[^"<]*|/browse/[^"<]*|http.*$)', | text = re.sub(r'(http.*)', r'<a href="\1">\1</a>', text) | ||||
r'<a href="\1">\1</a>', | |||||
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 as exc: | except Exception as exc: | ||||
sentry_sdk.capture_exception(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. | ||||
Args | Args | ||||
text: Text whose content contains Link header value | text: Text whose content contains Link header value | ||||
Returns: | Returns: | ||||
The text transformed with html link if any link is found. | The text transformed with html link if any link is found. | ||||
The text as is otherwise. | The text as is otherwise. | ||||
""" | """ | ||||
links = text.split(',') | links = text.split(',') | ||||
ret = '' | ret = '' | ||||
for i, link in enumerate(links): | for i, link in enumerate(links): | ||||
ret += re.sub(r'<(/api/.*|/browse/.*)>', r'<<a href="\1">\1</a>>', | ret += re.sub(r'<(http.*)>', r'<<a href="\1">\1</a>>', link) | ||||
link) | |||||
# add one link per line and align them | # add one link per line and align them | ||||
if i != len(links) - 1: | if i != len(links) - 1: | ||||
ret += '\n ' | ret += '\n ' | ||||
return ret | return ret | ||||
@register.filter | @register.filter | ||||
def jsonify(obj): | def jsonify(obj): | ||||
▲ Show 20 Lines • Show All 81 Lines • Show Last 20 Lines |