Skip to content

Commit de4dc62

Browse files
authored
Merge pull request #77 from fiji-flo/whitespace-control
whitespace control
2 parents 96ba777 + 9e04133 commit de4dc62

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

src/lexer.rs

Lines changed: 34 additions & 3 deletions
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]

tests/whitespace_control.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
extern crate liquid;
2+
3+
use liquid::LiquidOptions;
4+
use liquid::Renderable;
5+
use liquid::Context;
6+
use liquid::parse;
7+
use std::default::Default;
8+
9+
macro_rules! compare {
10+
($input:expr, $output:expr) => {
11+
let input = $input.replace("…", " ");
12+
let expected = $output.replace("…", " ");
13+
let options: LiquidOptions = Default::default();
14+
let template = parse(&input, options).unwrap();
15+
16+
let mut data = Context::new();
17+
let output = template.render(&mut data);
18+
assert_eq!(output.unwrap(), Some(expected));
19+
}
20+
}
21+
22+
#[test]
23+
pub fn no_whitespace_control() {
24+
compare!("
25+
topic1
26+
……{% assign foo = \"bar\" %}
27+
……{% if foo %}
28+
…………-……{{ foo }}
29+
……{% endif %}
30+
",
31+
"
32+
topic1
33+
……
34+
……
35+
…………-……bar
36+
……
37+
");
38+
}
39+
40+
#[test]
41+
pub fn simple_whitespace_control() {
42+
compare!("
43+
topic1
44+
……{% assign foo = \"bar\" -%}
45+
……{% if foo -%}
46+
…………-……{{- foo }}
47+
……{%- endif %}
48+
",
49+
"
50+
topic1
51+
……-bar
52+
");
53+
}
54+
55+
#[test]
56+
pub fn double_sided_whitespace_control() {
57+
compare!("
58+
topic1
59+
……{%- assign foo = \"bar\" -%}
60+
……-……{{- foo -}}……
61+
62+
",
63+
"
64+
topic1-bar\
65+
");
66+
}

0 commit comments

Comments
 (0)