pogress
This commit is contained in:
parent
8c7128591f
commit
614a7389e8
2 changed files with 42 additions and 14 deletions
5
blogs/day5.md
Normal file
5
blogs/day5.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
title: Crazy Hamburger
|
||||
date: Tue, 12 Mar 2024 16:53:47 GMT
|
||||
---
|
||||
😎 I don't like loosing.
|
||||
|
||||
47
src/main.rs
47
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::<Result<Vec<String>, io::Error>>()?;
|
||||
// parse metadata, then the rest to html
|
||||
let mut metadata: HashMap<String, String> = 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::<Result<Vec<(String, HashMap<String, String>)>, 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()
|
||||
}).collect();
|
||||
print!("{}", channel);
|
||||
},
|
||||
"html" => {
|
||||
print!("{}", posts.iter().enumerate().map(|(i, p)| format!("<div class=\"blogpost\" id=post{i}>{p}</div>")).collect::<Vec<String>>().concat())
|
||||
print!("{}", posts.iter().enumerate().map(|(i, (body, metadata))| format!("<div class=\"blogpost\" id=post{i}>{body}</div>")).collect::<Vec<String>>().concat())
|
||||
},
|
||||
_ => eprintln!("need to specify 'rss' or 'html'"),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue