From 778bfdc57102816b1234d13ed022447a983942ee Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Thu, 4 Jun 2020 00:01:12 -0400 Subject: Markdown support. This accomplishes two things: 1. The og cards and page title no longer contain half-baked markup. Instead, they show the markdown equivalent, which is generally pretty friendly. In other words, the page title is "Have you checked `resolv.conf`?" and not "Have you checked resolve.conf?" 2. Phrases can now start with terms other than "Have you checked". --- Cargo.lock | 43 +++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/things-to-check.yml | 68 ++++++++++++++++++++++++------------------------- src/view.rs | 50 ++++++++++++++++++++++++++++-------- 4 files changed, 118 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83cfe5e..c2a4945 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -654,6 +654,15 @@ dependencies = [ "byteorder", ] +[[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + [[package]] name = "getrandom" version = "0.1.14" @@ -1086,6 +1095,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "pulldown-cmark" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e142c3b8f49d2200605ee6ba0b1d757310e9e7a72afe78c36ee2ef67300ee00" +dependencies = [ + "bitflags", + "getopts", + "memchr", + "unicase", +] + [[package]] name = "quick-error" version = "1.2.3" @@ -1343,6 +1364,7 @@ dependencies = [ "actix-web", "lazy_static", "maud", + "pulldown-cmark", "quickcheck", "quickcheck_macros", "rand", @@ -1488,6 +1510,15 @@ dependencies = [ "trust-dns-proto", ] +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.4" @@ -1512,6 +1543,12 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" + [[package]] name = "unicode-xid" version = "0.2.0" @@ -1529,6 +1566,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 1002fa2..41870a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] actix-rt = "~1.0" actix-web = "~2.0" +pulldown-cmark = "~0.7" rand = "~0.7" serde = "~1.0" serde_urlencoded = "~0.6" diff --git a/src/things-to-check.yml b/src/things-to-check.yml index 6147971..a352535 100644 --- a/src/things-to-check.yml +++ b/src/things-to-check.yml @@ -3,37 +3,37 @@ # permalink. # # Yes, this is HTML and no, I don't care about injection. -- permissions -- cabling -- for a full disk -- the cache -- for a version conflict -- for a duplex mismatch -- the firewall rules -- resolv.conf -- /etc/hosts -- DNS -- CR/LF -- setuid/setgid bits -- the default gateway -- for IP conflicts -- the logs -- the port number -- for a zonefile dot -- I/O dammit -- the mounts -- the power -- for the wrong whitespace -- if it reloaded into bad config -- IP forwarding -- the trailing slash -- the MAC address -- if the filesystem is out of inodes -- the line length -- if you're on the wrong wifi network -- if the vpn timed out -- if that's the wrong host -- the security groups -- the fucking binlogs -- the documentation -- the manpages +- Have you checked permissions? +- Have you checked cabling? +- Have you checked for a full disk? +- Have you checked the cache? +- Have you checked for a version conflict? +- Have you checked for a duplex mismatch? +- Have you checked the firewall rules? +- Have you checked `resolv.conf`? +- Have you checked `/etc/hosts`? +- Have you checked DNS? +- Have you checked `CR/LF`? +- Have you checked setuid/setgid bits? +- Have you checked the default gateway? +- Have you checked for IP conflicts? +- Have you checked the logs? +- Have you checked the port number? +- Have you checked for a zonefile dot? +- Have you checked I/O dammit? +- Have you checked the mounts? +- Have you checked the power? +- Have you checked for the wrong whitespace? +- Have you checked if it reloaded into bad config? +- Have you checked IP forwarding? +- Have you checked the trailing slash? +- Have you checked the MAC address? +- Have you checked if the filesystem is out of inodes? +- Have you checked the line length? +- Have you checked if you're on the wrong wifi network? +- Have you checked if the vpn timed out? +- Have you checked if that's the wrong host? +- Have you checked the security groups? +- Have you checked the fucking binlogs? +- Have you checked the documentation? +- Have you checked the manpages? diff --git a/src/view.rs b/src/view.rs index 448b27c..3417e0f 100644 --- a/src/view.rs +++ b/src/view.rs @@ -45,6 +45,7 @@ use actix_web::{get, error, web}; use maud::{DOCTYPE, html, Markup, PreEscaped}; +use pulldown_cmark::{Parser, Options, html}; use rand::thread_rng; use rand::seq::SliceRandom; use serde::{Serialize, Deserialize}; @@ -105,12 +106,12 @@ impl From<&usize> for ItemQuery { } } -fn index_view(req: impl Urls, idx: &usize, thing: &String) -> Result { +fn index_view(req: impl Urls, idx: &usize, thing: &Thing) -> Result { Ok(html! { (DOCTYPE) html { head { - title { "Have you checked " (thing) "?" } + title { (thing.markdown) } style { (PreEscaped(" body { @@ -140,11 +141,11 @@ fn index_view(req: impl Urls, idx: &usize, thing: &String) -> Result); +struct Thing { + markdown: String, + html: String, +} + +impl From for Thing { + fn from(markdown: String) -> Self { + let options = Options::empty(); + let parser = Parser::new_ext(&markdown, options); + + let mut html = String::new(); + html::push_html(&mut html, parser); + + Thing{ + markdown, + html, + } + } +} + +#[derive(Clone)] +struct Things(Vec<(usize, Thing)>); + +fn load_things(src: &str) -> serde_yaml::Result { + let raw_things: Vec = serde_yaml::from_str(src)?; + + Ok(Things( + raw_things.into_iter() + .map(Thing::from) + .enumerate() + .collect() + )) +} /// Errors that can arise initializing the service. #[derive(Error, Debug)] @@ -201,14 +234,11 @@ pub enum Error { /// The returned function will configure any actix-web App with the necessary /// state to tell people how to troubleshoot problems. pub fn make_service() -> Result { - let things: Vec = serde_yaml::from_str(THINGS)?; - let things = things.into_iter() - .enumerate() - .collect(); - let things = Things(things); + let things = load_things(THINGS)?; Ok(move |cfg: &mut web::ServiceConfig| { cfg.data(things.clone()) + .data((123u8,)) .service(index); }) } -- cgit v1.2.3