Skip to content

Commit

Permalink
(feat) added support for left-side whitespace omitting [#10]
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Feb 16, 2015
1 parent e85e229 commit f0529f4
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ impl Helper {
}
}

enum WhiteSpaceOmit {
Left,
Right,
None
}

fn process_whitespace(buf: &String, wso: &mut WhiteSpaceOmit) -> String {
let result = match *wso {
WhiteSpaceOmit::Left => {
buf.trim_left().to_string()
},
WhiteSpaceOmit::Right => {
buf.trim_right().to_string()
},
WhiteSpaceOmit::None => {
buf.clone()
}
};
*wso = WhiteSpaceOmit::None;
result
}

impl Template {
pub fn compile(source: String) -> Result<Template, TemplateError> {
let mut helper_stack: RingBuf<Helper> = RingBuf::new();
Expand All @@ -156,27 +178,28 @@ impl Template {
let mut state = ParserState::Text;

let mut c:usize = 0;
let mut ws_omitter = false;
let mut ws_omitter = WhiteSpaceOmit::None;
let source_len = source.chars().count();
while c < source_len {
let mut slice = source.slice_chars(c, min(c+3, source_len));
if slice == "{{~" {
ws_omitter = true;
ws_omitter = WhiteSpaceOmit::Right;
// read another char and remove ~
slice = source.slice_chars(c, min(c+4, source_len));
c += 1;
}
if slice == "~}}" {
ws_omitter = WhiteSpaceOmit::Left;
c += 1;
slice = source.slice_chars(c, min(c+4, source_len));
}
println!("{}", slice);
state = match slice.replace("~", "").as_slice() {
"{{{" | "{{!" | "{{#" | "{{/" => {
c += 2;
if !buffer.is_empty() {
let mut t = template_stack.front_mut().unwrap();
let buf_clone = if ws_omitter {
ws_omitter = false;
String::from_str(buffer.trim_right())
} else {
buffer.clone()
};
let buf_clone = process_whitespace(&buffer, &mut ws_omitter);
t.elements.push(RawString(buf_clone));
buffer.clear();
}
Expand Down Expand Up @@ -211,12 +234,7 @@ impl Template {
c += 1;
if !buffer.is_empty() {
let mut t = template_stack.front_mut().unwrap();
let buf_clone = if ws_omitter {
ws_omitter = false;
String::from_str(buffer.trim_right())
} else {
buffer.clone()
};
let buf_clone = process_whitespace(&buffer, &mut ws_omitter);
t.elements.push(RawString(buf_clone));
buffer.clear();
}
Expand Down Expand Up @@ -306,11 +324,7 @@ impl Template {

if !buffer.is_empty() {
let mut t = template_stack.front_mut().unwrap();
let buf_clone = if ws_omitter {
String::from_str(buffer.trim_right())
} else {
buffer.clone()
};
let buf_clone = process_whitespace(&buffer, &mut ws_omitter);
t.elements.push(TemplateElement::RawString(buf_clone));
}

Expand Down Expand Up @@ -445,12 +459,13 @@ fn test_parse_error() {
}

#[test]
fn test_white_space_omitter_right() {
let source = "hello {{~world}}".to_string();
fn test_white_space_omitter() {
let source = "hello~ {{~world~}} \n !".to_string();
let t = Template::compile(source).ok().unwrap();

assert_eq!(t.elements.len(), 2);
assert_eq!(t.elements.len(), 3);

assert_eq!(t.elements[0], RawString(String::from_str("hello")));
assert_eq!(t.elements[0], RawString(String::from_str("hello~")));
assert_eq!(t.elements[1], Expression(String::from_str("world")));
assert_eq!(t.elements[2], RawString(String::from_str("!")));
}

0 comments on commit f0529f4

Please # to comment.