Skip to content

Commit

Permalink
More flexible URL linking in CSV preview
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-uva committed Jun 19, 2024
1 parent f4f8e66 commit 8d4f99b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
22 changes: 22 additions & 0 deletions webtool/lib/template_filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import urllib.parse
import datetime
import markdown
import json
import ural
import uuid
import math
import os
Expand Down Expand Up @@ -117,6 +119,26 @@ def _jinja2_filter_httpquery(data):
except TypeError:
return ""

@app.template_filter("add_ahref")
def _jinja2_filter_add_ahref(content):
"""
Add HTML links to text
Replaces URLs with a clickable link
:param str content: Text to parse
:return str: Parsed text
"""
try:
content = str(content)
except ValueError:
return content

for link in set(ural.urls_from_text(str(content))):
content = content.replace(link, f'<a href="{urllib.parse.quote(link, safe="%/")}" rel="external">{link}</a>')

return content

@app.template_filter('markdown')
def _jinja2_filter_markdown(text):
val = markdown.markdown(text)
Expand Down
6 changes: 1 addition & 5 deletions webtool/templates/preview/csv.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
{% endif %}
{% endif %}
<t{% if outer_loop.index == 1 %}h{% else %}d{% endif %}>
{% if inner_loop.index in ns.links and outer_loop.index != 1 %}
<a href="{{ cell }}" target="_blank">{{ cell }}</a>
{% else %}
{{ cell }}
{% endif %}
{{ cell|e|add_ahref|safe }}
</t{% if outer_loop.index == 1 %}h{% else %}d{% endif %}>
{% endfor %}
</tr>
Expand Down

0 comments on commit 8d4f99b

Please sign in to comment.