summaryrefslogtreecommitdiff
path: root/src/blog
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-02-13 21:02:09 +0100
committermetamuffin <metamuffin@disroot.org>2023-02-13 21:02:09 +0100
commit9cefa70c3594445c3af6428be982b8b5b5883a42 (patch)
treecebfeb0026348d83666674aaece3ae22b13ba5dc /src/blog
parentad483c540b24b4adc1cdab5bdf62772ba19b615a (diff)
downloadmetamuffin-website-9cefa70c3594445c3af6428be982b8b5b5883a42.tar
metamuffin-website-9cefa70c3594445c3af6428be982b8b5b5883a42.tar.bz2
metamuffin-website-9cefa70c3594445c3af6428be982b8b5b5883a42.tar.zst
include blog (2)
Diffstat (limited to 'src/blog')
-rw-r--r--src/blog/helper.rs71
-rw-r--r--src/blog/mod.rs61
2 files changed, 128 insertions, 4 deletions
diff --git a/src/blog/helper.rs b/src/blog/helper.rs
new file mode 100644
index 0000000..6dea529
--- /dev/null
+++ b/src/blog/helper.rs
@@ -0,0 +1,71 @@
+use anyhow::{anyhow, Context};
+use futures::future::join_all;
+use std::{
+ path::{Path, PathBuf},
+ process::Stdio,
+};
+use tokio::{
+ fs::File,
+ io::{AsyncBufReadExt, BufReader},
+ process::Command,
+};
+
+pub struct ArticleMeta {
+ pub title: String,
+ pub canonical_name: String,
+ pub date: iso8601::Date,
+}
+
+pub async fn article_metadata(path: PathBuf) -> anyhow::Result<ArticleMeta> {
+ let f = File::open(&path).await.context("article not found")?;
+ let mut f = BufReader::new(f);
+ let mut buf = String::new();
+ f.read_line(&mut buf).await.context("cant read the file")?; // assume the 1st line has the title
+ Ok(ArticleMeta {
+ title: String::from(buf[2..].trim()),
+ canonical_name: path
+ .file_stem()
+ .ok_or(anyhow!("no file stem"))?
+ .to_str()
+ .ok_or(anyhow!("this file's name is broken"))?
+ .to_string(),
+ date: iso8601::date(
+ &path
+ .file_name()
+ .ok_or(anyhow!("file has no name"))?
+ .to_str()
+ .ok_or(anyhow!("this file's name is broken"))?[0..10],
+ )
+ .map_err(|e| anyhow!("file date wrong: {e}"))?,
+ })
+}
+
+pub async fn get_articles(blog_root: &Path) -> anyhow::Result<Vec<ArticleMeta>> {
+ let mut a = join_all(
+ std::fs::read_dir(blog_root)?
+ .map(|e| e.unwrap())
+ .map(|e| article_metadata(e.path())),
+ )
+ .await
+ .into_iter()
+ .collect::<anyhow::Result<Vec<_>>>()?;
+ a.sort_by_cached_key(|e| -match e.date {
+ iso8601::Date::YMD { year, month, day } => day as i32 + month as i32 * 100 + year * 10000,
+ _ => unreachable!(),
+ });
+ Ok(a)
+}
+
+// TODO use this somewhere
+pub async fn file_history(filename: &str) -> String {
+ String::from_utf8(
+ Command::new("/usr/bin/git")
+ .args(&["log", "--follow", "--pretty=tformat:%as %h %s", filename])
+ .stdout(Stdio::piped())
+ .output()
+ .await
+ .unwrap()
+ .stdout,
+ )
+ .unwrap()
+}
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)
+ },
+ })
}