diff --git a/Cargo.lock b/Cargo.lock index c4ca2ad..e37a4fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,6 +69,12 @@ version = "3.15.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" +[[package]] +name = "camino" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" + [[package]] name = "cc" version = "1.0.90" @@ -868,7 +874,7 @@ checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" name = "ssg" version = "0.1.0" dependencies = [ - "aho-corasick", + "camino", "chrono", "emojis", "glob", diff --git a/Cargo.toml b/Cargo.toml index 08539b3..a8cef0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -aho-corasick = "1.1.3" +camino = "1.1.6" chrono = "0.4.35" emojis = "0.6.1" glob = "0.3.1" diff --git a/content/wiki/japanese.md b/content/wiki/japanese/index.md similarity index 100% rename from content/wiki/japanese.md rename to content/wiki/japanese/index.md diff --git a/src/gen/load.rs b/src/gen/load.rs index 6d3e77e..3a466fc 100644 --- a/src/gen/load.rs +++ b/src/gen/load.rs @@ -1,6 +1,6 @@ use std::collections::HashSet ; -use std::path::PathBuf; +use camino::Utf8PathBuf; use glob::glob; @@ -14,27 +14,27 @@ pub enum SourceKind { pub struct Source { pub kind: SourceKind, pub ext: String, - pub dirs: PathBuf, - pub path: PathBuf, + pub dirs: Utf8PathBuf, + pub path: Utf8PathBuf, } -fn to_source(path: PathBuf, exts: &HashSet<&'static str>) -> Source { - let dirs = path.parent().unwrap(); - let ext = path.extension().unwrap().to_str().unwrap(); +fn to_source(path: Utf8PathBuf, exts: &HashSet<&'static str>) -> Source { + let dir = path.parent().unwrap(); + let ext = path.extension().unwrap(); if !exts.contains(ext) { return Source { kind: SourceKind::Asset, ext: ext.to_owned(), - dirs: dirs.to_owned(), + dirs: dir.to_owned(), path, }; } - let dirs = match path.file_stem().unwrap().to_str().unwrap() { - "index" => dirs.to_owned(), - name => dirs.join(name), + let dirs = match path.file_stem().unwrap() { + "index" => dir.to_owned(), + name => dir.join(name), }; Source { @@ -52,6 +52,7 @@ pub fn gather(pattern: &str, exts: &HashSet<&'static str>) -> Vec { .into_iter() .filter_map(|path| { let path = path.unwrap(); + let path = Utf8PathBuf::from_path_buf(path).expect("Filename is not valid UTF8"); match path.is_dir() { true => None, diff --git a/src/gen/render.rs b/src/gen/render.rs index 9224b14..94940fe 100644 --- a/src/gen/render.rs +++ b/src/gen/render.rs @@ -1,8 +1,9 @@ use std::fmt::Debug; use std::fs::{self, File}; -use std::path::{Path, PathBuf}; use std::io::Write; +use camino::{Utf8Path, Utf8PathBuf}; + use crate::html::Linkable; use crate::Sack; @@ -28,17 +29,17 @@ impl Debug for AssetKind { #[derive(Debug)] pub struct Asset { pub kind: AssetKind, - pub out: PathBuf, + pub out: Utf8PathBuf, pub link: Option, pub meta: super::Source, } -pub struct Virtual(pub PathBuf, pub Box String>); +pub struct Virtual(pub Utf8PathBuf, pub Box String>); impl Virtual { pub fn new(path: P, call: F) -> Self where - P: AsRef, + P: AsRef, F: Fn(&Sack) -> String + 'static { Self(path.as_ref().into(), Box::new(call)) @@ -87,29 +88,29 @@ fn render_real(item: &Asset, assets: &Sack) { match &item.kind { AssetKind::Html(render) => { let i = &item.meta.path; - let o = Path::new("dist").join(&item.out); + let o = Utf8Path::new("dist").join(&item.out); fs::create_dir_all(&o.parent().unwrap()).unwrap(); let mut file = File::create(&o).unwrap(); file.write_all(render(assets).as_bytes()).unwrap(); - println!("HTML: {} -> {}", i.to_str().unwrap(), o.to_str().unwrap()); + println!("HTML: {} -> {}", i, o); }, AssetKind::Image => { let i = &item.meta.path; - let o = Path::new("dist").join(&item.out); + let o = Utf8Path::new("dist").join(&item.out); fs::create_dir_all(&o.parent().unwrap()).unwrap(); fs::copy(&i, &o).unwrap(); - println!("Image: {} -> {}", i.to_str().unwrap(), o.to_str().unwrap()); + println!("Image: {} -> {}", i, o); }, AssetKind::Bib(_) => (), AssetKind::Unknown => { let i = &item.meta.path; - let o = Path::new("dist").join(&item.out); + let o = Utf8Path::new("dist").join(&item.out); fs::create_dir_all(&o.parent().unwrap()).unwrap(); fs::copy(&i, &o).unwrap(); - println!("Unknown: {} -> {}", i.to_str().unwrap(), o.to_str().unwrap()); + println!("Unknown: {} -> {}", i, o); }, } } @@ -117,10 +118,10 @@ fn render_real(item: &Asset, assets: &Sack) { fn render_fake(item: &Virtual, assets: &Sack) { let Virtual(out, render) = item; - let o = Path::new("dist").join(&out); + let o = Utf8Path::new("dist").join(&out); fs::create_dir_all(&o.parent().unwrap()).unwrap(); let mut file = File::create(&o).unwrap(); file.write_all(render(assets).as_bytes()).unwrap(); - println!("Virtual: -> {}", o.to_str().unwrap()); + println!("Virtual: -> {}", o); } diff --git a/src/main.rs b/src/main.rs index 29745bd..c5b5358 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,7 +55,7 @@ impl Sack<'_> { fn get_links(&self, path: &str) -> Vec { let pattern = glob::Pattern::new(path).unwrap(); self.assets.iter() - .filter(|f| pattern.matches_path(&f.out)) + .filter(|f| pattern.matches_path(f.out.as_ref())) .filter_map(|f| match &f.link { Some(Linkable::Date(link)) => Some(link.clone()), _ => None, @@ -66,7 +66,7 @@ impl Sack<'_> { fn get_links_2(&self, path: &str) -> Vec { let pattern = glob::Pattern::new(path).unwrap(); self.assets.iter() - .filter(|f| pattern.matches_path(&f.out)) + .filter(|f| pattern.matches_path(f.out.as_ref())) .filter_map(|f| match &f.link { Some(Linkable::Link(link)) => Some(link.clone()), _ => None,