added feed generation

This commit is contained in:
2019-01-25 23:38:14 +01:00
parent 57f30c4b5f
commit 879580df39
5 changed files with 482 additions and 11 deletions
+49 -10
View File
@@ -8,6 +8,8 @@ use chrono;
use chrono::NaiveDate;
use markdown;
use std::fs::OpenOptions;
use rss::{Channel, ChannelBuilder, Item, ItemBuilder, Guid};
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
#[derive(Debug)]
struct Bread {
@@ -94,28 +96,61 @@ fn main() {
let mut thumbs = String::new();
let mut channel : Channel = ChannelBuilder::default()
.title("Piggo's Bread Gallery")
.link("https://www.ondrovo.com/bread")
.description("Sourdough feed")
.build()
.unwrap();
let mut channel_items = Vec::<Item>::new();
for bread in &breads {
let date = bread.date.format("%Y/%m/%d").to_string();
let detail_file = bread.date.format("%Y-%m-%d.html").to_string();
let (img_path, img_alt) = bread.thumb_photo();
let note = if bread.note.is_empty() { "<i>There's no note about this bread.</i>" } else { &bread.note };
let thumb = thumb_tpl
.replace("{detail_url}", &detail_file)
.replace("{img_src}", &img_path)
.replace("{img_alt}", &img_alt)
.replace("{title}", &date);
let image_path_encoded = utf8_percent_encode(img_path, DEFAULT_ENCODE_SET).to_string();
thumbs.push_str(&thumb);
// bread pic for the thumbnails page
{
let thumb = thumb_tpl
.replace("{detail_url}", &detail_file)
.replace("{img_src}", &image_path_encoded)
.replace("{img_alt}", &img_alt)
.replace("{title}", &date);
thumbs.push_str(&thumb);
}
// add to rss
{
let image_url : String = channel.link().to_string() + "/" + &image_path_encoded;
let link : String = channel.link().to_string() + "/" + &detail_file;
let mut guid = Guid::default();
guid.set_value(link.clone());
guid.set_permalink(true);
channel_items.push(ItemBuilder::default()
.title(date.clone())
.link(link.clone())
.description(note.to_string() + &format!("<img src=\"{}\" alt=\"{}\"><p>Open the link for more...</p>", image_url, img_alt))
.guid(guid)
.build().unwrap());
}
// generate the detail page
{
let detail = detail_tpl
.replace("{title}", &date)
.replace("{note}", if bread.note.is_empty() { "<i>There's no note about this bread.</i>" } else { &bread.note });
.replace("{note}", note);
let mut pics = String::new();
for img in &bread.images {
pics.push_str(&"<a href=\"{src}\"><img src=\"{src}\"><a/>".replace("{src}", img.to_str().unwrap()))
pics.push_str(&format!("<a href=\"{src}\"><img src=\"{src}\"><a/>", src=&utf8_percent_encode(img.to_str().unwrap(), DEFAULT_ENCODE_SET).to_string()))
}
let detail = detail.replace("{images}", &pics);
@@ -125,12 +160,16 @@ fn main() {
}
}
// TODO generate a RSS feed
let main = main_tpl.replace("{breads}", &thumbs);
{
let mut f = OpenOptions::new().write(true).truncate(true).create(true).open(web_path.join("index.html")).unwrap();
f.write(main.as_bytes()).unwrap();
}
{
let f = OpenOptions::new().write(true).truncate(true).create(true).open(web_path.join("feed.xml")).unwrap();
channel.set_items(channel_items);
channel.pretty_write_to(f, b' ', 2).unwrap();
}
}