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.
|
||||||
|
|
||||||
51
src/main.rs
51
src/main.rs
|
|
@ -1,12 +1,32 @@
|
||||||
use std::io;
|
use std::{collections::HashMap, io, path::Path};
|
||||||
use markdown::to_html;
|
use markdown::to_html;
|
||||||
use rss::{Channel, GuidBuilder, ItemBuilder};
|
use rss::{Channel, GuidBuilder, ItemBuilder};
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
let posts = std::env::args().skip(2).map(|f| {
|
let posts = std::env::args().skip(2).map(|f| {
|
||||||
let contents = std::fs::read_to_string(f.clone())?;
|
let contents = std::fs::read_to_string(f.clone())?;
|
||||||
Ok(to_html(&contents))
|
// parse metadata, then the rest to html
|
||||||
}).collect::<Result<Vec<String>, io::Error>>()?;
|
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() {
|
if let Some(mode) = std::env::args().skip(1).next() {
|
||||||
match mode.as_str() {
|
match mode.as_str() {
|
||||||
|
|
@ -16,22 +36,25 @@ fn main() -> io::Result<()> {
|
||||||
channel.link = "https://mdf.farm".to_string();
|
channel.link = "https://mdf.farm".to_string();
|
||||||
channel.description = "morbius text wow".to_string();
|
channel.description = "morbius text wow".to_string();
|
||||||
channel.generator = Some("Rakabaka's tooling".to_string());
|
channel.generator = Some("Rakabaka's tooling".to_string());
|
||||||
channel.items = posts.iter().enumerate().map(|(i, p)| {
|
channel.items = posts.iter().enumerate().map(|(i, (body, metadata))| {
|
||||||
let guid = GuidBuilder::default()
|
let guid = metadata.get("title").map(|title|
|
||||||
.value(format!("{i}"))
|
GuidBuilder::default()
|
||||||
.permalink(false)
|
.value(title)
|
||||||
.build();
|
.permalink(true)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
ItemBuilder::default()
|
ItemBuilder::default()
|
||||||
.title(format!("Blog post {i}"))
|
.title(metadata.get("title").map(|title| title.clone()))
|
||||||
.description(Some(p.clone()))
|
.description(Some(body.clone()))
|
||||||
|
.pub_date(metadata.get("pubDate").map(|title| title.clone()))
|
||||||
|
.link(format!("https://mdf.farm/#guid"))
|
||||||
.guid(guid)
|
.guid(guid)
|
||||||
.link(format!("https://mdf.farm/#post{i}"))
|
.build()
|
||||||
.build()
|
|
||||||
}).collect();
|
}).collect();
|
||||||
print!("{}", channel);
|
print!("{}", channel);
|
||||||
},
|
},
|
||||||
"html" => {
|
"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'"),
|
_ => eprintln!("need to specify 'rss' or 'html'"),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue