Browse Source

Use jinja filter instead

master
decentral1se 4 years ago
parent
commit
db5534f437
No known key found for this signature in database GPG Key ID: 92DAD76BD9567B8A
  1. 15
      LogBot/logbot.py
  2. 2
      LogBot/template.html

15
LogBot/logbot.py

@ -9,6 +9,14 @@ import jinja2
from xbotlib import Bot 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): class Logbot(Bot):
help = """Oh dear, logbot is here! help = """Oh dear, logbot is here!
@ -103,7 +111,9 @@ class Logbot(Bot):
def _write_log(self, message): def _write_log(self, message):
"""Write new log to the file system.""" """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"] folder_name = self.db[message.room]["folder"]
if "@" in folder_name: # hacky if "@" in folder_name: # hacky
folder_name = self._parse_room_name(folder_name) folder_name = self._parse_room_name(folder_name)
@ -147,9 +157,6 @@ class Logbot(Bot):
self.db[message.room]["messages"][new_key] = media_post self.db[message.room]["messages"][new_key] = media_post
else: else:
post = message.content.replace("@add ", "") 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[message.room]["messages"][new_key] = post
self.db._dumps() self.db._dumps()

2
LogBot/template.html

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

Loading…
Cancel
Save