From 81affd4017870bd3f24a753bd4b937d86bd8da76 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:06 +0200 Subject: Add macro that checks whether links are working or not --- Cargo.lock | 105 +++++++++++++++++++++++++++++++++ Cargo.toml | 4 +- src/macro_processor/deadlinks.rs | 45 ++++++++++++++ src/macro_processor/macro_processor.rs | 4 ++ src/macro_processor/mod.rs | 2 + 5 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/macro_processor/deadlinks.rs diff --git a/Cargo.lock b/Cargo.lock index 9a8bb87..cd22cd6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,6 +64,17 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "iana-time-zone" version = "0.1.60" @@ -117,6 +128,19 @@ dependencies = [ "unicode-id", ] +[[package]] +name = "minreq" +version = "2.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "763d142cdff44aaadd9268bebddb156ef6c65a0e13486bb81673cf2d8739f9b0" +dependencies = [ + "log", + "once_cell", + "rustls", + "rustls-webpki", + "webpki-roots", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -150,6 +174,53 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys", +] + +[[package]] +name = "rustls" +version = "0.21.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" +dependencies = [ + "log", + "ring", + "rustls-webpki", + "sct", +] + +[[package]] +name = "rustls-webpki" +version = "0.101.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "sct" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "shlex" version = "1.3.0" @@ -162,8 +233,15 @@ version = "0.1.0" dependencies = [ "chrono", "markdown", + "minreq", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + [[package]] name = "syn" version = "2.0.77" @@ -187,6 +265,18 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.93" @@ -242,6 +332,12 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" +[[package]] +name = "webpki-roots" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" + [[package]] name = "windows-core" version = "0.52.0" @@ -251,6 +347,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index 30b051c..d0aa9b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,13 +5,15 @@ edition = "2021" default-run = "skaldpress" [features] -default = ["time", "markdown"] +default = ["time", "markdown", "deadlinks"] time = ["dep:chrono"] markdown = ["dep:markdown"] +deadlinks = ["dep:minreq"] [dependencies] markdown = { version = "1.0.0-alpha.20", optional = true } chrono = { version = "0.4.38", optional = true } +minreq = { version = "2.12.0", optional = true, features = ["https"] } [[bin]] name = "skaldpress" diff --git a/src/macro_processor/deadlinks.rs b/src/macro_processor/deadlinks.rs new file mode 100644 index 0000000..061f729 --- /dev/null +++ b/src/macro_processor/deadlinks.rs @@ -0,0 +1,45 @@ +use crate::macro_processor::error::SMPError; +use crate::macro_processor::macro_processor::MacroProcessorWarning; +use crate::macro_processor::MacroProcessor; + +/// Warn on dead link +pub fn smp_builtin_wodl( + smp: &mut MacroProcessor, + macro_name: &str, + args: &mut [String], +) -> Result { + if args.len() != 1 { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Wrong number of arguments, expected 1"), + )); + return Ok(macro_name.to_string()); + } + + let response = match minreq::get(&args[0]).send() { + Ok(s) => s, + Err(e) => { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Dead link {:#?} ({:#?})!", args[0], e), + )); + return Ok(args[0].to_string()); + } + }; + if response.status_code != 200 || response.reason_phrase != "OK" { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!( + "Dead link {:#?} ({} {})!", + args[0], response.status_code, response.reason_phrase + ), + )); + } + Ok(args[0].to_string()) +} diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index 612d2c5..a9585bb 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -3,6 +3,8 @@ use std::collections::HashMap; use std::fs; use std::process::Command; +#[cfg(feature = "deadlinks")] +use crate::macro_processor::deadlinks::smp_builtin_wodl; // print only with debug_assertions macro_rules! dprint { ($($x:tt)*) => { @@ -672,6 +674,8 @@ impl MacroProcessor { String::from("html_from_markdown"), MacroType::Function(smp_builtin_html_from_markdown), ); + #[cfg(feature = "deadlinks")] + self.define_macro(String::from("wodl"), MacroType::Function(smp_builtin_wodl)); #[cfg(feature = "webring")] self.define_macro( String::from("webring_rss"), diff --git a/src/macro_processor/mod.rs b/src/macro_processor/mod.rs index ffc2144..f586e0c 100644 --- a/src/macro_processor/mod.rs +++ b/src/macro_processor/mod.rs @@ -1,3 +1,5 @@ pub mod error; pub mod macro_processor; pub use macro_processor::MacroProcessor; +#[cfg(feature = "deadlinks")] +pub mod deadlinks; -- cgit v1.2.3