diff options
author | Qrius <[email protected]> | 2024-09-26 00:11:06 +0200 |
---|---|---|
committer | Qrius <[email protected]> | 2024-09-26 00:11:06 +0200 |
commit | 81affd4017870bd3f24a753bd4b937d86bd8da76 (patch) | |
tree | 936363e543f1b33b2fb88a7c24a12ea8526931de /src/macro_processor/deadlinks.rs | |
parent | c71bdceab75374edcb0e4de31c7907b5ef55bb4e (diff) | |
download | skaldpress-81affd4017870bd3f24a753bd4b937d86bd8da76.tar.gz skaldpress-81affd4017870bd3f24a753bd4b937d86bd8da76.zip |
Add macro that checks whether links are working or not
Diffstat (limited to 'src/macro_processor/deadlinks.rs')
-rw-r--r-- | src/macro_processor/deadlinks.rs | 45 |
1 files changed, 45 insertions, 0 deletions
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<String, SMPError> { + 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()) +} |