From dcdc2051ccb207ea02da265e1d0eea823a82b700 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 13 Apr 2024 06:16:32 +0530 Subject: [PATCH 1/3] fix: assign unique IDs to gist permanent links --- Cargo.lock | 20 ++++++++++++++++++++ Cargo.toml | 1 + src/data.rs | 7 +++++-- src/render_html.rs | 7 ++++--- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca6ffa0..713e2bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -924,6 +924,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "home" version = "0.5.5" @@ -1170,6 +1176,7 @@ dependencies = [ "sailfish", "serde", "serde_json", + "sha256", "sled", "syntect", "url", @@ -1963,6 +1970,19 @@ dependencies = [ "digest", ] +[[package]] +name = "sha256" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18278f6a914fa3070aa316493f7d2ddfb9ac86ebc06fa3b83bffda487e9065b0" +dependencies = [ + "async-trait", + "bytes", + "hex", + "sha2", + "tokio", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" diff --git a/Cargo.toml b/Cargo.toml index 197562a..a4def99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ sled = "0.34.7" syntect = "5.0.0" url = "2.2" actix-web-codegen-const-routes = "0.2.0" +sha256 = "1.5.0" [dependencies.graphql_client] features = ["reqwest"] diff --git a/src/data.rs b/src/data.rs index 84162c7..8d891fb 100644 --- a/src/data.rs +++ b/src/data.rs @@ -21,6 +21,7 @@ use graphql_client::{reqwest::post_graphql, GraphQLQuery}; use reqwest::header::USER_AGENT; use reqwest::Client; use serde::{Deserialize, Serialize}; +use sha256::digest; use sled::{Db, Tree}; use crate::proxy::StringUtils; @@ -287,19 +288,21 @@ impl Data { filepath: &file.file_name, code: &file.content, }; - file.content = highlight.syntax_highlight(); + file.content = highlight.syntax_highlight(&digest(&file.raw_url)); files.push(file); GistContent { files, html_url: gist_url, } } else { + let mut index = 1; gist.files.iter_mut().for_each(|f| { let highlight = render_html::SourcegraphQuery { filepath: &f.file_name, code: &f.content, }; - f.content = highlight.syntax_highlight(); + f.content = highlight.syntax_highlight(&digest(&f.raw_url)); + index += 1; }); gist }; diff --git a/src/render_html.rs b/src/render_html.rs index 487490f..d51b97e 100644 --- a/src/render_html.rs +++ b/src/render_html.rs @@ -38,7 +38,7 @@ pub struct SourcegraphQuery<'a> { } impl<'a> SourcegraphQuery<'a> { - pub fn syntax_highlight(&self) -> String { + pub fn syntax_highlight(&self, gist_name: &str) -> String { // let ss = SYNTAX_SET; let ts = ThemeSet::load_defaults(); @@ -66,7 +66,8 @@ impl<'a> SourcegraphQuery<'a> { if line_num == 0 || line_num == total_lines - 1 { output.push_str(line); } else { - output.push_str(&format!("
" + let line_id = format!("{gist_name}-{num}"); + output.push_str(&format!("
" )); num += 1; } @@ -153,7 +154,7 @@ mod tests { }; let result = query.determine_language(&syntax_set); assert_eq!(result.name, "TeX"); - let _result = query.syntax_highlight(); + let _result = query.syntax_highlight("foo"); } //#[test] From 612001e5706c4e4bbbde5c7f4684d5bebfd82747 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 13 Apr 2024 06:50:33 +0530 Subject: [PATCH 2/3] fix: update default source code config param --- config/default.toml | 2 +- src/proxy.rs | 2 +- templates/index.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/default.toml b/config/default.toml index 3b9b917..70d3797 100644 --- a/config/default.toml +++ b/config/default.toml @@ -1,5 +1,5 @@ debug = true -source_code = "https://github.com/realaravinth/libmedium" +source_code = "https://git.batsense.net/realaravinth/libmedium" #cache = "/var/lib/libmedium" [server] diff --git a/src/proxy.rs b/src/proxy.rs index c7480e8..310039b 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -133,7 +133,7 @@ const INDEX: &str = include_str!("../templates/index.html"); async fn index() -> impl Responder { HttpResponse::Ok() .content_type("text/html; charset=utf-8") - .body(INDEX) + .body(INDEX.replace("SOURCE_CODE_REPLACE", &crate::SETTINGS.source_code)) } #[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.proxy.asset")] diff --git a/templates/index.html b/templates/index.html index 3c58c9d..f63268f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,7 +15,7 @@ href="/@tylerneely/fear-and-loathing-in-lock-free-programming-7158b1cdd50c" >Demo Article - | Source Code + | Source Code

From f231175d97338fef850723ab324b76c914008f42 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 13 Apr 2024 16:31:56 +0530 Subject: [PATCH 3/3] fix: update tests to reflect new permanent linking scheme --- tests/7158b1cdd50c.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/7158b1cdd50c.html b/tests/7158b1cdd50c.html index 1e0f305..17eb63d 100644 --- a/tests/7158b1cdd50c.html +++ b/tests/7158b1cdd50c.html @@ -149,7 +149,7 @@ rel="noreferrer"> Open post in medium.com
}
+ }
}
See gist on GitHub

Even though a spinlock is burning power in a tight loop until it succeeds, it actually is sometimes preferred over a traditional mutex. Traditional mutexes put a thread to sleep when they block, increasing the minimum latency for acquiring them from another thread. Modern mutexes are often a hybrid approach between a spinlock and a traditional mutex. Hybrid mutexes will attempt to acquire the lock quickly in userspace using atomic operations, without giving away their kernel-scheduled appointment on the CPU core. If the gambit didn’t pan out, the hybrid mutex will put the thread to sleep with a blocking syscall.

@@ -177,7 +177,7 @@ rel="noreferrer"> Open post in medium.com
}
+ }
}
See gist on GitHub

Spin Spin Spin