Skip to content

Commit

Permalink
Add support for escaping mustaches
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehuda Katz authored and Yehuda Katz committed Dec 28, 2011
1 parent e474e56 commit c79c761
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
14 changes: 14 additions & 0 deletions spec/tokenizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ def tokenize(string)
result[1].should be_token("ID", "foo")
end

it "supports escaping delimiters" do
result = tokenize("{{foo}} \\{{bar}} {{baz}}")
result.should match_tokens(%w(OPEN ID CLOSE CONTENT CONTENT OPEN ID CLOSE))

result[4].should be_token("CONTENT", "{{bar}} ")
end

it "supports escaping a triple stash" do
result = tokenize("{{foo}} \\{{{bar}}} {{baz}}")
result.should match_tokens(%w(OPEN ID CLOSE CONTENT CONTENT OPEN ID CLOSE))

result[4].should be_token("CONTENT", "{{{bar}}} ")
end

it "tokenizes a simple path" do
result = tokenize("{{foo/bar}}")
result.should match_tokens(%w(OPEN ID SEP ID CLOSE))
Expand Down
17 changes: 12 additions & 5 deletions src/handlebars.l
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@

%x mu
%x mu emu

%%

[^\x00]*?/("{{") { this.begin("mu"); if (yytext) return 'CONTENT'; }
[^\x00]*?/("{{") {
if(yytext.slice(-1) !== "\\") this.begin("mu");
if(yytext.slice(-1) === "\\") yytext = yytext.substr(0,yyleng-1), this.begin("emu");
if(yytext) return 'CONTENT';
}

[^\x00]+ { return 'CONTENT'; }

<emu>[^\x00]{2,}?/("{{") { this.popState(); return 'CONTENT'; }

<mu>"{{>" { return 'OPEN_PARTIAL'; }
<mu>"{{#" { return 'OPEN_BLOCK'; }
<mu>"{{/" { return 'OPEN_ENDBLOCK'; }
<mu>"{{^" { return 'OPEN_INVERSE'; }
<mu>"{{"\s*"else" { return 'OPEN_INVERSE'; }
<mu>"{{{" { return 'OPEN_UNESCAPED'; }
<mu>"{{&" { return 'OPEN_UNESCAPED'; }
<mu>"{{!"[\s\S]*?"}}" { yytext = yytext.substr(3,yyleng-5); this.begin("INITIAL"); return 'COMMENT'; }
<mu>"{{!"[\s\S]*?"}}" { yytext = yytext.substr(3,yyleng-5); this.popState(); return 'COMMENT'; }
<mu>"{{" { return 'OPEN'; }

<mu>"=" { return 'EQUALS'; }
<mu>"."/[} ] { return 'ID'; }
<mu>".." { return 'ID'; }
<mu>[\/.] { return 'SEP'; }
<mu>\s+ { /*ignore whitespace*/ }
<mu>"}}}" { this.begin("INITIAL"); return 'CLOSE'; }
<mu>"}}" { this.begin("INITIAL"); return 'CLOSE'; }
<mu>"}}}" { this.popState(); return 'CLOSE'; }
<mu>"}}" { this.popState(); return 'CLOSE'; }
<mu>'"'("\\"["]|[^"])*'"' { yytext = yytext.substr(1,yyleng-2).replace(/\\"/g,'"'); return 'STRING'; }
<mu>"true"/[}\s] { return 'BOOLEAN'; }
<mu>"false"/[}\s] { return 'BOOLEAN'; }
Expand Down

0 comments on commit c79c761

Please sign in to comment.