diff --git a/LogBot/logbot.py b/LogBot/logbot.py index ea4b958..8c8df7b 100644 --- a/LogBot/logbot.py +++ b/LogBot/logbot.py @@ -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"{url}" + 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"{url}" - post.replace(url, url_with_href) self.db[message.room]["messages"][new_key] = post self.db._dumps() diff --git a/LogBot/template.html b/LogBot/template.html index f6038d5..52898e1 100644 --- a/LogBot/template.html +++ b/LogBot/template.html @@ -18,7 +18,7 @@ {% for num in sorted | sort | reverse %}

{{ num | string }}

-

{{ db[num|string] }}

+

{{ db[num|string] | href_wrap }}

{% endfor %}