use UTF8 paths

This commit is contained in:
Maciej Jur 2024-04-22 20:05:09 +02:00
parent 4fd9f73769
commit 764c6f44f0
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
6 changed files with 34 additions and 26 deletions

8
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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<Source> {
.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,

View file

@ -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<Linkable>,
pub meta: super::Source,
}
pub struct Virtual(pub PathBuf, pub Box<dyn Fn(&Sack) -> String>);
pub struct Virtual(pub Utf8PathBuf, pub Box<dyn Fn(&Sack) -> String>);
impl Virtual {
pub fn new<P, F>(path: P, call: F) -> Self
where
P: AsRef<Path>,
P: AsRef<Utf8Path>,
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);
}

View file

@ -55,7 +55,7 @@ impl Sack<'_> {
fn get_links(&self, path: &str) -> Vec<LinkDate> {
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<Link> {
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,