summaryrefslogtreecommitdiff
path: root/src/blog/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/blog/mod.rs')
-rw-r--r--src/blog/mod.rs61
1 files changed, 57 insertions, 4 deletions
diff --git a/src/blog/mod.rs b/src/blog/mod.rs
index 5cc5f0b..3ac38eb 100644
--- a/src/blog/mod.rs
+++ b/src/blog/mod.rs
@@ -1,5 +1,13 @@
+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 {
@@ -7,9 +15,54 @@ pub fn r_blog() -> Redirect {
}
#[get("/blog/index")]
-pub fn r_blog_index() -> DynScaffold<'static> {
- Scaffold {
+pub async fn r_blog_index() -> MyResult<DynScaffold<'static>> {
+ // 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! {},
- }
+ 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/<name>")]
+pub async fn r_blog_article(name: &str) -> MyResult<DynScaffold<'static>> {
+ 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)
+ },
+ })
}