aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/makefile5
-rw-r--r--tools/src/main.rs45
2 files changed, 46 insertions, 4 deletions
diff --git a/tools/makefile b/tools/makefile
index 15b8a45..5e8d26d 100644
--- a/tools/makefile
+++ b/tools/makefile
@@ -4,7 +4,10 @@ TOOL := ../tools/target/debug/blog-tool
SRC_ARTICLES := $(shell find articles -type f)
OUT_ARTICLES := $(SRC_ARTICLES:articles/%.md=out/%.html)
-all: $(OUT_ARTICLES)
+all: $(OUT_ARTICLES) out/index.html
+
+out/index.html: $(TOOL) $(SRC_ARTICLES)
+ $(TOOL) render-index ./articles > $@
out/%.html: articles/%.md $(TOOL)
mkdir -p out
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 {