You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.4 KiB
137 lines
4.4 KiB
6 years ago
|
use std::env;
|
||
|
use std::fs;
|
||
|
use std::fs::DirEntry;
|
||
|
use std::fs::File;
|
||
|
use std::io::prelude::*;
|
||
|
use std::path::{Path, PathBuf};
|
||
|
use chrono;
|
||
|
use chrono::NaiveDate;
|
||
|
use markdown;
|
||
|
use std::fs::OpenOptions;
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
struct Bread {
|
||
|
path: PathBuf,
|
||
|
rel_path: PathBuf,
|
||
|
date: chrono::NaiveDate,
|
||
|
note: String,
|
||
|
images: Vec<PathBuf>,
|
||
|
}
|
||
|
|
||
|
impl Bread {
|
||
|
fn thumb_photo(&self) -> (&str, &str) {
|
||
|
let first_img : &PathBuf = self.images.get(0).unwrap();
|
||
|
|
||
|
let img_path = first_img.to_str().unwrap();
|
||
|
let img_alt = first_img.file_name().unwrap().to_str().unwrap();
|
||
|
|
||
|
(img_path, img_alt)
|
||
|
}
|
||
|
|
||
|
fn parse(base_dir : &PathBuf, bread_dir : &DirEntry) -> Result<Bread, std::io::Error> {
|
||
|
let bpath = bread_dir.path();
|
||
|
let mut note = String::new();
|
||
|
|
||
|
let mut note_path = bpath.join("note.txt");
|
||
|
|
||
|
// try a md one as a fallback
|
||
|
if !note_path.exists() {
|
||
|
note_path = bpath.join("note.md");
|
||
|
}
|
||
|
|
||
|
if note_path.exists() {
|
||
|
let mut note_file = File::open(note_path)?;
|
||
|
note_file.read_to_string(&mut note)?;
|
||
|
note = markdown::to_html(¬e);
|
||
|
}
|
||
|
|
||
|
let mut bread_files: Vec<DirEntry> = fs::read_dir(&bpath)?.map(|e| e.unwrap()).collect();
|
||
|
bread_files.sort_by(|x, y| x.file_name().cmp(&y.file_name()));
|
||
|
|
||
|
let images = bread_files.iter().filter(|&f| {
|
||
|
let fname = f.file_name();
|
||
|
let name = fname.to_str().unwrap();
|
||
|
return
|
||
|
name.ends_with(".png") ||
|
||
|
name.ends_with(".jpg") ||
|
||
|
name.ends_with(".jpeg") ||
|
||
|
name.ends_with(".gif");
|
||
|
}).map(|x| x.path().strip_prefix(base_dir).unwrap().to_path_buf()).collect();
|
||
|
|
||
|
return Ok(Bread {
|
||
|
date: NaiveDate::parse_from_str(bpath.file_name().unwrap().to_str().unwrap(), "%Y-%m-%d").unwrap(),
|
||
|
rel_path: bpath.strip_prefix(base_dir).unwrap().to_path_buf(),
|
||
|
path: bpath,
|
||
|
note,
|
||
|
images
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let cwd = env::current_dir().unwrap();
|
||
|
let web_path = Path::new(&cwd).join("web");
|
||
|
let data_path = web_path.join("data");
|
||
|
let tpl_path = web_path.join("templates");
|
||
|
|
||
|
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()));
|
||
|
|
||
|
let mut breads : Vec<Bread> = Vec::new();
|
||
|
|
||
|
for bread_dir in bread_dirs {
|
||
|
if let Ok(b) = Bread::parse(&web_path, &bread_dir) {
|
||
|
breads.push(b);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let mut main_tpl = String::new();
|
||
|
let mut thumb_tpl = String::new();
|
||
|
let mut detail_tpl = String::new();
|
||
|
File::open(tpl_path.join("index.html")).unwrap().read_to_string(&mut main_tpl).unwrap();
|
||
|
File::open(tpl_path.join("_thumb.html")).unwrap().read_to_string(&mut thumb_tpl).unwrap();
|
||
|
File::open(tpl_path.join("detail.html")).unwrap().read_to_string(&mut detail_tpl).unwrap();
|
||
|
|
||
|
let mut thumbs = String::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 thumb = thumb_tpl
|
||
|
.replace("{detail_url}", &detail_file)
|
||
|
.replace("{img_src}", &img_path)
|
||
|
.replace("{img_alt}", &img_alt)
|
||
|
.replace("{title}", &date);
|
||
|
|
||
|
thumbs.push_str(&thumb);
|
||
|
|
||
|
{
|
||
|
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 });
|
||
|
|
||
|
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()))
|
||
|
}
|
||
|
|
||
|
let detail = detail.replace("{images}", &pics);
|
||
|
|
||
|
let mut f = OpenOptions::new().write(true).truncate(true).create(true).open(web_path.join(detail_file)).unwrap();
|
||
|
f.write(detail.as_bytes()).unwrap();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 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();
|
||
|
}
|
||
|
}
|