pub mod helper; use self::helper::{article_metadata, get_articles}; use crate::error::MyResult; use crate::layout::{DynScaffold, Scaffold}; use crate::uri; use anyhow::anyhow; use rocket::{get, response::Redirect}; use std::{path::PathBuf, str::FromStr}; use tokio::fs::read_to_string; #[get("/blog")] pub fn r_blog() -> Redirect { Redirect::to(rocket::uri!(r_blog_index())) } #[get("/blog/index")] pub async fn r_blog_index() -> MyResult> { // TODO this is a major performance issue here. requires O(n) syscalls to complete let articles = get_articles(&PathBuf::from_str("./blog/articles").unwrap()).await?; Ok(Scaffold { title: "blog index".to_string(), content: markup::new! { h2 { "The Weblog" } i { "Articles in reverse-chronological order." } ul { @for a in &articles { li { @a.date.to_string() ": " a[href=uri!(r_blog_article(&a.canonical_name))] { @a.title } } } } }, }) } #[get("/blog/")] pub async fn r_blog_article(name: &str) -> MyResult> { let apath = PathBuf::from_str("./blog/articles") .unwrap() .join(PathBuf::new().with_file_name(name).with_extension("md")); let a = article_metadata(apath.clone()).await?; let text = read_to_string(apath).await?; let html = markdown::to_html_with_options( &text, &markdown::Options { parse: markdown::ParseOptions { constructs: markdown::Constructs { math_flow: true, math_text: true, ..Default::default() }, ..Default::default() }, compile: markdown::CompileOptions { ..Default::default() }, }, ) .map_err(|e| anyhow!("the server had trouble compiling markdown: {e}"))?; Ok(Scaffold { title: a.title, content: markup::new! { @markup::raw(&html) }, }) }