diff options
author | Cara Salter <cara@devcara.com> | 2022-02-02 02:23:50 -0500 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-02-02 02:23:50 -0500 |
commit | 8b2d12e799d01059843a8eeb1eb8ff3899d2d274 (patch) | |
tree | f0fe36dd5e225e42253997e0faa233317ea0117d /src | |
parent | 22ee6a2efacb608647edc6f834b6949c0c73d199 (diff) | |
download | site-8b2d12e799d01059843a8eeb1eb8ff3899d2d274.tar.gz site-8b2d12e799d01059843a8eeb1eb8ff3899d2d274.zip |
internal: Make footnotes work
Apparently, the footnotes and superscript extensions conflicted with
each other.
Diffstat (limited to 'src')
-rw-r--r-- | src/blog/mod.rs | 22 | ||||
-rw-r--r-- | src/blog/post.rs | 49 | ||||
-rw-r--r-- | src/build.rs | 2 | ||||
-rw-r--r-- | src/internal/markdown.rs | 33 | ||||
-rw-r--r-- | src/internal/mod.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 27 | ||||
-rw-r--r-- | src/misc/mod.rs | 7 |
7 files changed, 76 insertions, 70 deletions
diff --git a/src/blog/mod.rs b/src/blog/mod.rs index c227f22..811de15 100644 --- a/src/blog/mod.rs +++ b/src/blog/mod.rs @@ -1,21 +1,20 @@ pub mod post; pub mod handlers { -use std::sync::Arc; -use color_eyre::eyre::eyre; + use color_eyre::eyre::eyre; + use std::sync::Arc; -use warp::{Reply, Rejection}; -use warp::http::Response; -use crate::templates::{self, Html, RenderRucte}; + use crate::templates::{self, Html, RenderRucte}; + use warp::http::Response; + use warp::{Rejection, Reply}; -use crate::internal::SiteState; + use crate::internal::SiteState; -use super::post::Post; + use super::post::Post; pub async fn list(state: Arc<SiteState>) -> Result<impl Reply, Rejection> { let state = state.clone(); - Response::builder() - .html(|o| templates::bloglist_html(o, state.blog.clone())) + Response::builder().html(|o| templates::bloglist_html(o, state.blog.clone())) } pub async fn post(name: String, state: Arc<SiteState>) -> Result<impl Reply, Rejection> { @@ -31,9 +30,8 @@ use super::post::Post; match want { Some(post) => { let body = Html(post.body_html.clone()); - Response::builder() - .html(|o| templates::post_html(o, post, body)) - }, + Response::builder().html(|o| templates::post_html(o, post, body)) + } None => panic!("No post found"), } } diff --git a/src/blog/post.rs b/src/blog/post.rs index 3e4a0d6..353c0a1 100644 --- a/src/blog/post.rs +++ b/src/blog/post.rs @@ -1,11 +1,10 @@ -use std::{cmp::Ordering, path::PathBuf}; +use color_eyre::eyre::{eyre, Context, Result}; use glob::glob; -use color_eyre::eyre::{Result, Context, eyre}; -use tokio::{fs}; +use std::{cmp::Ordering, path::PathBuf}; +use tokio::fs; use chrono::prelude::*; - #[derive(Eq, PartialEq, Debug, Clone)] pub struct Post { pub front_matter: frontmatter::Data, @@ -42,20 +41,24 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result<Post> { let link = format!("{}/{}", dir, fname.file_stem().unwrap().to_str().unwrap()); let body_html = crate::internal::markdown::render(&body) .wrap_err_with(|| format!("can't parse markdown for {:?}", fname))?; - let date: DateTime<FixedOffset> = DateTime::<Utc>::from_utc(NaiveDateTime::new(date, NaiveTime::from_hms(0,0,0)), Utc) - .with_timezone(&Utc) - .into(); - let author = &front_matter.clone().author.unwrap_or("Cara Salter".to_string()); + let date: DateTime<FixedOffset> = + DateTime::<Utc>::from_utc(NaiveDateTime::new(date, NaiveTime::from_hms(0, 0, 0)), Utc) + .with_timezone(&Utc) + .into(); + let author = &front_matter + .clone() + .author + .unwrap_or("Cara Salter".to_string()); let draft = &front_matter.clone().draft.unwrap_or(false); Ok(Post { front_matter, body_html, - link, + link, date, author: author.clone(), draft: draft.clone(), - } ) + }) } pub async fn load(dir: &str) -> Result<Vec<Post>> { @@ -78,12 +81,12 @@ pub async fn load(dir: &str) -> Result<Vec<Post>> { result.sort(); result.reverse(); Ok(result) - } + } } mod frontmatter { - use serde::{Serialize, Deserialize}; use color_eyre::eyre::Result; + use serde::{Deserialize, Serialize}; #[derive(Eq, PartialEq, Deserialize, Default, Debug, Serialize, Clone)] pub struct Data { pub title: String, @@ -129,19 +132,17 @@ mod frontmatter { count: 1, end: false, }; - }, - '\n' | '\t' | ' ' => { - - }, + } + '\n' | '\t' | ' ' => {} _ => { panic!("Start of frontmatter not found!"); } }, - ParseState::ReadingMark {count, end } => match ch { + ParseState::ReadingMark { count, end } => match ch { '-' => { *count += 1; if *count == 3 { - state = ParseState::SkipNewLine{ end: *end }; + state = ParseState::SkipNewLine { end: *end }; } } _ => { @@ -159,29 +160,29 @@ mod frontmatter { line_start: true, }; } - }, - _ => { + } + _ => { panic!("Expected newline, got {:?}", ch); } }, ParseState::ReadingFM { buf, line_start } => match ch { '-' if *line_start => { - let mut state_tmp = ParseState::ReadingMark { + let mut state_tmp = ParseState::ReadingMark { count: 1, end: true, }; std::mem::swap(&mut state, &mut state_tmp); - if let ParseState::ReadingFM {buf, ..} = state_tmp { + if let ParseState::ReadingFM { buf, .. } = state_tmp { payload = Some(buf); } else { unreachable!(); } - }, + } ch => { buf.push(ch); *line_start = ch == '\n'; } - } + }, } } diff --git a/src/build.rs b/src/build.rs index 3fcea82..2aa86b1 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,5 +1,5 @@ -use std::process::Command; use ructe::{Ructe, RucteError}; +use std::process::Command; fn main() -> Result<(), Box<dyn std::error::Error>> { let mut ructe = Ructe::from_env()?; diff --git a/src/internal/markdown.rs b/src/internal/markdown.rs index 1538d11..9d0d0b9 100644 --- a/src/internal/markdown.rs +++ b/src/internal/markdown.rs @@ -1,28 +1,33 @@ -use color_eyre::{Result, eyre::Context}; -use comrak::{ComrakOptions, Arena, parse_document, format_html, markdown_to_html}; - +use color_eyre::{eyre::Context, Result}; +use comrak::{format_html, nodes::AstNode, parse_document, Arena, ComrakOptions}; pub fn render(inp: &str) -> Result<String> { let mut options = ComrakOptions::default(); - options.extension.autolink = true; options.extension.table = true; options.extension.description_lists = true; - options.extension.superscript = true; options.extension.strikethrough = true; options.extension.footnotes = true; - options.render.unsafe_ = true; - - info!("{:?}", options.clone()); - info!("{:?}", inp.clone()); + let arena = Arena::new(); + let root = parse_document(&arena, inp, &options); - Ok(markdown_to_html(inp, &options)) -/* let mut html = vec![]; format_html(root, &options, &mut html).unwrap(); - info!("{:?}", String::from_utf8(html.clone())); - String::from_utf8(html).wrap_err("this is somehow not UTF-8") - */ +} + +/** + * Takes in a root node and a function to act on it, then recursively acts on + * all children of that node + */ +fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F) -> Result<()> +where + F: Fn(&'a AstNode<'a>) -> Result<()>, +{ + f(node)?; + for c in node.children() { + iter_nodes(c, f)?; + } + Ok(()) } diff --git a/src/internal/mod.rs b/src/internal/mod.rs index f924c50..2a5fa56 100644 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -1,5 +1,5 @@ -use color_eyre::eyre::Result; use crate::blog::post::Post; +use color_eyre::eyre::Result; pub mod markdown; @@ -10,7 +10,5 @@ pub struct SiteState { pub async fn init() -> Result<SiteState> { let blog = crate::blog::post::load("blog").await?; - Ok(SiteState { - blog, - }) + Ok(SiteState { blog }) } diff --git a/src/main.rs b/src/main.rs index 0726f87..a83c1ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,13 @@ extern crate tracing; use color_eyre::eyre::Result; use std::{net::IpAddr, sync::Arc}; -use warp::{Filter, path}; +use warp::{path, Filter}; use std::str::FromStr; pub mod blog; -pub mod misc; mod internal; +pub mod misc; use internal::SiteState; @@ -19,7 +19,6 @@ async fn main() -> Result<()> { tracing_subscriber::fmt::init(); info!("Starting launch of {}", env!("GIT_SHA")); - // Load .env kankyo::init(); @@ -27,15 +26,19 @@ async fn main() -> Result<()> { let index = warp::get().and(path::end().and_then(misc::handlers::index)); - let blog_index = warp::path!("blog").and(give_site_state(state.clone())).and_then(blog::handlers::list); - - let blog_post = warp::path!("blog" / String).and(give_site_state(state.clone())).and_then(blog::handlers::post); - + let blog_index = warp::path!("blog") + .and(give_site_state(state.clone())) + .and_then(blog::handlers::list); + let blog_post = warp::path!("blog" / String) + .and(give_site_state(state.clone())) + .and_then(blog::handlers::post); - let static_files = warp::path("static") - .and(warp::fs::dir("./statics")); - let site = index.or(blog_index.or(blog_post)).or(static_files).with(warp::log("site")); + let static_files = warp::path("static").and(warp::fs::dir("./statics")); + let site = index + .or(blog_index.or(blog_post)) + .or(static_files) + .with(warp::log("site")); let server = warp::serve(site); @@ -51,7 +54,9 @@ async fn main() -> Result<()> { Ok(()) } -fn give_site_state(sitestate: Arc<SiteState>) -> impl Filter<Extract = (Arc<SiteState>,), Error=std::convert::Infallible> + Clone { +fn give_site_state( + sitestate: Arc<SiteState>, +) -> impl Filter<Extract = (Arc<SiteState>,), Error = std::convert::Infallible> + Clone { warp::any().map(move || sitestate.clone()) } diff --git a/src/misc/mod.rs b/src/misc/mod.rs index 5942b8c..c2992cc 100644 --- a/src/misc/mod.rs +++ b/src/misc/mod.rs @@ -1,10 +1,9 @@ pub mod handlers { - use color_eyre::Result; - use warp::{Reply, Rejection, http::Response}; use crate::templates::{self, Html, RenderRucte}; + use color_eyre::Result; + use warp::{http::Response, Rejection, Reply}; pub async fn index() -> Result<impl Reply, Rejection> { - Response::builder() - .html(|o| templates::index_html(o)) + Response::builder().html(|o| templates::index_html(o)) } } |