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". --- src/view.rs | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) (limited to 'src/view.rs') 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