-#{ skip = 'true' }
+#{ ignore = 'true' }
--- /dev/null
+#{ minify = false }
+
+&{ base = DEFAULT }
+
+# TEST
+
+this shouldn't get minified
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)]
Ok(f) => Ok(Some(f)),
Err(e) => match e {
MetaError::Ignored => Ok(None),
- e => Err(e.into()),
+ e => Err(e),
},
}
}
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)?;
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;
}
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)?;
Err(_) => {
return Err(MetaError::FileNotFound {
path: path.to_string_lossy().to_string(),
- }
- .into())
+ })
}
};
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 {
} else if self
.opts
.pattern
- .join(key.replace(".", "/") + ".meta")
+ .join(key.replace('.', "/") + ".meta")
.exists()
|| is_source
{
pub fn get_variable(&self, key: &str) -> Result<String> {
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)) {
use std::collections::HashMap;
+use crate::MetaError;
+
#[derive(Debug, Clone, Default)]
pub struct Header {
pub blank: bool,
}
}
-impl From<HashMap<String, String>> for Header {
- fn from(value: HashMap<String, String>) -> Self {
+impl TryFrom<HashMap<String, String>> for Header {
+ type Error = MetaError;
+ fn try_from(value: HashMap<String, String>) -> Result<Self, Self::Error> {
let mut header = Header::new();
for (key, val) in value.iter() {
match &key[..] {
"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)
}
}
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())?,
"<p>GOOD GOOD</p>"
);
+ assert_eq!(
+ fs::read_to_string(dir.join("build/unit_tests/global/no_minify.html"))?,
+ "<html>\n<h1 id=\"test\">TEST</h1>\n<p>this shouldn’t get minified</p>\n\n\n\n</html>\n"
+ );
+
Ok(())
}