Skip to content

Commit

Permalink
Replace URLs to links in comments (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
berk76 authored Oct 5, 2024
1 parent b5b6c6b commit 1d2bb9d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion svjis/articles/templates/article.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h1 class="article-title" id="tbl-desc">{{ obj.header | highlight:search | safe
<h2 class="article-title">{% trans 'Comments' %}:</h2>
{% for comment in obj.comments %}
<div class="article box"><strong>{{ comment.author.first_name }}&nbsp;{{ comment.author.last_name }} {{ comment.created_date | date:"d.m.Y H:i" }}</strong>
<br>{{ comment.body | linebreaks | safe }}
<br>{{ comment.body | replace_url_to_link | linebreaks | safe }}
</div>
{% endfor %}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion svjis/articles/templates/fault.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h1 class="page-title" {% if obj.closed %}style="text-decoration: line-through;
<h2 class="article-title">{% trans 'Comments' %}:</h2>
{% for comment in obj.comments %}
<div class="article box"><strong>{{ comment.author.first_name }}&nbsp;{{ comment.author.last_name }} {{ comment.created_date | date:"d.m.Y H:i" }}</strong>
<br>{{ comment.body | linebreaks | safe }}
<br>{{ comment.body | replace_url_to_link | linebreaks | safe }}
</div>
{% endfor %}
{% endif %}
Expand Down
12 changes: 12 additions & 0 deletions svjis/articles/templatetags/article_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.template.defaultfilters import stringfilter
from django.conf import settings
import markdown as md
import re
from re import IGNORECASE, compile

register = template.Library()
Expand Down Expand Up @@ -35,6 +36,17 @@ def inject_pictures(text, assets):
return mark_safe(text)


@register.filter()
def replace_url_to_link(value):
# Replace url to link
urls = re.compile(r"((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", re.MULTILINE | re.UNICODE)
value = urls.sub(r'<a href="\1" target="_blank">\1</a>', value)
# Replace email to mailto
urls = re.compile(r"([\w\-\.]+@(\w[\w\-]+\.)+[\w\-]+)", re.MULTILINE | re.UNICODE)
value = urls.sub(r'<a href="mailto:\1">\1</a>', value)
return mark_safe(value)


@register.filter()
def yes_no(bool_value):
result = gt("Yes") if bool_value else gt("No")
Expand Down

0 comments on commit 1d2bb9d

Please sign in to comment.