Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add languages by filename #55

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Easily share code and text.
**Setup:**
- Clone
- Copy `config.template.toml` into `config.toml`
- For local testing `[SERVER] > domain` can be set to `http://localhost:PORT` (Default Port `8181`)
- Set Database connection DSN.
- Optionally set URLs to a running Redis Instance.
- ! If you haven't already: Create a Database in `postgres` (Default `mystbin`)
Expand Down
1 change: 0 additions & 1 deletion config.template.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[SERVER]
host = "localhost"
port = 8181
domain = "https://mystb.in"
session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64))
maintenance = false

Expand Down
4 changes: 2 additions & 2 deletions views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ async def security_info(self, request: starlette_plus.Request) -> starlette_plus
status_code=404,
)

delete: str = f"{CONFIG['SERVER']['domain']}/api/security/delete/{token}"
info: str = f"{CONFIG['SERVER']['domain']}/api/security/info/{token}"
delete: str = f"{request.url.scheme}://{request.url.hostname}/api/security/delete/{token}"
info: str = f"{request.url.scheme}://{request.url.hostname}/api/security/info/{token}"
data: dict[str, str] = {
"token": paste.safety,
"delete": delete,
Expand Down
2 changes: 1 addition & 1 deletion views/htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ async def paste_raw(self, request: starlette_plus.Request) -> starlette_plus.Res

htmx_url: str | None = request.headers.get("HX-Current-URL", None)
if identifier == "0" and htmx_url:
identifier = htmx_url.removeprefix(f'{CONFIG["SERVER"]["domain"]}/')
identifier = htmx_url.removeprefix(f"{request.url.scheme}://{request.url.hostname}/")

headers: dict[str, str] = {"HX-Redirect": f"/raw/{identifier}"}
paste = await self.app.database.fetch_paste(identifier, password=password)
Expand Down
1 change: 1 addition & 0 deletions web/password.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<!-- SCRIPTS -->
<script src="/static/scripts/initialTheme.js?v=1"></script>
<script src="/static/scripts/utils.js"></script>
<script src="/static/scripts/themes.js?v=1" defer></script>
<script src="/static/scripts/hidecopy.js?v=1" defer></script>
<script src="/static/scripts/highlightsHTMX.js?v=5"></script>
Expand Down
1 change: 1 addition & 0 deletions web/paste.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<!-- SCRIPTS -->
<script src="/static/scripts/initialTheme.js?v=1"></script>
<script src="/static/scripts/utils.js"></script>
<script src="/static/scripts/themes.js?v=1" defer></script>
<script src="/static/scripts/hidecopy.js?v=1" defer></script>
<script src="/static/scripts/highlights.js?v=5" defer></script>
Expand Down
15 changes: 13 additions & 2 deletions web/static/scripts/highlights.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ let DlCount = 0;

for (let area of HIGHLIGHT_AREAS) {
let code = area.querySelector("pre > code");
let name = area.querySelector(".pasteHeader > div > .filenameArea");

pasteStores.push(code.textContent);

// Highlight Code Block and get Language Details...
let details = hljs.highlightAuto(code.textContent);
let highlightedLang = details.language ? details.language : "plaintext";
let nameLang = getLangByName(name.textContent);
let highlightedLang;
let details;

if (!nameLang) {
details = hljs.highlightAuto(code.textContent);
highlightedLang = details.language || "plaintext";
} else {
details = hljs.highlight(code.textContent, { "language": nameLang })
highlightedLang = nameLang.toLowerCase();
}

code.innerHTML = details.value;

Expand Down
14 changes: 12 additions & 2 deletions web/static/scripts/highlightsHTMX.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ document.addEventListener("htmx:afterRequest", function (evt) {

for (let area of HIGHLIGHT_AREAS) {
let code = area.querySelector("pre > code");
let name = area.querySelector(".pasteHeader > div > .filenameArea");
pasteStores.push(code.textContent);

// Highlight Code Block and get Language Details...
let details = hljs.highlightAuto(code.textContent);
let highlightedLang = details.language ? details.language : "plaintext";
let nameLang = getLangByName(name.textContent);
let highlightedLang;
let details;

if (!nameLang) {
details = hljs.highlightAuto(code.textContent);
highlightedLang = details.language || "plaintext";
} else {
details = hljs.highlight(code.textContent, { "language": nameLang })
highlightedLang = nameLang.toLowerCase();
}

code.innerHTML = details.value;

Expand Down
14 changes: 14 additions & 0 deletions web/static/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function getLangByName(name) {
splat = name.split(".");
if (splat.length <= 1) {
return null
}

ext = splat[splat.length - 1];
lang = hljs.getLanguage(ext);

if (!lang) {
return null
}
return lang.name;
}
Loading