Skip to content

Commit ba8f937

Browse files
authored
Change the sanitization function to a asyncio.to_thread (#44)
1 parent aae3379 commit ba8f937

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

views/htmx.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from __future__ import annotations
2020

21+
import asyncio
2122
import datetime
2223
import json
2324
from typing import TYPE_CHECKING, Any, cast
@@ -46,26 +47,36 @@ class HTMXView(starlette_plus.View, prefix="htmx"):
4647
def __init__(self, app: Application) -> None:
4748
self.app: Application = app
4849

49-
def highlight_code(self, filename: str, content: str, *, index: int, raw_url: str, annotation: str) -> str:
50-
filename = bleach.clean(filename, attributes=[], tags=[])
51-
filename = "_".join(filename.splitlines())
52-
53-
content = bleach.clean(content.replace("<!", "&lt;&#33;"), attributes=[], tags=[], strip_comments=False)
54-
annotations: str = f'<small class="annotations">❌ {annotation}</small>' if annotation else ""
55-
56-
return f"""
57-
<div id="__paste_a_{index}" class="pasteArea">
58-
<div class="pasteHeader">
59-
<div style="display: flex; gap: 0.5rem; align-items: center;">
60-
<span class="filenameArea">{filename}</span>
61-
<span class="pasteButton" onclick="hideFile(this, {index})">Hide</span>
62-
<span id="__paste_copy_{index}" class="pasteButton" onclick="copyFile({index})">Copy</span>
63-
<a class="pasteButton" href="{raw_url}/{index + 1}">Raw</a>
50+
def highlight_code(self, *, files: list[dict[str, Any]]) -> str:
51+
html: str = ""
52+
53+
for index, file in enumerate(files):
54+
filename = bleach.clean(file["filename"], attributes=[], tags=[])
55+
filename = "_".join(filename.splitlines())
56+
57+
raw_url: str = f'/raw/{file["parent_id"]}'
58+
annotation: str = file["annotation"]
59+
60+
content = bleach.clean(
61+
file["content"].replace("<!", "&lt;&#33;"), attributes=[], tags=[], strip_comments=False
62+
)
63+
annotations: str = f'<small class="annotations">❌ {annotation}</small>' if annotation else ""
64+
65+
html += f"""
66+
<div id="__paste_a_{index}" class="pasteArea">
67+
<div class="pasteHeader">
68+
<div style="display: flex; gap: 0.5rem; align-items: center;">
69+
<span class="filenameArea">{filename}</span>
70+
<span class="pasteButton" onclick="hideFile(this, {index})">Hide</span>
71+
<span id="__paste_copy_{index}" class="pasteButton" onclick="copyFile({index})">Copy</span>
72+
<a class="pasteButton" href="{raw_url}/{index + 1}">Raw</a>
73+
</div>
6474
</div>
65-
</div>
66-
{annotations}
67-
<pre id="__paste_c_{index}" class="fileContent" style="display: flex; flex-grow: 1;"><code>{content}</code></pre>
68-
</div>"""
75+
{annotations}
76+
<pre id="__paste_c_{index}" class="fileContent" style="display: flex; flex-grow: 1;"><code>{content}</code></pre>
77+
</div>"""
78+
79+
return html
6980

7081
def check_discord(self, request: starlette_plus.Request) -> starlette_plus.Response | None:
7182
agent: str = request.headers.get("user-agent", "")
@@ -154,15 +165,7 @@ async def paste(self, request: starlette_plus.Request) -> starlette_plus.Respons
154165
</div>
155166
"""
156167

157-
for i, file in enumerate(files):
158-
html += self.highlight_code(
159-
file["filename"],
160-
file["content"],
161-
index=i,
162-
raw_url=raw_url,
163-
annotation=file["annotation"],
164-
)
165-
168+
html += await asyncio.to_thread(self.highlight_code, files=files)
166169
if htmx_url and password:
167170
return starlette_plus.HTMLResponse(html, headers={"HX-Replace-Url": f"{url}?pastePassword={password}"})
168171

0 commit comments

Comments
 (0)