From 6299a9e1fb8efbc714925a45675ea0b3d411bbbc Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Sat, 8 Oct 2022 11:45:37 +0200 Subject: [PATCH] Remove redundant whitespace from rendered comments Doesn't fix the perf issues but at least it reduces size quite a bit --- db/sqlite.py | 9 ----- main.py | 5 +++ templates/comment.html | 92 ++++++++++++++++++++---------------------- templates/thread.html | 16 ++++---- 4 files changed, 57 insertions(+), 65 deletions(-) diff --git a/db/sqlite.py b/db/sqlite.py index 1617946..1d1561b 100644 --- a/db/sqlite.py +++ b/db/sqlite.py @@ -77,15 +77,6 @@ class DB: (limit,) ) - def get_comments(self, thread): - return self._db().execute(''' - select text - from comments - where thread_id = ? - ''', - (thread,) - ) - def get_comment(self, comment_id): return self._db().execute(''' select title, c.text diff --git a/main.py b/main.py index d79a74b..8a5453a 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,8 @@ NAME = 'Agrepy' # TODO config file app.config['SECRET_KEY'] = 'totally random' +app.jinja_env.trim_blocks = True +app.jinja_env.lstrip_blocks = True @app.route('/') def index(): @@ -212,6 +214,7 @@ class Comment: self.parent_id = parent_id def create_comment_tree(comments): + start = time.time(); # Collect comments first, then build the tree in case we encounter a child before a parent comment_map = { 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: sort_time(c.children) sort_time(root) + if __debug__: + print('building tree with', len(comment_map), 'comments took', time.time() - start, 'seconds') return root diff --git a/templates/comment.html b/templates/comment.html index 2ece02d..8b64716 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -1,62 +1,58 @@ -{% macro author(name, ctime, mtime) %} +{%- macro author(name, ctime, mtime) -%} {{ name }} - {{ format_since(ctime) }}{% if ctime != mtime %} (last modified {{ format_since(mtime) }}){% endif %} -{% endmacro %} +{%- endmacro -%} -{% macro comment_author(comment, thread_id) %} -

- - {{ author(comment.author, comment.create_time, comment.modify_time) }} - {# Suffixing a # prevents unnecessary reloads #} - thread - {% if comment.parent_id is not none %} - parent - {% endif %} - {% if comment.author_id == session.get('user_id') %} - {% endif %} - {% if comment.author_id == session.get('user_id') %} - delete - {% endif %} - -

-{% endmacro %} +{%- macro comment_author(comment, thread_id) -%} +

+ {{- author(comment.author, comment.create_time, comment.modify_time) -}} + {# Suffixing a # prevents unnecessary reloads #} + thread + {%- if comment.parent_id is not none -%} + parent + {%- endif -%} + {%- if comment.author_id == session.get('user_id') -%} + {%- endif -%} + {%- if comment.author_id == session.get('user_id') -%} + delete + {%- endif -%} +

+{%- endmacro -%} -{% macro thread_author(author_id, name, ctime, mtime) %} -

- - {{ author(name, ctime, mtime) }} - {% if author_id == session.get('user_id') %} - {% endif %} - {% if author_id == session.get('user_id') %} - delete - {% endif %} - -

-{% endmacro %} +{%- macro thread_author(author_id, name, ctime, mtime) -%} +

+ {{- author(name, ctime, mtime) -}} + {%- if author_id == session.get('user_id') -%} + {%- endif -%} + {%- if author_id == session.get('user_id') -%} + delete + {%- endif -%} +

+{%- endmacro -%} -{% macro render_comment_pre(comment, thread_id) %} +{%- macro render_comment_pre(comment, thread_id) -%}
- {{ comment_author(comment, thread_id) }} -

{{ minimd(comment.text) | safe }}

-{% endmacro %} + {{- comment_author(comment, thread_id) -}} +

{{- minimd(comment.text) | safe -}}

+{%- endmacro -%} -{% macro render_comment_post(comment, thread_id) %} - {% for c in comment.children %} - {{ render_comment(c, thread_id) }} - {% endfor %} +{%- macro render_comment_post(comment, thread_id) -%} + {%- for c in comment.children -%} + {{- render_comment(c, thread_id) -}} + {%- endfor -%}
-{% endmacro %} +{%- endmacro -%} -{% macro render_comment(comment, thread_id) %} -{{ render_comment_pre(comment, thread_id) }} +{%- macro render_comment(comment, thread_id) -%} +{{- render_comment_pre(comment, thread_id) -}} reply -{{ render_comment_post(comment, thread_id) }} -{% endmacro %} +{{- render_comment_post(comment, thread_id) -}} +{%- endmacro -%} -{% macro reply() %} -{% if 'user_id' in session %} +{%- macro reply() -%} +{%- if 'user_id' in session -%}

-{% endif %} -{% endmacro %} +{%- endif -%} +{%- endmacro -%} diff --git a/templates/thread.html b/templates/thread.html index 60d5902..0000bae 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -1,13 +1,13 @@ -{% extends 'base.html' %} -{% from 'comment.html' import render_comment, reply, thread_author with context %} +{%- extends 'base.html' %} +{%- from 'comment.html' import render_comment, reply, thread_author with context %} -{% block content %} +{%- block content %} {{ thread_author(author_id, author, create_time, modify_time) }}

{{ minimd(text) | safe }}

-{{ reply() }} +{{- reply() }} -{% for c in comments %} -{{ render_comment(c, thread_id) }} -{% endfor %} -{% endblock %} +{%- for c in comments %} +{{- render_comment(c, thread_id) }} +{%- endfor %} +{%- endblock %}