mustache 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/README.md +30 -197
  2. data/Rakefile +87 -46
  3. data/bin/mustache +5 -29
  4. data/lib/mustache/context.rb +54 -11
  5. data/lib/mustache/template.rb +2 -14
  6. data/lib/mustache/version.rb +1 -1
  7. data/man/mustache.1 +138 -0
  8. data/man/mustache.1.html +168 -0
  9. data/man/mustache.1.ron +94 -0
  10. data/man/mustache.5 +364 -0
  11. data/man/mustache.5.html +300 -0
  12. data/man/mustache.5.ron +220 -0
  13. data/test/helper.rb +1 -0
  14. data/test/mustache_test.rb +19 -16
  15. metadata +32 -65
  16. data/.gitignore +0 -1
  17. data/.kick +0 -26
  18. data/CONTRIBUTORS +0 -7
  19. data/HISTORY.md +0 -76
  20. data/benchmarks/complex.erb +0 -15
  21. data/benchmarks/complex.haml +0 -10
  22. data/benchmarks/helper.rb +0 -20
  23. data/benchmarks/simple.erb +0 -5
  24. data/benchmarks/speed.rb +0 -76
  25. data/contrib/mustache.vim +0 -69
  26. data/contrib/tpl-mode.el +0 -274
  27. data/examples/comments.mustache +0 -1
  28. data/examples/comments.rb +0 -14
  29. data/examples/complex_view.mustache +0 -16
  30. data/examples/complex_view.rb +0 -34
  31. data/examples/delimiters.mustache +0 -6
  32. data/examples/delimiters.rb +0 -22
  33. data/examples/double_section.mustache +0 -7
  34. data/examples/double_section.rb +0 -14
  35. data/examples/escaped.mustache +0 -1
  36. data/examples/escaped.rb +0 -14
  37. data/examples/inner_partial.mustache +0 -1
  38. data/examples/inner_partial.txt +0 -1
  39. data/examples/namespaced.mustache +0 -1
  40. data/examples/namespaced.rb +0 -25
  41. data/examples/partial_with_module.mustache +0 -3
  42. data/examples/partial_with_module.rb +0 -37
  43. data/examples/passenger.conf +0 -5
  44. data/examples/passenger.rb +0 -27
  45. data/examples/simple.mustache +0 -5
  46. data/examples/simple.rb +0 -26
  47. data/examples/template_partial.mustache +0 -2
  48. data/examples/template_partial.rb +0 -18
  49. data/examples/template_partial.txt +0 -4
  50. data/examples/unescaped.mustache +0 -1
  51. data/examples/unescaped.rb +0 -14
@@ -11,23 +11,66 @@ class Mustache
11
11
 
12
12
  # A Context represents the context which a Mustache template is
13
13
  # executed within. All Mustache tags reference keys in the Context.
14
- class Context < Hash
14
+ class Context
15
+ # Expect to be passed an instance of `Mustache`.
15
16
  def initialize(mustache)
16
17
  @mustache = mustache
17
- super()
18
+ @stack = [@mustache]
18
19
  end
19
20
 
21
+ # Adds a new object to the context's internal stack.
22
+ #
23
+ # Returns the Context.
24
+ def push(new)
25
+ @stack.unshift(new)
26
+ self
27
+ end
28
+ alias_method :update, :push
29
+
30
+ # Removes the most recently added object from the context's
31
+ # internal stack.
32
+ #
33
+ # Returns the Context.
34
+ def pop
35
+ @stack.shift
36
+ self
37
+ end
38
+
39
+ # Can be used to add a value to the context in a hash-like way.
40
+ #
41
+ # context[:name] = "Chris"
42
+ def []=(name, value)
43
+ push(name => value)
44
+ end
45
+
46
+ # Alias for `fetch`.
20
47
  def [](name)
21
- if has_key?(name)
22
- super
23
- elsif has_key?(name.to_s)
24
- super(name.to_s)
25
- elsif @mustache.respond_to?(name)
26
- @mustache.send(name)
27
- elsif @mustache.raise_on_context_miss?
28
- raise ContextMiss.new("Can't find #{name} in #{@mustache.inspect}")
48
+ fetch(name, nil)
49
+ end
50
+
51
+ # Similar to Hash#fetch, finds a value by `name` in the context's
52
+ # stack. You may specify the default return value by passing a
53
+ # second parameter.
54
+ #
55
+ # If no second parameter is passed (or raise_on_context_miss is
56
+ # set to true), will raise a ContextMiss exception on miss.
57
+ def fetch(name, default = :__raise)
58
+ @stack.each do |frame|
59
+ hash = frame.respond_to?(:has_key?)
60
+
61
+ if hash && frame.has_key?(name)
62
+ return frame[name]
63
+ elsif hash && frame.has_key?(name.to_s)
64
+ return frame[name.to_s]
65
+ elsif !hash && frame.respond_to?(name)
66
+ return frame.__send__(name)
67
+ end
68
+ end
69
+
70
+ if default == :__raise || @mustache.raise_on_context_miss?
71
+ raise ContextMiss.new("Can't find #{name} in #{@stack.inspect}")
29
72
  else
30
- nil
73
+ default
31
74
  end
32
75
  end
33
76
  end
@@ -76,20 +76,8 @@ class Mustache
76
76
  ctxtmp = "ctx#{tmpid}"
77
77
  res << ev(<<-compiled)
78
78
  if v = ctx[#{name}]
79
- v = [v] if v.is_a?(Hash) # shortcut when passed a single hash
80
- if v.respond_to?(:each)
81
- #{ctxtmp} = ctx.dup
82
- begin
83
- r = v.map { |h| ctx.update(h); #{code} }.join
84
- rescue TypeError => e
85
- raise TypeError,
86
- "All elements in {{#{name.to_s[1..-1]}}} are not hashes!"
87
- end
88
- ctx.replace(#{ctxtmp})
89
- r
90
- else
91
- #{code}
92
- end
79
+ v = [v] unless v.is_a?(Array) # shortcut when passed non-array
80
+ v.map { |h| ctx.push(h); c = #{code}; ctx.pop; c }.join
93
81
  end
94
82
  compiled
95
83
  # $' = The string to the right of the last successful match
@@ -1,3 +1,3 @@
1
1
  class Mustache
2
- Version = '0.5.1'
2
+ Version = '0.6.0'
3
3
  end
@@ -0,0 +1,138 @@
1
+ .\" generated with Ron/v0.3
2
+ .\" http://github.com/rtomayko/ron/
3
+ .
4
+ .TH "MUSTACHE" "1" "March 2010" "DEFUNKT" "Mustache Manual"
5
+ .
6
+ .SH "NAME"
7
+ \fBmustache\fR \-\- Mustache processor
8
+ .
9
+ .SH "SYNOPSIS"
10
+ \fBcat data.yml template.mustache | mustache\fR
11
+ .
12
+ .SH "DESCRIPTION"
13
+ Mustache is a logic\-less templating system for HTML, config files,
14
+ anything.
15
+ .
16
+ .P
17
+ The \fBmustache\fR command processes a Mustache template preceded by YAML
18
+ frontmatter from standard input and prints one or more documents to
19
+ standard output.
20
+ .
21
+ .P
22
+ YAML frontmatter beings with \fB---\fR on a single line, followed by YAML,
23
+ ending with another \fB---\fR on a single line, e.g.
24
+ .
25
+ .IP "" 4
26
+ .
27
+ .nf
28
+
29
+ \fB---
30
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
31
+ --- \fR
32
+ .
33
+ .fi
34
+ .
35
+ .IP "" 0
36
+ .
37
+ .P
38
+ If you are unfamiliar with YAML, it is a superset of JSON. Valid JSON
39
+ should work fine.
40
+ .
41
+ .P
42
+ After the frontmatter should come any valid Mustache template. See
43
+ mustache(5) for an overview of Mustache templates.
44
+ .
45
+ .P
46
+ For example:
47
+ .
48
+ .IP "" 4
49
+ .
50
+ .nf
51
+
52
+ \fB{{#names}}
53
+ Hi {{name}}!
54
+ {{/names}} \fR
55
+ .
56
+ .fi
57
+ .
58
+ .IP "" 0
59
+ .
60
+ .P
61
+ Now let's combine them.
62
+ .
63
+ .IP "" 4
64
+ .
65
+ .nf
66
+
67
+ \fB$ cat data.yml
68
+ ---
69
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
70
+ ---
71
+ $ cat template.mustache
72
+ {{#names}}
73
+ Hi {{name}}!
74
+ {{/names}}
75
+
76
+ $ cat data.yml template.mustache | mustache
77
+ Hi chris!
78
+ Hi mark!
79
+ Hi scott!
80
+ \fR
81
+ .
82
+ .fi
83
+ .
84
+ .IP "" 0
85
+ .
86
+ .P
87
+ If you provide multiple YAML documents (as delimited by \fB---\fR), your
88
+ template will be rendered multiple times. Like a mail merge.
89
+ .
90
+ .P
91
+ For example:
92
+ .
93
+ .IP "" 4
94
+ .
95
+ .nf
96
+
97
+ \fB$ cat data.yml
98
+ ---
99
+ name: chris
100
+ ---
101
+ name: mark
102
+ ---
103
+ name: scott
104
+ ---
105
+ $ cat template.mustache
106
+ Hi {{name}}!
107
+
108
+ $ cat data.yml template.mustache | mustache
109
+ Hi chris!
110
+ Hi mark!
111
+ Hi scott!
112
+ \fR
113
+ .
114
+ .fi
115
+ .
116
+ .IP "" 0
117
+ .
118
+ .SH "INSTALLATION"
119
+ If you have RubyGems installed:
120
+ .
121
+ .IP "" 4
122
+ .
123
+ .nf
124
+
125
+ \fBgem install mustache \fR
126
+ .
127
+ .fi
128
+ .
129
+ .IP "" 0
130
+ .
131
+ .SH "COPYRIGHT"
132
+ Mustache is Copyright (C) 2009 Chris Wanstrath
133
+ .
134
+ .P
135
+ Original CTemplate by Google
136
+ .
137
+ .SH "SEE ALSO"
138
+ mustache(5), mustache(7), gem(1),\fIhttp://defunkt.github.com/mustache/\fR
@@ -0,0 +1,168 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv='content-type' value='text/html;charset=utf8'>
5
+ <meta name='generator' value='Ron/v0.3'>
6
+ <title>mustache(1) -- Mustache processor</title>
7
+ <style type='text/css'>
8
+ body {margin:0}
9
+ #man, #man code, #man pre, #man tt, #man kbd, #man samp {
10
+ font-family:consolas,monospace;
11
+ font-size:16px;
12
+ line-height:1.3;
13
+ color:#343331;
14
+ background:#fff; }
15
+ #man { max-width:89ex; text-align:justify; margin:0 25px 25px 25px }
16
+ #man h1, #man h2, #man h3 { color:#232221;clear:left }
17
+ #man h1 { font-size:28px; margin:15px 0 30px 0; text-align:center }
18
+ #man h2 { font-size:18px; margin-bottom:0; margin-top:10px; line-height:1.3; }
19
+ #man h3 { font-size:16px; margin:0 0 0 4ex; }
20
+ #man p, #man ul, #man ol, #man dl, #man pre { margin:0 0 18px 0; }
21
+ #man pre {
22
+ color:#333231;
23
+ background:#edeceb;
24
+ padding:5px 7px;
25
+ margin:0px 0 20px 0;
26
+ border-left:2ex solid #ddd}
27
+ #man pre + h2, #man pre + h3 {
28
+ margin-top:22px;
29
+ }
30
+ #man h2 + pre, #man h3 + pre {
31
+ margin-top:5px;
32
+ }
33
+ #man > p, #man > ul, #man > ol, #man > dl, #man > pre { margin-left:8ex; }
34
+ #man dt { margin:0; clear:left }
35
+ #man dt.flush { float:left; width:8ex }
36
+ #man dd { margin:0 0 0 9ex }
37
+ #man code, #man strong, #man b { font-weight:bold; color:#131211; }
38
+ #man pre code { font-weight:normal; color:#232221; background:inherit }
39
+ #man em, var, u {
40
+ font-style:normal; color:#333231; border-bottom:1px solid #999; }
41
+ #man h1.man-title { display:none; }
42
+ #man ol.man, #man ol.man li { margin:2px 0 10px 0; padding:0;
43
+ float:left; width:33%; list-style-type:none;
44
+ text-transform:uppercase; font-size:18px; color:#999;
45
+ letter-spacing:1px;}
46
+ #man ol.man { width:100%; }
47
+ #man ol.man li.tl { text-align:left }
48
+ #man ol.man li.tc { text-align:center;letter-spacing:4px }
49
+ #man ol.man li.tr { text-align:right }
50
+ #man ol.man a { color:#999 }
51
+ #man ol.man a:hover { color:#333231 }
52
+ </style>
53
+ </head>
54
+ <body>
55
+ <div id='man'>
56
+
57
+ <h1 class='man-title'>mustache(1)</h1>
58
+
59
+ <ol class='head man'>
60
+ <li class='tl'>mustache(1)</li>
61
+ <li class='tc'>Mustache Manual</li>
62
+ <li class='tr'>mustache(1)</li>
63
+ </ol>
64
+
65
+ <h2 id='NAME'>NAME</h2>
66
+ <p><code>mustache</code> -- Mustache processor</p>
67
+ <h2>SYNOPSIS</h2>
68
+
69
+ <p><code>cat data.yml template.mustache | mustache</code></p>
70
+
71
+ <h2>DESCRIPTION</h2>
72
+
73
+ <p>Mustache is a logic-less templating system for HTML, config files,
74
+ anything.</p>
75
+
76
+ <p>The <code>mustache</code> command processes a Mustache template preceded by YAML
77
+ frontmatter from standard input and prints one or more documents to
78
+ standard output.</p>
79
+
80
+ <p>YAML frontmatter beings with <code>---</code> on a single line, followed by YAML,
81
+ ending with another <code>---</code> on a single line, e.g.</p>
82
+
83
+ <pre><code>---
84
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
85
+ ---
86
+ </code></pre>
87
+
88
+ <p>If you are unfamiliar with YAML, it is a superset of JSON. Valid JSON
89
+ should work fine.</p>
90
+
91
+ <p>After the frontmatter should come any valid Mustache template. See
92
+ mustache(5) for an overview of Mustache templates.</p>
93
+
94
+ <p>For example:</p>
95
+
96
+ <pre><code>{{#names}}
97
+ Hi {{name}}!
98
+ {{/names}}
99
+ </code></pre>
100
+
101
+ <p>Now let's combine them.</p>
102
+
103
+ <pre><code>$ cat data.yml
104
+ ---
105
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
106
+ ---
107
+
108
+ $ cat template.mustache
109
+ {{#names}}
110
+ Hi {{name}}!
111
+ {{/names}}
112
+
113
+ $ cat data.yml template.mustache | mustache
114
+ Hi chris!
115
+ Hi mark!
116
+ Hi scott!
117
+ </code></pre>
118
+
119
+ <p>If you provide multiple YAML documents (as delimited by <code>---</code>), your
120
+ template will be rendered multiple times. Like a mail merge.</p>
121
+
122
+ <p>For example:</p>
123
+
124
+ <pre><code>$ cat data.yml
125
+ ---
126
+ name: chris
127
+ ---
128
+ name: mark
129
+ ---
130
+ name: scott
131
+ ---
132
+
133
+ $ cat template.mustache
134
+ Hi {{name}}!
135
+
136
+ $ cat data.yml template.mustache | mustache
137
+ Hi chris!
138
+ Hi mark!
139
+ Hi scott!
140
+ </code></pre>
141
+
142
+ <h2>INSTALLATION</h2>
143
+
144
+ <p>If you have RubyGems installed:</p>
145
+
146
+ <pre><code>gem install mustache
147
+ </code></pre>
148
+
149
+ <h2>COPYRIGHT</h2>
150
+
151
+ <p>Mustache is Copyright (C) 2009 Chris Wanstrath</p>
152
+
153
+ <p>Original CTemplate by Google</p>
154
+
155
+ <h2>SEE ALSO</h2>
156
+
157
+ <p>mustache(5), mustache(7), gem(1),
158
+ <a href="http://defunkt.github.com/mustache/">http://defunkt.github.com/mustache/</a></p>
159
+
160
+ <ol class='foot man'>
161
+ <li class='tl'>DEFUNKT</li>
162
+ <li class='tc'>March 2010</li>
163
+ <li class='tr'>mustache(1)</li>
164
+ </ol>
165
+
166
+ </div>
167
+ </body>
168
+ </html>
@@ -0,0 +1,94 @@
1
+ mustache(1) -- Mustache processor
2
+ =================================
3
+
4
+ ## SYNOPSIS
5
+
6
+ `cat data.yml template.mustache | mustache`
7
+
8
+
9
+ ## DESCRIPTION
10
+
11
+ Mustache is a logic-less templating system for HTML, config files,
12
+ anything.
13
+
14
+ The `mustache` command processes a Mustache template preceded by YAML
15
+ frontmatter from standard input and prints one or more documents to
16
+ standard output.
17
+
18
+ YAML frontmatter beings with `---` on a single line, followed by YAML,
19
+ ending with another `---` on a single line, e.g.
20
+
21
+ ---
22
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
23
+ ---
24
+
25
+ If you are unfamiliar with YAML, it is a superset of JSON. Valid JSON
26
+ should work fine.
27
+
28
+ After the frontmatter should come any valid Mustache template. See
29
+ mustache(5) for an overview of Mustache templates.
30
+
31
+ For example:
32
+
33
+ {{#names}}
34
+ Hi {{name}}!
35
+ {{/names}}
36
+
37
+ Now let's combine them.
38
+
39
+ $ cat data.yml
40
+ ---
41
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
42
+ ---
43
+
44
+ $ cat template.mustache
45
+ {{#names}}
46
+ Hi {{name}}!
47
+ {{/names}}
48
+
49
+ $ cat data.yml template.mustache | mustache
50
+ Hi chris!
51
+ Hi mark!
52
+ Hi scott!
53
+
54
+ If you provide multiple YAML documents (as delimited by `---`), your
55
+ template will be rendered multiple times. Like a mail merge.
56
+
57
+ For example:
58
+
59
+ $ cat data.yml
60
+ ---
61
+ name: chris
62
+ ---
63
+ name: mark
64
+ ---
65
+ name: scott
66
+ ---
67
+
68
+ $ cat template.mustache
69
+ Hi {{name}}!
70
+
71
+ $ cat data.yml template.mustache | mustache
72
+ Hi chris!
73
+ Hi mark!
74
+ Hi scott!
75
+
76
+
77
+ ## INSTALLATION
78
+
79
+ If you have RubyGems installed:
80
+
81
+ gem install mustache
82
+
83
+
84
+ ## COPYRIGHT
85
+
86
+ Mustache is Copyright (C) 2009 Chris Wanstrath
87
+
88
+ Original CTemplate by Google
89
+
90
+
91
+ ## SEE ALSO
92
+
93
+ mustache(5), mustache(7), gem(1),
94
+ <http://defunkt.github.com/mustache/>