Use jinja filter instead

This commit is contained in:
decentral1se 2021-06-03 16:39:45 +02:00
parent 163175748f
commit db5534f437
No known key found for this signature in database
GPG Key ID: 92DAD76BD9567B8A
2 changed files with 12 additions and 5 deletions

View File

@ -9,6 +9,14 @@ import jinja2
from xbotlib import Bot
def _href_wrap(post):
"""Wrap links in a tags as a Jinja template filter."""
for url in re.findall(r"http\S+", post):
url_with_href = f"<a href='{url}'>{url}</a>"
post.replace(url, url_with_href)
return post
class Logbot(Bot):
help = """Oh dear, logbot is here!
@ -103,7 +111,9 @@ class Logbot(Bot):
def _write_log(self, message):
"""Write new log to the file system."""
template = jinja2.Template(open("template.html").read())
jinja_env = jinja2.Environment()
jinja_env.filters["href_wrap"] = _href_wrap
template = jinja_env.from_string(open("template.html").read())
folder_name = self.db[message.room]["folder"]
if "@" in folder_name: # hacky
folder_name = self._parse_room_name(folder_name)
@ -147,9 +157,6 @@ class Logbot(Bot):
self.db[message.room]["messages"][new_key] = media_post
else:
post = message.content.replace("@add ", "")
for url in re.findall(r"http\S+", post):
url_with_href = f"<a href='{url}'>{url}</a>"
post.replace(url, url_with_href)
self.db[message.room]["messages"][new_key] = post
self.db._dumps()

View File

@ -18,7 +18,7 @@
{% for num in sorted | sort | reverse %}
<div class="post">
<p class="key">{{ num | string }}</p>
<p class="message">{{ db[num|string] }}</p>
<p class="message">{{ db[num|string] | href_wrap }}</p>
</div>
{% endfor %}
</div>