Skip to content

Commit

Permalink
Add block comment syntax:
Browse files Browse the repository at this point in the history
{{!-- can contain
      {{handlebars expressions}} --}}
  • Loading branch information
wycats committed Oct 15, 2012
1 parent 8ed7a1e commit a927a9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions spec/tokenizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ def tokenize(string)
result[1].should be_token("COMMENT", " this is a comment ")
end

it "tokenizes a block comment as 'COMMENT'" do
result = tokenize("foo {{!-- this is a {{comment}} --}} bar {{ baz }}")
result.should match_tokens(%w(CONTENT COMMENT CONTENT OPEN ID CLOSE))
result[1].should be_token("COMMENT", " this is a {{comment}} ")
end

it "tokenizes a block comment with whitespace as 'COMMENT'" do
result = tokenize("foo {{!-- this is a\n{{comment}}\n--}} bar {{ baz }}")
result.should match_tokens(%w(CONTENT COMMENT CONTENT OPEN ID CLOSE))
result[1].should be_token("COMMENT", " this is a\n{{comment}}\n")
end

it "tokenizes open and closing blocks as 'OPEN_BLOCK ID CLOSE ... OPEN_ENDBLOCK ID CLOSE'" do
result = tokenize("{{#foo}}content{{/foo}}")
result.should match_tokens(%w(OPEN_BLOCK ID CLOSE CONTENT OPEN_ENDBLOCK ID CLOSE))
Expand Down
5 changes: 4 additions & 1 deletion src/handlebars.l
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

%x mu emu
%x mu emu com

%%

Expand All @@ -17,13 +17,16 @@
return 'CONTENT';
}

<com>[\s\S]*?"--}}" { yytext = yytext.substr(0, yyleng-4); this.popState(); return 'COMMENT'; }

<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>"{{!--" { this.popState(); this.begin('com'); }
<mu>"{{!"[\s\S]*?"}}" { yytext = yytext.substr(3,yyleng-5); this.popState(); return 'COMMENT'; }
<mu>"{{" { return 'OPEN'; }

Expand Down

0 comments on commit a927a9b

Please sign in to comment.