Skip to content

whitespace control #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ pub enum Element {
}

lazy_static! {
static ref MARKUP: Regex = Regex::new("\\{%.*?%\\}|\\{\\{.*?\\}\\}").unwrap();
static ref MARKUP: Regex = {
let t = "(?:[[:space:]]*\\{\\{-|\\{\\{).*?(?:-\\}\\}[[:space:]]*|\\}\\})";
let e = "(?:[[:space:]]*\\{%-|\\{%).*?(?:-%\\}[[:space:]]*|%\\})";
Regex::new(&format!("{}|{}", t, e)).unwrap()
};
}

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

lazy_static! {
static ref EXPRESSION: Regex = Regex::new("\\{\\{(.*?)\\}\\}").unwrap();
static ref TAG: Regex = Regex::new("\\{%(.*?)%\\}").unwrap();
static ref EXPRESSION: Regex = {
let t = "(?:[[:space:]]*\\{\\{-|\\{\\{)(.*?)(?:-\\}\\}[[:space:]]*|\\}\\})";
Regex::new(t).unwrap()
};
static ref TAG: Regex = {
let e = "(?:[[:space:]]*\\{%-|\\{%)(.*?)(?:-%\\}[[:space:]]*|%\\})";
Regex::new(e).unwrap()
};
}

pub fn tokenize(text: &str) -> Result<Vec<Element>> {
Expand Down Expand Up @@ -150,6 +160,21 @@ fn test_split_blocks() {
assert_eq!(split_blocks("asdlkjfn\n{%askdljfbalkjsdbf%} asdjlfb"),
vec!["asdlkjfn\n", "{%askdljfbalkjsdbf%}", " asdjlfb"]);
}
#[test]
fn test_whitespace_control() {
assert_eq!(split_blocks("foo {{ bar }} 2000"),
vec!["foo ", "{{ bar }}", " 2000"]);
assert_eq!(split_blocks("foo {{- bar -}} 2000"),
vec!["foo", " {{- bar -}} ", "2000"]);
assert_eq!(split_blocks("foo \n{{- bar }} 2000"),
vec!["foo", " \n{{- bar }}", " 2000"]);
assert_eq!(split_blocks("foo {% bar %} 2000"),
vec!["foo ", "{% bar %}", " 2000"]);
assert_eq!(split_blocks("foo {%- bar -%} 2000"),
vec!["foo", " {%- bar -%} ", "2000"]);
assert_eq!(split_blocks("foo \n{%- bar %} 2000"),
vec!["foo", " \n{%- bar %}", " 2000"]);
}

#[test]
fn test_split_atom() {
Expand Down Expand Up @@ -182,6 +207,12 @@ fn test_tokenize() {
StringLiteral("world".to_owned())],
"{{hello 'world'}}".to_owned()),
Raw(" test".to_owned())]);
assert_eq!(tokenize("wat \n {{-hello 'world'-}} test").unwrap(),
vec![Raw("wat".to_owned()),
Expression(vec![Identifier("hello".to_owned()),
StringLiteral("world".to_owned())],
" \n {{-hello 'world'-}} ".to_owned()),
Raw("test".to_owned())]);
}

#[test]
Expand Down
66 changes: 66 additions & 0 deletions tests/whitespace_control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
extern crate liquid;

use liquid::LiquidOptions;
use liquid::Renderable;
use liquid::Context;
use liquid::parse;
use std::default::Default;

macro_rules! compare {
($input:expr, $output:expr) => {
let input = $input.replace("…", " ");
let expected = $output.replace("…", " ");
let options: LiquidOptions = Default::default();
let template = parse(&input, options).unwrap();

let mut data = Context::new();
let output = template.render(&mut data);
assert_eq!(output.unwrap(), Some(expected));
}
}

#[test]
pub fn no_whitespace_control() {
compare!("
topic1
……{% assign foo = \"bar\" %}
……{% if foo %}
…………-……{{ foo }}
……{% endif %}
",
"
topic1
……
……
…………-……bar
……
");
}

#[test]
pub fn simple_whitespace_control() {
compare!("
topic1
……{% assign foo = \"bar\" -%}
……{% if foo -%}
…………-……{{- foo }}
……{%- endif %}
",
"
topic1
……-bar
");
}

#[test]
pub fn double_sided_whitespace_control() {
compare!("
topic1
……{%- assign foo = \"bar\" -%}
……-……{{- foo -}}……
",
"
topic1-bar\
");
}