From 2c81d2cf35c0e6605b0740c41ac691e89cf0a7de Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Sat, 3 Jun 2023 10:14:25 -0500 Subject: [PATCH] fixed: minify header directive --- files/test_site/source/unit_tests/find_dest.meta | 2 +- .../source/unit_tests/global/no_minify.meta | 7 +++++++ src/error.rs | 4 +++- src/lib.rs | 4 ++-- src/metafile/dir/node.rs | 4 ++-- src/metafile/file.rs | 12 +++++------- src/metafile/file/patterns.rs | 2 +- src/metafile/file/variables.rs | 8 ++------ src/metafile/header.rs | 13 ++++++++----- src/parser.rs | 2 +- src/tests.rs | 5 +++++ 11 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 files/test_site/source/unit_tests/global/no_minify.meta diff --git a/files/test_site/source/unit_tests/find_dest.meta b/files/test_site/source/unit_tests/find_dest.meta index b27622f..1a2a819 100644 --- a/files/test_site/source/unit_tests/find_dest.meta +++ b/files/test_site/source/unit_tests/find_dest.meta @@ -1 +1 @@ -#{ skip = 'true' } +#{ ignore = 'true' } diff --git a/files/test_site/source/unit_tests/global/no_minify.meta b/files/test_site/source/unit_tests/global/no_minify.meta new file mode 100644 index 0000000..02d1ba3 --- /dev/null +++ b/files/test_site/source/unit_tests/global/no_minify.meta @@ -0,0 +1,7 @@ +#{ minify = false } + +&{ base = DEFAULT } + +# TEST + +this shouldn't get minified diff --git a/src/error.rs b/src/error.rs index 4f82158..241edcd 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,6 +24,8 @@ pub enum MetaError { UndefinedDefault { pattern: String, path: String }, #[error("the parser cannot resolve this input: {input}")] UnreachableRule { input: String }, + #[error("unknown option in header: {opt}")] + Header { opt: String }, #[error("{file}\n{error}")] ParserError { file: String, error: String }, #[error(transparent)] @@ -39,7 +41,7 @@ pub fn check_ignore(result: Result) -> Result, MetaEr Ok(f) => Ok(Some(f)), Err(e) => match e { MetaError::Ignored => Ok(None), - e => Err(e.into()), + e => Err(e), }, } } diff --git a/src/lib.rs b/src/lib.rs index 696d5eb..017094f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,9 +54,9 @@ pub fn build_site(opts: &Options) -> Result<()> { let mut source = DirNode::build(opts.source.clone(), opts)?; let global_init = if source.path.join("default.meta").exists() { - MetaFile::build(source.path.join("default.meta"), &opts)? + MetaFile::build(source.path.join("default.meta"), opts)? } else { - MetaFile::new(&opts) + MetaFile::new(opts) }; source.map(&global_init)?; diff --git a/src/metafile/dir/node.rs b/src/metafile/dir/node.rs index d9b2a05..94071bf 100644 --- a/src/metafile/dir/node.rs +++ b/src/metafile/dir/node.rs @@ -62,7 +62,7 @@ impl<'a> DirNode<'a> { if self.global.header.copy_only { let dest = self.global.dest()?; - fs::copy(file, &dest.parent().unwrap_or(&self.opts.build))?; + fs::copy(file, dest.parent().unwrap_or(&self.opts.build))?; continue; } @@ -86,7 +86,7 @@ impl<'a> DirNode<'a> { file.merge(&self.global); match file.construct() { Ok(str) => { - if file.header.minify && self.opts.minify { + if file.header.minify && &file.header.filetype == "html" { fs::write(file.dest()?, minify(str.as_bytes(), &HTML_CFG))?; } else { fs::write(file.dest()?, str)?; diff --git a/src/metafile/file.rs b/src/metafile/file.rs index b865709..71e0535 100644 --- a/src/metafile/file.rs +++ b/src/metafile/file.rs @@ -41,8 +41,7 @@ impl<'a> MetaFile<'a> { Err(_) => { return Err(MetaError::FileNotFound { path: path.to_string_lossy().to_string(), - } - .into()) + }) } }; @@ -71,12 +70,11 @@ impl<'a> MetaFile<'a> { return Err(Box::new(MetaError::Ignored)); } - let src_str: String; - if self.header.pandoc.map_or(true, |x| x) { - src_str = self.pandoc().map_err(MetaError::from)?; + let src_str = if self.header.pandoc.map_or(true, |x| x) { + self.pandoc().map_err(MetaError::from) } else { - src_str = self.get_source().map_err(MetaError::from)?; - } + self.get_source().map_err(MetaError::from) + }?; let pattern = self.get_pattern("base").map_err(MetaError::from)?; let mut base = parse_string(pattern, self.opts).map_err(|e| MetaError::ParserError { diff --git a/src/metafile/file/patterns.rs b/src/metafile/file/patterns.rs index 304254f..012c561 100644 --- a/src/metafile/file/patterns.rs +++ b/src/metafile/file/patterns.rs @@ -21,7 +21,7 @@ impl<'a> MetaFile<'a> { } else if self .opts .pattern - .join(key.replace(".", "/") + ".meta") + .join(key.replace('.', "/") + ".meta") .exists() || is_source { diff --git a/src/metafile/file/variables.rs b/src/metafile/file/variables.rs index 08f09f8..e217ee7 100644 --- a/src/metafile/file/variables.rs +++ b/src/metafile/file/variables.rs @@ -4,14 +4,10 @@ impl<'a> MetaFile<'a> { pub fn get_variable(&self, key: &str) -> Result { log!( self.opts, - format!( - "substituting {} in {}", - key.to_string(), - self.path.display() - ), + format!("substituting {key} in {}", self.path.display()), 2 ); - let long_key = self.name()? + "." + &key.to_string(); + let long_key = self.name()? + "." + key; if let Some(val) = self.variables.get(&Scope::create_local(&long_key)) { Ok(val.clone()) } else if let Some(val) = self.variables.get(&Scope::create_global(&long_key)) { diff --git a/src/metafile/header.rs b/src/metafile/header.rs index a895ac3..fb428ba 100644 --- a/src/metafile/header.rs +++ b/src/metafile/header.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; +use crate::MetaError; + #[derive(Debug, Clone, Default)] pub struct Header { pub blank: bool, @@ -31,8 +33,9 @@ impl Header { } } -impl From> for Header { - fn from(value: HashMap) -> Self { +impl TryFrom> for Header { + type Error = MetaError; + fn try_from(value: HashMap) -> Result { let mut header = Header::new(); for (key, val) in value.iter() { match &key[..] { @@ -45,10 +48,10 @@ impl From> for Header { "source" => header.source = val.to_string(), "ignore" => header.ignore = val == "true", "copy_only" => header.copy_only = val == "true", - "minify" => header.copy_only = val == "true", - _ => continue, + "minify" => header.minify = val == "true", + x => return Err(MetaError::Header { opt: x.to_string() }), } } - header + Ok(header) } } diff --git a/src/parser.rs b/src/parser.rs index d30dea5..a4ae372 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -32,7 +32,7 @@ pub fn parse_string(file: String, opts: &Options) -> Result { match pair.as_rule() { Rule::source => meta_file.source = parse_source(pair.into_inner()), Rule::header => { - meta_file.header = Header::from(parse_header_defs(pair.into_inner())) + meta_file.header = Header::try_from(parse_header_defs(pair.into_inner()))? } Rule::var_def => meta_file.variables = parse_defs(pair.into_inner())?, Rule::arr_def => meta_file.arrays = parse_array_defs(pair.into_inner())?, diff --git a/src/tests.rs b/src/tests.rs index 8564ce7..6acef57 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -203,5 +203,10 @@ fn test_global() -> Result<()> { "

GOOD GOOD

" ); + assert_eq!( + fs::read_to_string(dir.join("build/unit_tests/global/no_minify.html"))?, + "\n

TEST

\n

this shouldn’t get minified

\n\n\n\n\n" + ); + Ok(()) } -- 2.45.2