Improve performance on rendering files by ~30%

This commit is contained in:
Ersei Saggi 2023-12-03 01:35:39 -05:00 committed by Drew DeVault
parent fb1f3a947c
commit 4278f329f8
2 changed files with 34 additions and 7 deletions

View File

@ -100,6 +100,12 @@ def _highlight_file(repo, ref, entry, data, blob_id, commit_id):
else:
return get_highlighted_file("git.sr.ht:git", entry.name, blob_id, data)
def linecounter(count):
out = []
for i in range(1, count + 1):
out.append('<a href="#L{}" id="L{}">{}\n</a>'.format(i, i, i))
return "".join(out)
def render_empty_repo(owner, repo, view):
origin = cfg("git.sr.ht", "origin")
git_user = cfg("git.sr.ht::dispatch", "/usr/bin/gitsrht-keys", "git:git").split(":")[0]
@ -292,6 +298,7 @@ def tree(owner, repo, ref, path):
owner=owner, repo=repo, ref=ref, path=path, entry=entry,
blob=blob, data=data, commit=orig_commit,
highlight_file=_highlight_file,
linecounter=linecounter,
editorconfig=editorconfig,
markdown=md, force_source=force_source, pygit2=pygit2)
tree = git_repo.get(entry.id)

View File

@ -73,19 +73,18 @@ pre {
{{ markdown }}
</div>
{% else %}
{% if not blob.is_binary %}
{% if not blob.is_binary and blob.size < 512000 %}
<div class="col-md-12 code-view">
{% autoescape off %}
<pre class="ruler"><span>{% for i in range(
editorconfig.max_line_length()) %} {% endfor %}</span></pre>
<pre class="lines">{% for line in data.splitlines() %}<a
href="#L{{loop.index}}"
id="L{{loop.index}}"
>{{loop.index}}</a>{% if not loop.last %}
{% endif %}{% endfor %}</pre>
{% set lines = data.splitlines()|length %}
<pre class="lines">{{ linecounter(lines) }}</pre>
{% endautoescape %}
{{ highlight_file(repo, ref, entry,
data, blob.id.hex, commit.id.hex) }}
</div>
{% else %}
{% elif blob.is_binary %}
<div class="col-md-12">
<div style="padding: 1rem">
<p>
@ -106,6 +105,27 @@ pre {
</p>
</div>
</div>
{% else %}
<div class="col-md-12">
<div style="padding: 1rem">
<p>
<span title="{{ blob.size }} bytes">
{{humanize.naturalsize(blob.size,
binary=True).replace("Byte", "byte")}}
</span>
large file not shown.
</p>
<p>
<a href="{{url_for("repo.raw_blob",
owner=repo.owner.canonical_name,
repo=repo.name, ref=ref,
path=path_join(*path))}}"
class="btn btn-primary"
rel="nofollow"
>Download {{icon("caret-right")}}</a>
</p>
</div>
</div>
{% endif %}
{% endif %}
</div>