From: Huck Boles Date: Wed, 31 May 2023 19:23:27 +0000 (-0500) Subject: fixed: whitespace handling in substitutions X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=e2987f31e06b0946194805e75429536906c58fc8;p=metaforge.git fixed: whitespace handling in substitutions --- diff --git a/files/test_site/source/unit_tests/expand/spaces.meta b/files/test_site/source/unit_tests/expand/spaces.meta new file mode 100644 index 0000000..073cf92 --- /dev/null +++ b/files/test_site/source/unit_tests/expand/spaces.meta @@ -0,0 +1,6 @@ +${ + var1 = 'GOOD' + var2 = 'GOOD' +} + +${var1} ${var2} diff --git a/src/metafile/file/variables.rs b/src/metafile/file/variables.rs index 033f38c..08f09f8 100644 --- a/src/metafile/file/variables.rs +++ b/src/metafile/file/variables.rs @@ -12,13 +12,13 @@ impl<'a> MetaFile<'a> { 2 ); let long_key = self.name()? + "." + &key.to_string(); - if let Some(val) = self.variables.get(&Scope::into_local(&long_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::into_global(&long_key)) { + } else if let Some(val) = self.variables.get(&Scope::create_global(&long_key)) { Ok(val.clone()) - } else if let Some(val) = self.variables.get(&Scope::into_local(key)) { + } else if let Some(val) = self.variables.get(&Scope::create_local(key)) { Ok(val.clone()) - } else if let Some(val) = self.variables.get(&Scope::into_global(key)) { + } else if let Some(val) = self.variables.get(&Scope::create_global(key)) { Ok(val.clone()) } else if self.opts.undefined || self.header.panic_undefined { return Err(MetaError::UndefinedExpand { diff --git a/src/parser.rs b/src/parser.rs index 259ddba..d30dea5 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -11,7 +11,7 @@ use source::*; #[cfg(test)] mod tests; -use crate::{log, Header, MetaError, MetaFile, Options}; +use crate::{Header, MetaError, MetaFile, Options}; use eyre::Result; use pest::{ iterators::{Pair, Pairs}, diff --git a/src/parser/meta.pest b/src/parser/meta.pest index a4aa0d7..1116c97 100644 --- a/src/parser/meta.pest +++ b/src/parser/meta.pest @@ -17,10 +17,10 @@ char = _{ array = _{ "[" ~ "]" - | "[" - ~ WHITESPACE* - ~ string - ~ (WHITESPACE* ~ "," ~ WHITESPACE* ~ string)* + | "[" + ~ WHITESPACE* + ~ string + ~ (WHITESPACE* ~ "," ~ WHITESPACE* ~ string)* ~ WHITESPACE* ~ ","? ~ WHITESPACE* ~ "]" } @@ -46,9 +46,9 @@ substitution = _{ sigil ~ key ~ "}" } var_sub = { &("$") ~ substitution } arr_sub = { &("@") ~ substitution } pat_sub = { &("&") ~ substitution } -identifier = _{ var_sub | pat_sub | arr_sub } +identifier = _{ var_sub | pat_sub | arr_sub | COMMENT} -source = { (identifier | char_seq)* } +source = ${ (identifier | char_seq)* } file = { SOI ~ header? ~ definition* ~ source? ~ EOI diff --git a/src/tests.rs b/src/tests.rs index 26fb177..aaa3fc0 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -47,58 +47,62 @@ macro_rules! panic_test ( ); unit_test!(blank_pattern, "blank/blank_pattern", ""); -unit_test!(blank_variable, "blank/blank_variable", "\n\n"); -unit_test!(blank_array, "blank/blank_array", "\n\n"); -unit_test!(blank_comment, "blank/comment", "\n\n"); +unit_test!( + blank_variable, + "blank/blank_variable", + "\n\n\n\n" +); +unit_test!(blank_array, "blank/blank_array", "\n\n\n\n"); +unit_test!(blank_comment, "blank/comment", "\n\n\n\n\n"); unit_test!( inline_comment, "blank/inline_comment", - "\n

inline comment

\n\n" + "\n

inline comment

\n\n\n\n\n" ); unit_test!( expand_var_in_src, "expand/variable_in_source", - "\n

GOOD

\n\n" + "\n

GOOD

\n\n\n\n\n" ); unit_test!( expand_var_in_pat, "expand/variable_in_pattern", - "\nGOOD\n" + "\nGOOD\n\n\n\n" ); unit_test!( expand_arr_in_src, "expand/array_in_source", - "\n

12345

\n\n" + "\n

1 2 3 4 5

\n\n\n\n\n" ); unit_test!( expand_arr_in_pat, "expand/array_in_pattern", - "\n12345\n" + "\n1\n2\n3\n4\n5\n\n\n\n" ); unit_test!( expand_pat_in_src, "expand/pattern_in_source", - "

GOOD

\n" + "

GOOD

\n\n" ); unit_test!( expand_pat_in_pat, "expand/pattern_in_pattern", - "\nGOOD\nGOOD\n\n" + "\nGOOD\nGOOD\n\n\n\n\n" ); unit_test!( override_var, "override/variable", - "\n

GOOD

\n\n" + "\n

GOOD

\n\n\n\n\n" ); unit_test!( override_pat, "override/pattern", - "\nGOOD\nGOOD\n\n" + "\nGOOD\n GOOD\n\n\n\n\n" ); unit_test!( header_no_pandoc, "header/pandoc", - "# This should not become html\n" + "# This should not become html\n\n" ); unit_test!(header_blank, "header/blank", ""); @@ -106,13 +110,19 @@ unit_test!(header_blank, "header/blank", ""); unit_test!( pat_file, "expand/file.meta", - "\n

GOOD

\n\n" + "\n

GOOD

\n\n\n\n\n" ); unit_test!( direct_call, "expand/direct_call", - "\n

abcd

\n\n" + "\n

a b c d

\n\n\n\n\n" +); + +unit_test!( + expand_spaces, + "expand/spaces", + "\n

GOOD GOOD

\n\n\n\n\n" ); panic_test!(ignore, "ignore.meta", ""); @@ -157,12 +167,12 @@ fn test_global() -> Result<()> { assert_eq!( fs::read_to_string(dir.join("build/unit_tests/global/pattern.html"))?, - "

GOOD GOOD

\n" + "

GOOD

\n

GOOD

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

GOODGOOD

\n" + "

GOOD GOOD

\n\n" ); Ok(())