diff --git a/blogs/day5.md b/blogs/day5.md new file mode 100644 index 0000000..ec300b1 --- /dev/null +++ b/blogs/day5.md @@ -0,0 +1,5 @@ +title: Crazy Hamburger +date: Tue, 12 Mar 2024 16:53:47 GMT +--- +😎 I don't like loosing. + diff --git a/src/main.rs b/src/main.rs index 9ab9fae..9815c5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,32 @@ -use std::io; +use std::{collections::HashMap, io, path::Path}; use markdown::to_html; use rss::{Channel, GuidBuilder, ItemBuilder}; fn main() -> io::Result<()> { let posts = std::env::args().skip(2).map(|f| { - let contents = std::fs::read_to_string(f.clone())?; - Ok(to_html(&contents)) - }).collect::, io::Error>>()?; + let contents = std::fs::read_to_string(f.clone())?; + // parse metadata, then the rest to html + let mut metadata: HashMap = contents.lines().take_while(|l| { + // header ends here + *l != "---" + }).filter_map(|l| { + l.split_once(':').map(|(k,v)| (k.to_string(), v.trim().to_string())) + }).collect(); + + // use filename as guid + if let Some(x) = Path::new(&f).file_name() && let Some(y) = x.to_str() { + metadata.insert("guid".to_string(), y.to_string()); + } + + // I hate this + let body = contents.lines().skip_while(|l| *l != "---").skip(1).fold(Vec::new(), |mut a, l| { + a.push(l); + a.push("\n"); + a + }).concat(); + + Ok((to_html(&body), metadata)) + }).collect::)>, io::Error>>()?; if let Some(mode) = std::env::args().skip(1).next() { match mode.as_str() { @@ -16,22 +36,25 @@ fn main() -> io::Result<()> { channel.link = "https://mdf.farm".to_string(); channel.description = "morbius text wow".to_string(); channel.generator = Some("Rakabaka's tooling".to_string()); - channel.items = posts.iter().enumerate().map(|(i, p)| { - let guid = GuidBuilder::default() - .value(format!("{i}")) - .permalink(false) - .build(); + channel.items = posts.iter().enumerate().map(|(i, (body, metadata))| { + let guid = metadata.get("title").map(|title| + GuidBuilder::default() + .value(title) + .permalink(true) + .build() + ); ItemBuilder::default() - .title(format!("Blog post {i}")) - .description(Some(p.clone())) + .title(metadata.get("title").map(|title| title.clone())) + .description(Some(body.clone())) + .pub_date(metadata.get("pubDate").map(|title| title.clone())) + .link(format!("https://mdf.farm/#guid")) .guid(guid) - .link(format!("https://mdf.farm/#post{i}")) - .build() + .build() }).collect(); print!("{}", channel); }, "html" => { - print!("{}", posts.iter().enumerate().map(|(i, p)| format!("
{p}
")).collect::>().concat()) + print!("{}", posts.iter().enumerate().map(|(i, (body, metadata))| format!("
{body}
")).collect::>().concat()) }, _ => eprintln!("need to specify 'rss' or 'html'"), }