Skip to content

Commit 07e578d

Browse files
committed
whitespace control
resolves #67
1 parent 323ecd6 commit 07e578d

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/lexer.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub enum Element {
1818
}
1919

2020
lazy_static! {
21-
static ref MARKUP: Regex = Regex::new("\\{%.*?%\\}|\\{\\{.*?\\}\\}").unwrap();
21+
static ref MARKUP: Regex = {
22+
let t = "(?:[[:space:]]*\\{\\{-|\\{\\{).*?(?:-\\}\\}[[:space:]]*|\\}\\})";
23+
let e = "(?:[[:space:]]*\\{%-|\\{%).*?(?:-%\\}[[:space:]]*|%\\})";
24+
Regex::new(&format!("{}|{}", t, e)).unwrap()
25+
};
2226
}
2327

2428
fn split_blocks(text: &str) -> Vec<&str> {
@@ -42,8 +46,14 @@ fn split_blocks(text: &str) -> Vec<&str> {
4246
}
4347

4448
lazy_static! {
45-
static ref EXPRESSION: Regex = Regex::new("\\{\\{(.*?)\\}\\}").unwrap();
46-
static ref TAG: Regex = Regex::new("\\{%(.*?)%\\}").unwrap();
49+
static ref EXPRESSION: Regex = {
50+
let t = "(?:[[:space:]]*\\{\\{-|\\{\\{)(.*?)(?:-\\}\\}[[:space:]]*|\\}\\})";
51+
Regex::new(t).unwrap()
52+
};
53+
static ref TAG: Regex = {
54+
let e = "(?:[[:space:]]*\\{%-|\\{%)(.*?)(?:-%\\}[[:space:]]*|%\\})";
55+
Regex::new(e).unwrap()
56+
};
4757
}
4858

4959
pub fn tokenize(text: &str) -> Result<Vec<Element>> {
@@ -150,6 +160,21 @@ fn test_split_blocks() {
150160
assert_eq!(split_blocks("asdlkjfn\n{%askdljfbalkjsdbf%} asdjlfb"),
151161
vec!["asdlkjfn\n", "{%askdljfbalkjsdbf%}", " asdjlfb"]);
152162
}
163+
#[test]
164+
fn test_whitespace_control() {
165+
assert_eq!(split_blocks("foo {{ bar }} 2000"),
166+
vec!["foo ", "{{ bar }}", " 2000"]);
167+
assert_eq!(split_blocks("foo {{- bar -}} 2000"),
168+
vec!["foo", " {{- bar -}} ", "2000"]);
169+
assert_eq!(split_blocks("foo \n{{- bar }} 2000"),
170+
vec!["foo", " \n{{- bar }}", " 2000"]);
171+
assert_eq!(split_blocks("foo {% bar %} 2000"),
172+
vec!["foo ", "{% bar %}", " 2000"]);
173+
assert_eq!(split_blocks("foo {%- bar -%} 2000"),
174+
vec!["foo", " {%- bar -%} ", "2000"]);
175+
assert_eq!(split_blocks("foo \n{%- bar %} 2000"),
176+
vec!["foo", " \n{%- bar %}", " 2000"]);
177+
}
153178

154179
#[test]
155180
fn test_split_atom() {
@@ -182,6 +207,12 @@ fn test_tokenize() {
182207
StringLiteral("world".to_owned())],
183208
"{{hello 'world'}}".to_owned()),
184209
Raw(" test".to_owned())]);
210+
assert_eq!(tokenize("wat \n {{-hello 'world'-}} test").unwrap(),
211+
vec![Raw("wat".to_owned()),
212+
Expression(vec![Identifier("hello".to_owned()),
213+
StringLiteral("world".to_owned())],
214+
" \n {{-hello 'world'-}} ".to_owned()),
215+
Raw("test".to_owned())]);
185216
}
186217

187218
#[test]

0 commit comments

Comments
 (0)