|
18 | 18 |
|
19 | 19 | from __future__ import annotations
|
20 | 20 |
|
| 21 | +import asyncio |
21 | 22 | import datetime
|
22 | 23 | import json
|
23 | 24 | from typing import TYPE_CHECKING, Any, cast
|
@@ -46,26 +47,36 @@ class HTMXView(starlette_plus.View, prefix="htmx"):
|
46 | 47 | def __init__(self, app: Application) -> None:
|
47 | 48 | self.app: Application = app
|
48 | 49 |
|
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("<!", "<!"), 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("<!", "<!"), 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> |
64 | 74 | </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 |
69 | 80 |
|
70 | 81 | def check_discord(self, request: starlette_plus.Request) -> starlette_plus.Response | None:
|
71 | 82 | agent: str = request.headers.get("user-agent", "")
|
@@ -154,15 +165,7 @@ async def paste(self, request: starlette_plus.Request) -> starlette_plus.Respons
|
154 | 165 | </div>
|
155 | 166 | """
|
156 | 167 |
|
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) |
166 | 169 | if htmx_url and password:
|
167 | 170 | return starlette_plus.HTMLResponse(html, headers={"HX-Replace-Url": f"{url}?pastePassword={password}"})
|
168 | 171 |
|
|
0 commit comments