i dont remember
This commit is contained in:
parent
f4308b8487
commit
fc38c360eb
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -816,6 +816,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "roxy_core"
|
name = "roxy_core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
source = "git+https://github.com/kitsunecafe/roxy-core.git#42af0529bfc8259930c21b6edec94ba2793b0db1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pulldown-cmark",
|
"pulldown-cmark",
|
||||||
"tera",
|
"tera",
|
||||||
|
|
|
@ -6,6 +6,6 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
glob = "0.3.1"
|
glob = "0.3.1"
|
||||||
syntect = "5.1.0"
|
syntect = "5.1.0"
|
||||||
roxy_core = { path = "../roxy_core/" }
|
roxy_core = { git = "https://github.com/kitsunecafe/roxy-core.git" }
|
||||||
clap = { version = "4.4.17", features = ["derive"] }
|
clap = { version = "4.4.17", features = ["derive"] }
|
||||||
|
|
||||||
|
|
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[toolchain]
|
||||||
|
channel = "nightly"
|
54
src/main.rs
54
src/main.rs
|
@ -1,8 +1,6 @@
|
||||||
use clap::Parser as Clap;
|
use clap::Parser as Clap;
|
||||||
use std::{
|
use std::{
|
||||||
ffi,
|
|
||||||
io::{Error, ErrorKind},
|
io::{Error, ErrorKind},
|
||||||
ops::Deref,
|
|
||||||
path::{Path, PathBuf, StripPrefixError},
|
path::{Path, PathBuf, StripPrefixError},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -24,6 +22,12 @@ struct RoxyError {
|
||||||
message: String,
|
message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<String> for RoxyError {
|
||||||
|
fn from(value: String) -> Self {
|
||||||
|
Self { message: value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<PatternError> for RoxyError {
|
impl From<PatternError> for RoxyError {
|
||||||
fn from(value: PatternError) -> Self {
|
fn from(value: PatternError) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -46,24 +50,42 @@ impl From<RoxyError> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_files(path: &str) -> Result<Vec<PathBuf>, RoxyError> {
|
fn get_files<P: AsRef<Path> + std::fmt::Debug>(path: &P) -> Result<Vec<PathBuf>, RoxyError> {
|
||||||
glob(path)
|
let path = path
|
||||||
.map(|p| p.filter_map(|x| x.ok()).collect())
|
.as_ref()
|
||||||
.map_err(RoxyError::from)
|
.to_str()
|
||||||
|
.ok_or_else(|| RoxyError::from(format!("{path:?} is not a valid path.")))?;
|
||||||
|
|
||||||
|
let files: Vec<PathBuf> = glob(path)?
|
||||||
|
.filter_map(|x| x.ok())
|
||||||
|
.filter(|f| Path::is_file(f))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(files)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct FilePath<'a, P: AsRef<Path>> {
|
struct FilePath<'a, P: AsRef<Path>> {
|
||||||
input: &'a P,
|
input: PathBuf,
|
||||||
|
root_dir: PathBuf,
|
||||||
output: &'a P,
|
output: &'a P,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, P: AsRef<Path> + 'a> FilePath<'a, P> {
|
impl<'a, P: AsRef<Path> + 'a> FilePath<'a, P> {
|
||||||
pub fn new(input: &'a P, output: &'a P) -> Self {
|
pub fn new(input: &'a P, output: &'a P) -> Self {
|
||||||
Self { input, output }
|
Self {
|
||||||
|
input: Self::make_recursive(input),
|
||||||
|
root_dir: Self::strip_wildcards(input),
|
||||||
|
output,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_wildcard(path: &str) -> bool {
|
fn make_recursive(path: &'a P) -> PathBuf {
|
||||||
path.contains("*")
|
path.as_ref().join("**/*")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_no_wildcard<S: AsRef<str>>(path: &S) -> bool {
|
||||||
|
!path.as_ref().contains("*")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn strip_wildcards<P2: AsRef<Path> + ?Sized>(path: &'a P2) -> PathBuf {
|
fn strip_wildcards<P2: AsRef<Path> + ?Sized>(path: &'a P2) -> PathBuf {
|
||||||
|
@ -71,15 +93,14 @@ impl<'a, P: AsRef<Path> + 'a> FilePath<'a, P> {
|
||||||
.ancestors()
|
.ancestors()
|
||||||
.map(Path::to_str)
|
.map(Path::to_str)
|
||||||
.flatten()
|
.flatten()
|
||||||
.inspect(|f| println!("{f}"))
|
.find(Self::has_no_wildcard)
|
||||||
.skip_while(Self::has_wildcard)
|
.map_or_else(|| PathBuf::new(), PathBuf::from)
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_output<P2: AsRef<Path>>(&self, value: &'a P2) -> Result<PathBuf, RoxyError> {
|
pub fn to_output<P2: AsRef<Path>>(&self, value: &'a P2) -> Result<PathBuf, RoxyError> {
|
||||||
value
|
value
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.strip_prefix(Self::strip_wildcards(self.input))
|
.strip_prefix(&self.root_dir)
|
||||||
.map(|path| self.output.as_ref().join(path))
|
.map(|path| self.output.as_ref().join(path))
|
||||||
.map_err(RoxyError::from)
|
.map_err(RoxyError::from)
|
||||||
}
|
}
|
||||||
|
@ -93,8 +114,9 @@ fn main() -> Result<(), RoxyError> {
|
||||||
parser.push(html);
|
parser.push(html);
|
||||||
let file_path = FilePath::new(&opts.input, &opts.output);
|
let file_path = FilePath::new(&opts.input, &opts.output);
|
||||||
|
|
||||||
for file in get_files(&opts.input)? {
|
for file in get_files(&file_path.input)? {
|
||||||
Roxy::process_file(&file, &(&file_path.to_output(&file)?), &mut parser);
|
let file_name = file.with_extension("html");
|
||||||
|
let _ = Roxy::process_file(&file, &(&file_path.to_output(&file_name)?), &mut parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue