diff --git a/views/htmx.py b/views/htmx.py index 6353638..b3a68a0 100644 --- a/views/htmx.py +++ b/views/htmx.py @@ -18,6 +18,7 @@ from __future__ import annotations +import asyncio import datetime import json from typing import TYPE_CHECKING, Any, cast @@ -46,26 +47,36 @@ class HTMXView(starlette_plus.View, prefix="htmx"): def __init__(self, app: Application) -> None: self.app: Application = app - def highlight_code(self, filename: str, content: str, *, index: int, raw_url: str, annotation: str) -> str: - filename = bleach.clean(filename, attributes=[], tags=[]) - filename = "_".join(filename.splitlines()) - - content = bleach.clean(content.replace("❌ {annotation}' if annotation else "" - - return f""" -
-
-
- {filename} - Hide - Copy - Raw + def highlight_code(self, *, files: list[dict[str, Any]]) -> str: + html: str = "" + + for index, file in enumerate(files): + filename = bleach.clean(file["filename"], attributes=[], tags=[]) + filename = "_".join(filename.splitlines()) + + raw_url: str = f'/raw/{file["parent_id"]}' + annotation: str = file["annotation"] + + content = bleach.clean( + file["content"].replace("❌ {annotation}' if annotation else "" + + html += f""" +
+
+
+ {filename} + Hide + Copy + Raw +
-
- {annotations} -
{content}
-
""" + {annotations} +
{content}
+
""" + + return html def check_discord(self, request: starlette_plus.Request) -> starlette_plus.Response | None: agent: str = request.headers.get("user-agent", "") @@ -154,15 +165,7 @@ async def paste(self, request: starlette_plus.Request) -> starlette_plus.Respons
""" - for i, file in enumerate(files): - html += self.highlight_code( - file["filename"], - file["content"], - index=i, - raw_url=raw_url, - annotation=file["annotation"], - ) - + html += await asyncio.to_thread(self.highlight_code, files=files) if htmx_url and password: return starlette_plus.HTMLResponse(html, headers={"HX-Replace-Url": f"{url}?pastePassword={password}"})