From: Huck Boles Date: Wed, 31 May 2023 19:54:09 +0000 (-0500) Subject: added: copy_only header directory X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=2067e8d96e38b77a09e868e35d604a8a98030f78;p=metaforge.git added: copy_only header directory --- diff --git a/files/README/source/docs/header.meta b/files/README/source/docs/header.meta index 88a5351..5df7eb2 100644 --- a/files/README/source/docs/header.meta +++ b/files/README/source/docs/header.meta @@ -45,3 +45,4 @@ normal definition blocks. - source = **STRING** - change the the filetype of the source file - filetype = **STRING** - change the filetype of the output file - pandoc = **BOOL** - toggles if pandoc is ran on this file to convert between filetypes +- copy_only = **BOOL** - copys file or directory without processing anything diff --git a/files/test_site/source/unit_tests/header/copy.meta b/files/test_site/source/unit_tests/header/copy.meta new file mode 100644 index 0000000..fa04ce3 --- /dev/null +++ b/files/test_site/source/unit_tests/header/copy.meta @@ -0,0 +1,3 @@ +#{ copy_only = true } + +variable: ${this} should get copied verbatim diff --git a/src/metafile/dir/node.rs b/src/metafile/dir/node.rs index e57630e..5b878f4 100644 --- a/src/metafile/dir/node.rs +++ b/src/metafile/dir/node.rs @@ -43,6 +43,12 @@ impl<'a> DirNode<'a> { for f in fs::read_dir(&self.path)? { let file = f?.path(); + if self.global.header.copy_only { + let dest = self.global.dest()?; + fs::copy(file, &dest.parent().unwrap_or(&self.opts.build))?; + continue; + } + if file.is_dir() { let dir = DirNode::build(file, self.opts)?; self.dirs.push(dir); diff --git a/src/metafile/file.rs b/src/metafile/file.rs index a91843c..4e7a51b 100644 --- a/src/metafile/file.rs +++ b/src/metafile/file.rs @@ -63,6 +63,13 @@ impl<'a> MetaFile<'a> { return Err(Box::new(MetaError::Ignored)); } + if self.header.copy_only { + let dest = self.dest().map_err(MetaError::from)?; + let source: String = self.source.iter().map(|s| s.to_string()).collect(); + std::fs::write(dest, source).unwrap(); + return Err(Box::new(MetaError::Ignored)); + } + let html = self.to_html().map_err(MetaError::from)?; let pattern = self.get_pattern("base").map_err(MetaError::from)?; diff --git a/src/metafile/header.rs b/src/metafile/header.rs index fdaed0a..7f88674 100644 --- a/src/metafile/header.rs +++ b/src/metafile/header.rs @@ -10,6 +10,7 @@ pub struct Header { pub source: String, pub pandoc: bool, pub ignore: bool, + pub copy_only: bool, } impl Header { @@ -23,6 +24,7 @@ impl Header { source: String::from("markdown"), pandoc: true, ignore: false, + copy_only: false, } } } @@ -40,6 +42,7 @@ impl From> for Header { "filetype" => header.filetype = val.to_string(), "source" => header.source = val.to_string(), "ignore" => header.ignore = val == "true", + "copy_only" => header.copy_only = val == "true", _ => continue, } } diff --git a/src/tests.rs b/src/tests.rs index aaa3fc0..f265a7f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,4 @@ -use crate::{MetaFile, Options}; +use crate::{MetaError, MetaFile, Options}; use eyre::Result; use std::{fs, path::PathBuf}; @@ -18,7 +18,17 @@ macro_rules! unit_test ( let mut path = test_dir.join($file); path.set_extension("meta"); let file = MetaFile::build(path, &opts)?; - assert_eq!(file.construct()?, $test); + + let str = match file.construct() { + Ok(f) => f, + Err(e) => match *e { + MetaError::Ignored => return Ok(()), + e => return Err(e.into()) + } + + }; + + assert_eq!(str, $test); Ok(()) } }; @@ -125,6 +135,12 @@ unit_test!( "\n

GOOD GOOD

\n\n\n\n\n" ); +unit_test!( + copy_header, + "header/copy", + r#"variable: ${this} should get copied verbatim"# +); + panic_test!(ignore, "ignore.meta", ""); #[test]