]> git.huck.website - metaforge.git/commitdiff
added: copy_only header directory
authorHuck Boles <huck@huck.website>
Wed, 31 May 2023 19:54:09 +0000 (14:54 -0500)
committerHuck Boles <huck@huck.website>
Wed, 31 May 2023 19:54:09 +0000 (14:54 -0500)
files/README/source/docs/header.meta
files/test_site/source/unit_tests/header/copy.meta [new file with mode: 0644]
src/metafile/dir/node.rs
src/metafile/file.rs
src/metafile/header.rs
src/tests.rs

index 88a535161e8cf270c923a847b657183a7ed76c1f..5df7eb2b6ddc6ccb8d3e370b203160072107d592 100644 (file)
@@ -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 (file)
index 0000000..fa04ce3
--- /dev/null
@@ -0,0 +1,3 @@
+#{ copy_only = true }
+
+variable: ${this} should get copied verbatim
index e57630e3ca8f534fee3b480518937a4870767430..5b878f4d1dbb4611dc4fa8934f4ec023b0850d8e 100644 (file)
@@ -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);
index a91843ce48a9a0d86fae55b4467199b04736901a..4e7a51b9c1dc30667983711c82c7861aeacb0daa 100644 (file)
@@ -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)?;
index fdaed0aab50b02177a7c28240e57b04314a750a8..7f88674369e89c8a52f4ebb61c613cefac2627e7 100644 (file)
@@ -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<HashMap<String, String>> 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,
             }
         }
index aaa3fc055c777e83fd53b61c2e2c27223b2d6e3c..f265a7f419002ad7bdb3fd00871f4511ae9ae2db 100644 (file)
@@ -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!(
     "<html>\n<p>GOOD GOOD</p>\n\n\n\n</html>\n"
 );
 
+unit_test!(
+    copy_header,
+    "header/copy",
+    r#"variable: ${this} should get copied verbatim"#
+);
+
 panic_test!(ignore, "ignore.meta", "");
 
 #[test]