Implementing newline to <br>

This commit is contained in:
KnugiHK
2021-07-11 18:17:06 +08:00
parent 27a6ff98b3
commit f91aac1e11
4 changed files with 39 additions and 5 deletions

View File

@@ -8,6 +8,8 @@ import requests
import shutil import shutil
import re import re
import pkgutil import pkgutil
from bleach import clean as sanitize
from markupsafe import Markup
from datetime import datetime from datetime import datetime
from mimetypes import MimeTypes from mimetypes import MimeTypes
try: try:
@@ -19,6 +21,10 @@ else:
support_backup = True support_backup = True
def sanitize_except(html):
return Markup(sanitize(html, tags=["br"]))
def determine_day(last, current): def determine_day(last, current):
last = datetime.fromtimestamp(last).date() last = datetime.fromtimestamp(last).date()
current = datetime.fromtimestamp(current).date() current = datetime.fromtimestamp(current).date()
@@ -193,6 +199,11 @@ def messages(db, data):
msg = "{ Location shared: "f"{content[10], content[11]}"" }" msg = "{ Location shared: "f"{content[10], content[11]}"" }"
else: else:
msg = content[4] msg = content[4]
if msg is not None:
if "\r\n" in msg:
msg = msg.replace("\r\n", "<br>")
if "\n" in msg:
msg = msg.replace("\n", "<br>")
else: else:
if content[5] == 0 and content[6] == 7: if content[5] == 0 and content[6] == 7:
msg = "{Message deleted}" msg = "{Message deleted}"
@@ -201,6 +212,11 @@ def messages(db, data):
msg = "{ Location shared: "f"{content[10], content[11]}"" }" msg = "{ Location shared: "f"{content[10], content[11]}"" }"
else: else:
msg = content[4] msg = content[4]
if msg is not None:
if "\r\n" in msg:
msg = msg.replace("\r\n", "<br>")
if "\n" in msg:
msg = msg.replace("\n", "<br>")
data[content[0]]["messages"][content[1]]["data"] = msg data[content[0]]["messages"][content[1]]["data"] = msg
@@ -208,8 +224,7 @@ def messages(db, data):
if i % 1000 == 0: if i % 1000 == 0:
print(f"Gathering messages...({i}/{total_row_number})", end="\r") print(f"Gathering messages...({i}/{total_row_number})", end="\r")
content = c.fetchone() content = c.fetchone()
print( print(f"Gathering messages...({total_row_number}/{total_row_number})", end="\r")
f"Gathering messages...({total_row_number}/{total_row_number})", end="\r")
def media(db, data, media_folder): def media(db, data, media_folder):
@@ -304,6 +319,7 @@ def create_html(data, output_folder, template=None):
templateLoader = jinja2.FileSystemLoader(searchpath=template_dir) templateLoader = jinja2.FileSystemLoader(searchpath=template_dir)
templateEnv = jinja2.Environment(loader=templateLoader) templateEnv = jinja2.Environment(loader=templateLoader)
templateEnv.globals.update(determine_day=determine_day) templateEnv.globals.update(determine_day=determine_day)
templateEnv.filters['sanitize_except'] = sanitize_except
template = templateEnv.get_template(template_file) template = templateEnv.get_template(template_file)
total_row_number = len(data) total_row_number = len(data)

View File

@@ -7,12 +7,18 @@ import os
import requests import requests
import shutil import shutil
import pkgutil import pkgutil
from bleach import clean as sanitize
from markupsafe import Markup
from datetime import datetime from datetime import datetime
from mimetypes import MimeTypes from mimetypes import MimeTypes
APPLE_TIME = datetime.timestamp(datetime(2001, 1, 1)) APPLE_TIME = datetime.timestamp(datetime(2001, 1, 1))
def sanitize_except(html):
return Markup(sanitize(html, tags=["br"]))
def determine_day(last, current): def determine_day(last, current):
last = datetime.fromtimestamp(last).date() last = datetime.fromtimestamp(last).date()
current = datetime.fromtimestamp(current).date() current = datetime.fromtimestamp(current).date()
@@ -102,11 +108,21 @@ def messages(db, data):
msg = "{Message deleted}" msg = "{Message deleted}"
else: else:
msg = content[4] msg = content[4]
if msg is not None:
if "\r\n" in msg:
msg = msg.replace("\r\n", "<br>")
if "\n" in msg:
msg = msg.replace("\n", "<br>")
else: else:
if content[5] == 14: if content[5] == 14:
msg = "{Message deleted}" msg = "{Message deleted}"
else: else:
msg = content[4] msg = content[4]
if msg is not None:
if "\r\n" in msg:
msg = msg.replace("\r\n", "<br>")
if "\n" in msg:
msg = msg.replace("\n", "<br>")
data[content[0]]["messages"][content[1]]["data"] = msg data[content[0]]["messages"][content[1]]["data"] = msg
i += 1 i += 1
if i % 1000 == 0: if i % 1000 == 0:
@@ -216,6 +232,7 @@ def create_html(data, output_folder, template=None):
templateLoader = jinja2.FileSystemLoader(searchpath=template_dir) templateLoader = jinja2.FileSystemLoader(searchpath=template_dir)
templateEnv = jinja2.Environment(loader=templateLoader) templateEnv = jinja2.Environment(loader=templateLoader)
templateEnv.globals.update(determine_day=determine_day) templateEnv.globals.update(determine_day=determine_day)
templateEnv.filters['sanitize_except'] = sanitize_except
template = templateEnv.get_template(template_file) template = templateEnv.get_template(template_file)
total_row_number = len(data) total_row_number = len(data)

View File

@@ -73,7 +73,7 @@
</div> </div>
{% endif %} {% endif %}
{% if msg.media == false %} {% if msg.media == false %}
{% filter escape %}{{ msg.data or "{This message is not supported yet}" | replace('\n', '<br>') }}{% endfilter %} {{ msg.data or "{This message is not supported yet}" | sanitize_except() }}
{% else %} {% else %}
{% if "image/" in msg.mime %} {% if "image/" in msg.mime %}
<a href="{{ msg.data }}"><img src="{{ msg.data }}" /></a> <a href="{{ msg.data }}"><img src="{{ msg.data }}" /></a>
@@ -121,7 +121,7 @@
</div> </div>
{% endif %} {% endif %}
{% if msg.media == false %} {% if msg.media == false %}
{% filter escape %}{{ msg.data or "{This message is not supported yet}" }}{% endfilter %} {{ msg.data or "{This message is not supported yet}" | sanitize_except() }}
{% else %} {% else %}
{% if "image/" in msg.mime %} {% if "image/" in msg.mime %}
<a href="{{ msg.data }}"><img src="{{ msg.data }}" /></a> <a href="{{ msg.data }}"><img src="{{ msg.data }}" /></a>

View File

@@ -37,7 +37,8 @@ setuptools.setup(
], ],
python_requires='>=3.7', python_requires='>=3.7',
install_requires=[ install_requires=[
'jinja2' 'jinja2',
'bleach'
], ],
extras_require={ extras_require={
'android_backup': ["pycryptodome"] 'android_backup': ["pycryptodome"]