Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

1.9.3+ YAML compatibility #143

Merged
merged 5 commits into from

8 participants

@steakknife

No description provided.

@steakknife steakknife referenced this pull request
Closed

Fix for Ruby 1.9.3-rc1 #116

@gcatlin

Please merge!

@Tolmark12

Any chance this is going to be merged?

@patmcnally

+1 on this, I got bit by this issue again.

@mdekmetzian

+1 , getting sick of having to patch mustache on new deployments

@jtara

Shouldn't the test be for the existence of YAML.load_stream (and/or YAML/each_document), rather than for Ruby 1.9.3?

@locks locks merged commit 4b1e24b into mustache:master
@locks
Owner

Merging this as in in favor of #116.

@bobthecow

I'm with @jtara. Detect features, not versions.

@locks
Owner

1.8.7 support will have to be discussed going forward anyway, so this code will be reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 24, 2012
  1. 1.9.3+ YAML compatibility

    Barry Allard authored
  2. Merge pull request #1 from steakknife/develop

    Barry Allard authored
    1.9.3+ YAML compatibility
  3. oh cmon ruby, everything should be an expression

    Barry Allard authored
  4. added corrections from vovik

    Barry Allard authored
  5. [feature] list tags, sections and partials

    Barry Allard authored
This page is out of date. Refresh to see the latest.
View
10 bin/mustache
@@ -73,8 +73,14 @@ class Mustache
yaml = $2.strip
template = doc.sub($1, '')
- YAML.each_document(yaml) do |data|
- puts Mustache.render(template, data)
+ if RUBY_VERSION >= '1.9.3'
+ YAML.load_stream(yaml) do |data|
+ puts Mustache.render(template, data)
+ end
+ else
+ YAML.each_document(yaml) do |data|
+ puts Mustache.render(template, data)
+ end
end
else
puts Mustache.render(doc)
View
116 lib/mustache/blah.rb
@@ -0,0 +1,116 @@
+require 'mustache'
+
+class Mustache::Template
+ def token_names
+ def recursor(toks, section)
+ toks.map do |token|
+ next unless token.is_a? Array
+ if token[0] == :mustache && [:etag,:utag].include? token[1]
+ (section + [token[2][2][0]]).join '.'
+ elsif token[0] == :mustache && [:section,:inverted_section].include? token[1]
+ recursor(token[4], section + [token[2][2][0]])
+ else
+ recursor(token, section)
+ end
+ end
+ end
+ recursor(tokens, []).flatten.reject(&:nil?).uniq
+ end
+
+ def section_names
+ def recursor(toks, section)
+ sections = []
+ toks.each do |token|
+ next unless token.is_a? Array
+ if token[0] == :mustache && [:section,:inverted_section].include? token[1]
+ new_section = section + [token[2][2][0]]
+ sections += [ new_section.join('.') ] + recursor(token[4], new_section)
+ else
+ sections += recursor(token, section)
+ end
+ end
+ sections
+ end
+ recursor(tokens,[]).reject(&:nil?).uniq
+ end
+
+ def partial_names
+ def recursor(toks)
+ partials = []
+ toks.each do |token|
+ next unless token.is_a? Array
+ partials += if token[0..1] == [:mustache, :partial]
+ [token[2]] # partial here
+ else
+ recursor(token)
+ end
+ end
+ partials
+ end
+ recursor(tokens).reject(&:nil?).uniq
+ end
+
+end
+
+if __FILE__ == $0
+ require "test/unit"
+
+ class TestMustacheTokenNames < Test::Unit::TestCase
+
+ def setup
+ @template = Mustache::Template.new(@@template_text ||= DATA.read)
+ end
+
+ def test_token_names
+ assert_equal(@template.token_names,
+ [ "yourname",
+ "HOME",
+ "friend.name",
+ "friend.morr.word",
+ "friend.morr.up",
+ "friend.morr.awesomesauce",
+ "friend.morr.hiss",
+ "friend.notinmorr",
+ "friend.person",
+ "love",
+ "triplestash"
+ ]
+ )
+ end
+
+ def test_partial_names
+ assert_equal(@template.partial_names, ["partial1", "partial2"])
+ end
+
+ def test_section_names
+ assert_equal(@template.section_names, ["friend", "friend.morr"])
+ end
+ end
+end
+
+__END__
+Hi there {{yourname}}. Your home directory is {{HOME}}.
+
+{{#friend}}
+Your friend is named {{name}}
+ {{#morr}}
+ Hey {{word}} {{up}} {{{awesomesauce}}}.
+ {{/morr}}
+ {{^morr}}
+ Booooo. {{hiss}}
+ {{/morr}}
+ {{notinmorr}}
+ {{> partial1}}
+{{/friend}}
+{{^friend}}
+You have no friends, {{person}}. You suck.
+{{/friend}}
+
+{{> partial2}}
+{{! comments are awesome }}
+
+{{={% %}=}}
+
+{%love%}
+{%={{ }}=%}
+{{{triplestash}}}
View
52 lib/mustache/template.rb
@@ -54,5 +54,57 @@ def compile(src = @source)
def tokens(src = @source)
Parser.new.compile(src)
end
+
+ # Simple recursive iterator for tokens
+ def self.recursor(toks, section, &block)
+ toks.map do |token|
+ next unless token.is_a? Array
+ if token[0] == :mustache
+ new_token, new_section, result, stop = yield(token, section)
+ [ result ] + ( stop ? [] : recursor(new_token, new_section, &block))
+ else
+ recursor(token, section, &block)
+ end
+ end
+ end
+
+ # Returns an array of tags
+ # Tags that belong to sections will be of the form `section1.tag`
+ def tags
+ Template.recursor(tokens, []) do |token, section|
+ if [:etag, :utag].include?(token[1])
+ [ new_token=nil, new_section=nil, result=((section + [token[2][2][0]]).join('.')), stop=true ]
+ elsif [:section, :inverted_section].include?(token[1])
+ [ new_token=token[4], new_section=(section + [token[2][2][0]]), result=nil, stop=false ]
+ else
+ [ new_token=token, new_section=section, result=nil, stop=false ]
+ end
+ end.flatten.reject(&:nil?).uniq
+ end
+
+ # Returns an array of sections
+ # Sections that belong to other sections will be of the form `section1.childsection`
+ def sections
+ Template.recursor(tokens, []) do |token, section|
+ if [:section, :inverted_section].include?(token[1])
+ new_section=(section + [token[2][2][0]])
+ [ new_token=token[4], new_section, result=new_section.join('.'), stop=false ]
+ else
+ [ new_token=token, new_section=section, result=nil, stop=false ]
+ end
+ end.flatten.reject(&:nil?).uniq
+ end
+
+ # Returns an array of partials
+ # Partials that belong to sections are included, but the section name is not preserved
+ def partials
+ Template.recursor(tokens, []) do |token, section|
+ if token[1] == :partial
+ [ new_token=token, new_section=section, result=token[2], stop=true ]
+ else
+ [ new_token=token, new_section=section, result=nil, stop=false ]
+ end
+ end.flatten.reject(&:nil?).uniq
+ end
end
end
View
4 man/mustache.1
@@ -23,7 +23,7 @@ Mustache is a logic\-less templating system for HTML, config files, anything\.
The \fBmustache\fR command processes a Mustache template preceded by YAML frontmatter from standard input and prints one or more documents to standard output\.
.
.P
-YAML frontmatter beings with \fB\-\-\-\fR on a single line, followed by YAML, ending with another \fB\-\-\-\fR on a single line, e\.g\.
+YAML frontmatter begins with \fB\-\-\-\fR on a single line, followed by YAML, ending with another \fB\-\-\-\fR on a single line, e\.g\.
.
.IP "" 4
.
@@ -124,7 +124,7 @@ Print the compiled Ruby version of a given template\. This is the code that is a
.
.TP
\fB\-t\fR, \fB\-\-tokens\fR
-Print the tokenized form of a given Mustache template\. This can be used to understand how Mustache parses a template\. The tokens are handed to a generator which compiles them into a Ruby string\. Syntax errors and confused tags, therefor, can probably be identified by examining the tokens produced\.
+Print the tokenized form of a given Mustache template\. This can be used to understand how Mustache parses a template\. The tokens are handed to a generator which compiles them into a Ruby string\. Syntax errors and confused tags, therefore, can probably be identified by examining the tokens produced\.
.
.SH "INSTALLATION"
If you have RubyGems installed:
View
4 man/mustache.1.html
@@ -90,7 +90,7 @@ <h2 id="DESCRIPTION">DESCRIPTION</h2>
frontmatter from standard input and prints one or more documents to
standard output.</p>
-<p>YAML frontmatter beings with <code>---</code> on a single line, followed by YAML,
+<p>YAML frontmatter begins with <code>---</code> on a single line, followed by YAML,
ending with another <code>---</code> on a single line, e.g.</p>
<pre><code>---
@@ -165,7 +165,7 @@ <h2 id="OPTIONS">OPTIONS</h2>
<dt><code>-t</code>, <code>--tokens</code></dt><dd><p>Print the tokenized form of a given Mustache template. This can be
used to understand how Mustache parses a template. The tokens are
handed to a generator which compiles them into a Ruby
-string. Syntax errors and confused tags, therefor, can probably be
+string. Syntax errors and confused tags, therefore, can probably be
identified by examining the tokens produced.</p></dd>
</dl>
View
4 man/mustache.1.ron
@@ -17,7 +17,7 @@ The `mustache` command processes a Mustache template preceded by YAML
frontmatter from standard input and prints one or more documents to
standard output.
-YAML frontmatter beings with `---` on a single line, followed by YAML,
+YAML frontmatter begins with `---` on a single line, followed by YAML,
ending with another `---` on a single line, e.g.
---
@@ -90,7 +90,7 @@ YAML frontmatter you provide. It can do a few other things, however.
Print the tokenized form of a given Mustache template. This can be
used to understand how Mustache parses a template. The tokens are
handed to a generator which compiles them into a Ruby
- string. Syntax errors and confused tags, therefor, can probably be
+ string. Syntax errors and confused tags, therefore, can probably be
identified by examining the tokens produced.
View
25 test/fixtures/simply_complicated.mustache
@@ -0,0 +1,25 @@
+Hi there {{yourname}}. Your home directory is {{HOME}}.
+
+{{#friend}}
+Your friend is named {{name}}
+ {{#morr}}
+ Hey {{word}} {{up}} {{{awesomesauce}}}.
+ {{/morr}}
+ {{^morr}}
+ Booooo. {{hiss}}
+ {{/morr}}
+ {{notinmorr}}
+ {{> partial1}}
+{{/friend}}
+{{^friend}}
+You have no friends, {{person}}. You suck.
+{{/friend}}
+
+{{> partial2}}
+{{! comments are awesome }}
+
+{{={% %}=}}
+
+{%love%}
+{%={{ }}=%}
+{{{triplestash}}}
View
33 test/template_test.rb
@@ -18,3 +18,36 @@ def test_token_with_source
assert_equal [:multi, [:static, "bar"]], Mustache::Template.new("foo").tokens("bar")
end
end
+
+class TemplateTest2 < Test::Unit::TestCase
+ def setup
+ @@template_text ||= File.read(File.dirname(__FILE__) + "/fixtures/simply_complicated.mustache")
+ @template = Mustache::Template.new(@@template_text)
+ end
+
+ def test_tags
+ assert_equal [
+ "yourname",
+ "HOME",
+ "friend.name",
+ "friend.morr.word",
+ "friend.morr.up",
+ "friend.morr.awesomesauce",
+ "friend.morr.hiss",
+ "friend.notinmorr",
+ "friend.person",
+ "love",
+ "triplestash"
+ ], @template.tags
+ end
+
+ def test_partials
+ assert_equal ["partial1", "partial2"], @template.partials
+ end
+
+ def test_sections
+ assert_equal ["friend", "friend.morr"], @template.sections
+ end
+end
+
+
Something went wrong with that request. Please try again.