Skip to content

Commit

Permalink
Passing v1.1.2 of the Mustache spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvande committed Mar 20, 2011
1 parent 2baf789 commit 8349597
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
12 changes: 9 additions & 3 deletions lib/mustache/generator.rb
Expand Up @@ -90,7 +90,7 @@ def compile!(exp)


# Callback fired when the compiler finds a section token. We're # Callback fired when the compiler finds a section token. We're
# passed the section name and the array of tokens. # passed the section name and the array of tokens.
def on_section(name, content, raw) def on_section(name, content, raw, delims)
# Convert the tokenized content of this section into a Ruby # Convert the tokenized content of this section into a Ruby
# string we can use. # string we can use.
code = compile(content) code = compile(content)
Expand All @@ -102,7 +102,13 @@ def on_section(name, content, raw)
if v == true if v == true
#{code} #{code}
elsif v.is_a?(Proc) elsif v.is_a?(Proc)
Mustache::Template.new(v.call(#{raw.inspect}).to_s).render(ctx.dup) t = Mustache::Template.new(v.call(#{raw.inspect}).to_s)
def t.tokens(src=@source)
p = Parser.new
p.otag, p.ctag = #{delims.inspect}
p.compile(src)
end
t.render(ctx.dup)
else else
# Shortcut when passed non-array # Shortcut when passed non-array
v = [v] unless v.is_a?(Array) || defined?(Enumerator) && v.is_a?(Enumerator) v = [v] unless v.is_a?(Array) || defined?(Enumerator) && v.is_a?(Enumerator)
Expand All @@ -115,7 +121,7 @@ def on_section(name, content, raw)


# Fired when we find an inverted section. Just like `on_section`, # Fired when we find an inverted section. Just like `on_section`,
# we're passed the inverted section name and the array of tokens. # we're passed the inverted section name and the array of tokens.
def on_inverted_section(name, content, raw) def on_inverted_section(name, content, raw, _)
# Convert the tokenized content of this section into a Ruby # Convert the tokenized content of this section into a Ruby
# string we can use. # string we can use.
code = compile(content) code = compile(content)
Expand Down
2 changes: 1 addition & 1 deletion lib/mustache/parser.rb
Expand Up @@ -157,7 +157,7 @@ def scan_tags
when '/' when '/'
section, pos, result = @sections.pop section, pos, result = @sections.pop
raw = @scanner.pre_match[pos[3]...pre_match_position] + padding raw = @scanner.pre_match[pos[3]...pre_match_position] + padding
(@result = result).last << raw (@result = result).last << raw << [self.otag, self.ctag]


if section.nil? if section.nil?
error "Closing unopened #{content.inspect}" error "Closing unopened #{content.inspect}"
Expand Down
15 changes: 10 additions & 5 deletions test/parser_test.rb
Expand Up @@ -35,7 +35,8 @@ def test_parser
[:static, " <li><strong>"], [:static, " <li><strong>"],
[:mustache, :etag, [:mustache, :fetch, ["name"]]], [:mustache, :etag, [:mustache, :fetch, ["name"]]],
[:static, "</strong></li>\n"]], [:static, "</strong></li>\n"]],
%Q' <li><strong>{{name}}</strong></li>\n'], %Q' <li><strong>{{name}}</strong></li>\n',
%w[{{ }}]],
[:mustache, [:mustache,
:section, :section,
[:mustache, :fetch, ["link"]], [:mustache, :fetch, ["link"]],
Expand All @@ -45,14 +46,17 @@ def test_parser
[:static, "\">"], [:static, "\">"],
[:mustache, :etag, [:mustache, :fetch, ["name"]]], [:mustache, :etag, [:mustache, :fetch, ["name"]]],
[:static, "</a></li>\n"]], [:static, "</a></li>\n"]],
%Q' <li><a href="{{url}}">{{name}}</a></li>\n']], %Q' <li><a href="{{url}}">{{name}}</a></li>\n',
%Q'{{#first}}\n <li><strong>{{name}}</strong></li>\n{{/first}}\n{{#link}}\n <li><a href="{{url}}">{{name}}</a></li>\n{{/link}}\n'], %w[{{ }}]]],
%Q'{{#first}}\n <li><strong>{{name}}</strong></li>\n{{/first}}\n{{#link}}\n <li><a href="{{url}}">{{name}}</a></li>\n{{/link}}\n',
%w[{{ }}]],
[:static, "\n"], [:static, "\n"],
[:mustache, [:mustache,
:section, :section,
[:mustache, :fetch, ["empty"]], [:mustache, :fetch, ["empty"]],
[:multi, [:static, "<p>The list is empty.</p>\n"]], [:multi, [:static, "<p>The list is empty.</p>\n"]],
%Q'<p>The list is empty.</p>\n']] %Q'<p>The list is empty.</p>\n',
%w[{{ }}]]]


assert_equal expected, tokens assert_equal expected, tokens
end end
Expand All @@ -66,7 +70,8 @@ def test_raw_content_and_whitespace
:section, :section,
[:mustache, :fetch, ["list"]], [:mustache, :fetch, ["list"]],
[:multi, [:static, "\t"]], [:multi, [:static, "\t"]],
"\t"]] "\t",
%w[{{ }}]]]


assert_equal expected, tokens assert_equal expected, tokens
end end
Expand Down

0 comments on commit 8349597

Please sign in to comment.