allow picking cover photo, add pub dates

This commit is contained in:
2019-01-26 00:55:14 +01:00
parent 879580df39
commit 1657c72d34
38 changed files with 489 additions and 24 deletions
+31 -3
View File
@@ -10,6 +10,10 @@ use markdown;
use std::fs::OpenOptions;
use rss::{Channel, ChannelBuilder, Item, ItemBuilder, Guid};
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
use image_utils;
use chrono::offset::TimeZone;
use chrono::Date;
use chrono::Utc;
#[derive(Debug)]
struct Bread {
@@ -22,7 +26,14 @@ struct Bread {
impl Bread {
fn thumb_photo(&self) -> (&str, &str) {
let first_img : &PathBuf = self.images.get(0).unwrap();
let mut first_img : &PathBuf = self.images.get(0).unwrap();
for im in &self.images {
if im.file_name().unwrap().to_str().unwrap().contains("cover") {
first_img = im;
break;
}
}
let img_path = first_img.to_str().unwrap();
let img_alt = first_img.file_name().unwrap().to_str().unwrap();
@@ -75,6 +86,7 @@ fn main() {
let web_path = Path::new(&cwd).join("web");
let data_path = web_path.join("data");
let tpl_path = web_path.join("templates");
let thumbs_path = web_path.join("thumbs");
let mut bread_dirs: Vec<DirEntry> = fs::read_dir(&data_path).unwrap().map(|e| e.unwrap()).collect();
bread_dirs.sort_by(|x, y| x.file_name().cmp(&y.file_name()));
@@ -107,12 +119,24 @@ fn main() {
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 date_slug = bread.date.format("%Y-%m-%d").to_string();
let detail_file = date_slug.clone() + ".html";
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 image_path_encoded = utf8_percent_encode(img_path, DEFAULT_ENCODE_SET).to_string();
let thumb_fname = date_slug.clone() + "." + Path::new(&img_path).extension().unwrap().to_str().unwrap();
let thumb_path = thumbs_path.join(&thumb_fname);
let thumb_relpath = thumb_path.strip_prefix(&web_path).unwrap();
let image_path_encoded = utf8_percent_encode(thumb_relpath.to_str().unwrap(), DEFAULT_ENCODE_SET).to_string();
let im = image::open(&web_path.join(img_path)).unwrap();
let im = im.thumbnail(500, 500);
//let mut output = File::create(&thumb_path);
im.save(&thumb_path).unwrap();
//image_utils::resize(&web_path.join(img_path), 500, 500, &thumb_path).unwrap();
// bread pic for the thumbnails page
{
@@ -134,11 +158,15 @@ fn main() {
guid.set_value(link.clone());
guid.set_permalink(true);
let date_formatted : Date<Utc> = chrono::Utc.from_local_date(&bread.date).unwrap();
let dt = date_formatted.and_hms(12,0,0);
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)
.pub_date(dt.to_rfc2822())
.build().unwrap());
}