use UTF8 paths
This commit is contained in:
parent
4fd9f73769
commit
764c6f44f0
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -69,6 +69,12 @@ version = "3.15.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa"
|
checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "camino"
|
||||||
|
version = "1.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.0.90"
|
version = "1.0.90"
|
||||||
|
@ -868,7 +874,7 @@ checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
|
||||||
name = "ssg"
|
name = "ssg"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"camino",
|
||||||
"chrono",
|
"chrono",
|
||||||
"emojis",
|
"emojis",
|
||||||
"glob",
|
"glob",
|
||||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
aho-corasick = "1.1.3"
|
camino = "1.1.6"
|
||||||
chrono = "0.4.35"
|
chrono = "0.4.35"
|
||||||
emojis = "0.6.1"
|
emojis = "0.6.1"
|
||||||
glob = "0.3.1"
|
glob = "0.3.1"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::collections::HashSet ;
|
use std::collections::HashSet ;
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
|
use camino::Utf8PathBuf;
|
||||||
use glob::glob;
|
use glob::glob;
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,27 +14,27 @@ pub enum SourceKind {
|
||||||
pub struct Source {
|
pub struct Source {
|
||||||
pub kind: SourceKind,
|
pub kind: SourceKind,
|
||||||
pub ext: String,
|
pub ext: String,
|
||||||
pub dirs: PathBuf,
|
pub dirs: Utf8PathBuf,
|
||||||
pub path: PathBuf,
|
pub path: Utf8PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn to_source(path: PathBuf, exts: &HashSet<&'static str>) -> Source {
|
fn to_source(path: Utf8PathBuf, exts: &HashSet<&'static str>) -> Source {
|
||||||
let dirs = path.parent().unwrap();
|
let dir = path.parent().unwrap();
|
||||||
let ext = path.extension().unwrap().to_str().unwrap();
|
let ext = path.extension().unwrap();
|
||||||
|
|
||||||
if !exts.contains(ext) {
|
if !exts.contains(ext) {
|
||||||
return Source {
|
return Source {
|
||||||
kind: SourceKind::Asset,
|
kind: SourceKind::Asset,
|
||||||
ext: ext.to_owned(),
|
ext: ext.to_owned(),
|
||||||
dirs: dirs.to_owned(),
|
dirs: dir.to_owned(),
|
||||||
path,
|
path,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let dirs = match path.file_stem().unwrap().to_str().unwrap() {
|
let dirs = match path.file_stem().unwrap() {
|
||||||
"index" => dirs.to_owned(),
|
"index" => dir.to_owned(),
|
||||||
name => dirs.join(name),
|
name => dir.join(name),
|
||||||
};
|
};
|
||||||
|
|
||||||
Source {
|
Source {
|
||||||
|
@ -52,6 +52,7 @@ pub fn gather(pattern: &str, exts: &HashSet<&'static str>) -> Vec<Source> {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|path| {
|
.filter_map(|path| {
|
||||||
let path = path.unwrap();
|
let path = path.unwrap();
|
||||||
|
let path = Utf8PathBuf::from_path_buf(path).expect("Filename is not valid UTF8");
|
||||||
|
|
||||||
match path.is_dir() {
|
match path.is_dir() {
|
||||||
true => None,
|
true => None,
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
use camino::{Utf8Path, Utf8PathBuf};
|
||||||
|
|
||||||
use crate::html::Linkable;
|
use crate::html::Linkable;
|
||||||
use crate::Sack;
|
use crate::Sack;
|
||||||
|
|
||||||
|
@ -28,17 +29,17 @@ impl Debug for AssetKind {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Asset {
|
pub struct Asset {
|
||||||
pub kind: AssetKind,
|
pub kind: AssetKind,
|
||||||
pub out: PathBuf,
|
pub out: Utf8PathBuf,
|
||||||
pub link: Option<Linkable>,
|
pub link: Option<Linkable>,
|
||||||
pub meta: super::Source,
|
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 {
|
impl Virtual {
|
||||||
pub fn new<P, F>(path: P, call: F) -> Self
|
pub fn new<P, F>(path: P, call: F) -> Self
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Utf8Path>,
|
||||||
F: Fn(&Sack) -> String + 'static
|
F: Fn(&Sack) -> String + 'static
|
||||||
{
|
{
|
||||||
Self(path.as_ref().into(), Box::new(call))
|
Self(path.as_ref().into(), Box::new(call))
|
||||||
|
@ -87,29 +88,29 @@ fn render_real(item: &Asset, assets: &Sack) {
|
||||||
match &item.kind {
|
match &item.kind {
|
||||||
AssetKind::Html(render) => {
|
AssetKind::Html(render) => {
|
||||||
let i = &item.meta.path;
|
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::create_dir_all(&o.parent().unwrap()).unwrap();
|
||||||
|
|
||||||
let mut file = File::create(&o).unwrap();
|
let mut file = File::create(&o).unwrap();
|
||||||
file.write_all(render(assets).as_bytes()).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 => {
|
AssetKind::Image => {
|
||||||
let i = &item.meta.path;
|
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::create_dir_all(&o.parent().unwrap()).unwrap();
|
||||||
fs::copy(&i, &o).unwrap();
|
fs::copy(&i, &o).unwrap();
|
||||||
println!("Image: {} -> {}", i.to_str().unwrap(), o.to_str().unwrap());
|
println!("Image: {} -> {}", i, o);
|
||||||
},
|
},
|
||||||
AssetKind::Bib(_) => (),
|
AssetKind::Bib(_) => (),
|
||||||
AssetKind::Unknown => {
|
AssetKind::Unknown => {
|
||||||
let i = &item.meta.path;
|
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::create_dir_all(&o.parent().unwrap()).unwrap();
|
||||||
fs::copy(&i, &o).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) {
|
fn render_fake(item: &Virtual, assets: &Sack) {
|
||||||
let Virtual(out, render) = item;
|
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();
|
fs::create_dir_all(&o.parent().unwrap()).unwrap();
|
||||||
|
|
||||||
let mut file = File::create(&o).unwrap();
|
let mut file = File::create(&o).unwrap();
|
||||||
file.write_all(render(assets).as_bytes()).unwrap();
|
file.write_all(render(assets).as_bytes()).unwrap();
|
||||||
println!("Virtual: -> {}", o.to_str().unwrap());
|
println!("Virtual: -> {}", o);
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ impl Sack<'_> {
|
||||||
fn get_links(&self, path: &str) -> Vec<LinkDate> {
|
fn get_links(&self, path: &str) -> Vec<LinkDate> {
|
||||||
let pattern = glob::Pattern::new(path).unwrap();
|
let pattern = glob::Pattern::new(path).unwrap();
|
||||||
self.assets.iter()
|
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 {
|
.filter_map(|f| match &f.link {
|
||||||
Some(Linkable::Date(link)) => Some(link.clone()),
|
Some(Linkable::Date(link)) => Some(link.clone()),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -66,7 +66,7 @@ impl Sack<'_> {
|
||||||
fn get_links_2(&self, path: &str) -> Vec<Link> {
|
fn get_links_2(&self, path: &str) -> Vec<Link> {
|
||||||
let pattern = glob::Pattern::new(path).unwrap();
|
let pattern = glob::Pattern::new(path).unwrap();
|
||||||
self.assets.iter()
|
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 {
|
.filter_map(|f| match &f.link {
|
||||||
Some(Linkable::Link(link)) => Some(link.clone()),
|
Some(Linkable::Link(link)) => Some(link.clone()),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
Loading…
Reference in a new issue