From 1be561f6bd11496e68234694cef19c6aa6d931b7 Mon Sep 17 00:00:00 2001 From: KitsuneCafe <10284516+kitsunecafe@users.noreply.github.com> Date: Sun, 4 Feb 2024 14:42:28 -0500 Subject: [PATCH] copy static files --- src/main.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index ad955ba..cebaf0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use roxy_tera_parser::{TeraParser, TeraParserOptions}; use clap::Parser as Clap; use std::{ + ffi, fs::{self, File}, io::{BufReader, Read}, path::{Path, PathBuf}, @@ -23,8 +24,6 @@ use toml::Table; use glob::glob; use roxy_core::roxy::{Parser, Roxy}; -const DEFAULT_THEME: &'static str = "base16-ocean.dark"; - fn handle_err(err: E) -> Error { Error::new(err.to_string(), err) } @@ -88,6 +87,19 @@ fn context_from_meta_files<'a, T: AsRef>( Ok(context) } +fn copy_static>(files: &Vec<&PathBuf>, file_path: &FilePath) -> Result<(), Error> { + for file in files { + let output = file_path.to_output(file)?; + fs::create_dir_all(output.parent().unwrap())?; + fs::copy(file, file_path.to_output(file)?)?; + } + + Ok(()) +} + +const DEFAULT_THEME: &'static str = "base16-ocean.dark"; +const CONTENT_EXT: [&'static str; 4] = ["md", "tera", "html", "htm"]; + fn main() -> Result<(), Box> { let opts = Options::parse(); @@ -100,10 +112,18 @@ fn main() -> Result<(), Box> { let (meta, files): (Vec<&PathBuf>, Vec<&PathBuf>) = files.iter().partition(|f| f.extension().unwrap() == "toml"); + let (content, files): (Vec<&PathBuf>, Vec<&PathBuf>) = files + .iter() + .partition(|f| { + let ext = f.extension().and_then(ffi::OsStr::to_str).unwrap(); + CONTENT_EXT.contains(&ext) + }); + let mut context = context_from_meta_files(&meta, &file_path)?; let theme = config.theme.unwrap_or(DEFAULT_THEME.to_string()); - for file in files { + + for file in content { let file_name = file.with_extension("html"); let output_path = file_path.to_output(&file_name)?; @@ -136,5 +156,7 @@ fn main() -> Result<(), Box> { Roxy::process_file(&file, &output_path, &mut parser).unwrap(); } + copy_static(&files, &file_path)?; + Ok(()) }