diff options
author | metamuffin <metamuffin@disroot.org> | 2022-08-29 16:47:07 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-08-29 16:47:07 +0200 |
commit | ddc681c7793a990a67ae61ec183bb1a1138ea3ef (patch) | |
tree | 596f5b93ef5644023f1108d029d676cb77d44f4f /tools/src | |
parent | 18f53568b43380665686dc9e7785790f9921ce8f (diff) | |
download | metamuffin-blog-ddc681c7793a990a67ae61ec183bb1a1138ea3ef.tar metamuffin-blog-ddc681c7793a990a67ae61ec183bb1a1138ea3ef.tar.bz2 metamuffin-blog-ddc681c7793a990a67ae61ec183bb1a1138ea3ef.tar.zst |
more code
Diffstat (limited to 'tools/src')
-rw-r--r-- | tools/src/main.rs | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/tools/src/main.rs b/tools/src/main.rs index 3f3c065..0dc57c0 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -1,10 +1,11 @@ use std::{ fs::{read_to_string, File}, - io::Write, + io::{BufRead, BufReader, Write}, + path::PathBuf, }; use clap::{Parser, Subcommand}; -use laby::{html, internal::Buffer, raw, Render}; +use laby::{html, internal::Buffer, iter, li, raw, ul, Render}; use markdown::{Block, Span}; #[derive(Parser)] @@ -18,6 +19,7 @@ struct Args { #[derive(Subcommand)] enum ArgAction { RenderArticle { input: String }, + RenderIndex { root: String }, } fn main() { @@ -29,6 +31,11 @@ fn main() { article(md_source).render(&mut out); write_output(&args.output, out.into_string()); } + ArgAction::RenderIndex { root } => { + let mut out = Buffer::new(); + index(root).render(&mut out); + write_output(&args.output, out.into_string()); + } } } @@ -42,7 +49,39 @@ fn write_output(t: &Option<String>, o: String) { } fn scaffold(title: String, body: impl Render) -> impl Render { - html!(head!(title!(title)), body!(body)) + html!( + head!(title!(title)), + body!( + nav!(h2!("metamuffin's blog"), a!(href = "./index.html", "index")), + article!(body) + ) + ) +} + +fn index(root: String) -> impl Render { + scaffold( + "index".to_string(), + ul!(iter!(std::fs::read_dir(root) + .unwrap() + .map(|e| e.unwrap()) + .map(|e| ( + e.file_name().to_str().unwrap().to_string(), + article_title(e.path()) + )) + .map(|(path, title)| li!( + path.as_str()[0..10].to_string(), + ": ", + a!(href = format!("./{}", path.replace(".md", ".html")), title) + )))), + ) +} + +fn article_title(path: PathBuf) -> String { + let f = File::open(path).unwrap(); + let mut f = BufReader::new(f); + let mut buf = String::new(); + f.read_line(&mut buf).unwrap(); // assume the 1st line has the title + String::from(&buf[2..]) } fn article(md_source: String) -> impl Render { |