Remove redundant whitespace from rendered comments

Doesn't fix the perf issues but at least it reduces size quite a bit
This commit is contained in:
David Hoppenbrouwers
2022-10-08 11:45:37 +02:00
parent 9f74e1a2af
commit 6299a9e1fb
4 changed files with 57 additions and 65 deletions

View File

@@ -77,15 +77,6 @@ class DB:
(limit,) (limit,)
) )
def get_comments(self, thread):
return self._db().execute('''
select text
from comments
where thread_id = ?
''',
(thread,)
)
def get_comment(self, comment_id): def get_comment(self, comment_id):
return self._db().execute(''' return self._db().execute('''
select title, c.text select title, c.text

View File

@@ -11,6 +11,8 @@ NAME = 'Agrepy'
# TODO config file # TODO config file
app.config['SECRET_KEY'] = 'totally random' app.config['SECRET_KEY'] = 'totally random'
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
@app.route('/') @app.route('/')
def index(): def index():
@@ -212,6 +214,7 @@ class Comment:
self.parent_id = parent_id self.parent_id = parent_id
def create_comment_tree(comments): def create_comment_tree(comments):
start = time.time();
# Collect comments first, then build the tree in case we encounter a child before a parent # Collect comments first, then build the tree in case we encounter a child before a parent
comment_map = { comment_map = {
comment_id: Comment(comment_id, author_id, author, text, create_time, modify_time, parent_id) comment_id: Comment(comment_id, author_id, author, text, create_time, modify_time, parent_id)
@@ -232,6 +235,8 @@ def create_comment_tree(comments):
for c in l: for c in l:
sort_time(c.children) sort_time(c.children)
sort_time(root) sort_time(root)
if __debug__:
print('building tree with', len(comment_map), 'comments took', time.time() - start, 'seconds')
return root return root

View File

@@ -1,62 +1,58 @@
{% macro author(name, ctime, mtime) %} {%- macro author(name, ctime, mtime) -%}
<i>{{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}</i> <i>{{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %}</i>
{% endmacro %} {%- endmacro -%}
{% macro comment_author(comment, thread_id) %} {%- macro comment_author(comment, thread_id) -%}
<p> <p><sub>
<sub> {{- author(comment.author, comment.create_time, comment.modify_time) -}}
{{ author(comment.author, comment.create_time, comment.modify_time) }}
{# Suffixing a # prevents unnecessary reloads #} {# Suffixing a # prevents unnecessary reloads #}
<a href="{{ url_for('thread', thread_id = thread_id) }}#"> thread</a> <a href="{{ url_for('thread', thread_id = thread_id) }}#"> thread</a>
{% if comment.parent_id is not none %} {%- if comment.parent_id is not none -%}
<a href="{{ url_for('comment', comment_id = comment.parent_id) }}#"> parent</a> <a href="{{ url_for('comment', comment_id = comment.parent_id) }}#"> parent</a>
{% endif %} {%- endif -%}
{% if comment.author_id == session.get('user_id') %} {%- if comment.author_id == session.get('user_id') -%}
{% endif %} {%- endif -%}
{% if comment.author_id == session.get('user_id') %} {%- if comment.author_id == session.get('user_id') -%}
<a href="{{ url_for('confirm_delete_comment', comment_id = comment.id) }}"> delete</a> <a href="{{ url_for('confirm_delete_comment', comment_id = comment.id) }}"> delete</a>
{% endif %} {%- endif -%}
</sub> </sub></p>
</p> {%- endmacro -%}
{% endmacro %}
{% macro thread_author(author_id, name, ctime, mtime) %} {%- macro thread_author(author_id, name, ctime, mtime) -%}
<p> <p><sub>
<sub> {{- author(name, ctime, mtime) -}}
{{ author(name, ctime, mtime) }} {%- if author_id == session.get('user_id') -%}
{% if author_id == session.get('user_id') %} {%- endif -%}
{% endif %} {%- if author_id == session.get('user_id') -%}
{% if author_id == session.get('user_id') %}
<a href="{{ url_for('confirm_delete_thread', thread_id = thread_id) }}"> delete</a> <a href="{{ url_for('confirm_delete_thread', thread_id = thread_id) }}"> delete</a>
{% endif %} {%- endif -%}
</sub> </sub></p>
</p> {%- endmacro -%}
{% endmacro %}
{% macro render_comment_pre(comment, thread_id) %} {%- macro render_comment_pre(comment, thread_id) -%}
<div class=comment> <div class=comment>
{{ comment_author(comment, thread_id) }} {{- comment_author(comment, thread_id) -}}
<p>{{ minimd(comment.text) | safe }}</p> <p>{{- minimd(comment.text) | safe -}}</p>
{% endmacro %} {%- endmacro -%}
{% macro render_comment_post(comment, thread_id) %} {%- macro render_comment_post(comment, thread_id) -%}
{% for c in comment.children %} {%- for c in comment.children -%}
{{ render_comment(c, thread_id) }} {{- render_comment(c, thread_id) -}}
{% endfor %} {%- endfor -%}
</div> </div>
{% endmacro %} {%- endmacro -%}
{% macro render_comment(comment, thread_id) %} {%- macro render_comment(comment, thread_id) -%}
{{ render_comment_pre(comment, thread_id) }} {{- render_comment_pre(comment, thread_id) -}}
<sup><a href="{{ url_for("comment", comment_id = comment.id) }}">reply</a></sup> <sup><a href="{{ url_for("comment", comment_id = comment.id) }}">reply</a></sup>
{{ render_comment_post(comment, thread_id) }} {{- render_comment_post(comment, thread_id) -}}
{% endmacro %} {%- endmacro -%}
{% macro reply() %} {%- macro reply() -%}
{% if 'user_id' in session %} {%- if 'user_id' in session -%}
<form method="post" action="comment/"> <form method="post" action="comment/">
<p><textarea name="text"></textarea></p> <p><textarea name="text"></textarea></p>
<p><input type="submit" value="Post comment"></p> <p><input type="submit" value="Post comment"></p>
</form> </form>
{% endif %} {%- endif -%}
{% endmacro %} {%- endmacro -%}

View File

@@ -1,13 +1,13 @@
{% extends 'base.html' %} {%- extends 'base.html' %}
{% from 'comment.html' import render_comment, reply, thread_author with context %} {%- from 'comment.html' import render_comment, reply, thread_author with context %}
{% block content %} {%- block content %}
<i>{{ thread_author(author_id, author, create_time, modify_time) }}</i> <i>{{ thread_author(author_id, author, create_time, modify_time) }}</i>
<p>{{ minimd(text) | safe }}</p> <p>{{ minimd(text) | safe }}</p>
{{ reply() }} {{- reply() }}
{% for c in comments %} {%- for c in comments %}
{{ render_comment(c, thread_id) }} {{- render_comment(c, thread_id) }}
{% endfor %} {%- endfor %}
{% endblock %} {%- endblock %}