handlebar 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/handlebar.gemspec +4 -3
- data/lib/handlebar/support.rb +27 -0
- data/lib/handlebar/template.rb +13 -19
- data/test/test_handlebar_support.rb +47 -0
- data/test/test_handlebar_template.rb +20 -1
- metadata +4 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/handlebar.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "handlebar"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Scott Tadman"]
|
12
|
-
s.date = "
|
12
|
+
s.date = "2012-07-18"
|
13
13
|
s.description = "A simple text templating system"
|
14
14
|
s.email = "github@tadman.ca"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -29,12 +29,13 @@ Gem::Specification.new do |s|
|
|
29
29
|
"notes/example.hb",
|
30
30
|
"test/helper.rb",
|
31
31
|
"test/test_handlebar.rb",
|
32
|
+
"test/test_handlebar_support.rb",
|
32
33
|
"test/test_handlebar_template.rb"
|
33
34
|
]
|
34
35
|
s.homepage = "http://github.com/twg/handlebar"
|
35
36
|
s.licenses = ["MIT"]
|
36
37
|
s.require_paths = ["lib"]
|
37
|
-
s.rubygems_version = "1.8.
|
38
|
+
s.rubygems_version = "1.8.24"
|
38
39
|
s.summary = "Simple text tempating system"
|
39
40
|
|
40
41
|
if s.respond_to? :specification_version then
|
data/lib/handlebar/support.rb
CHANGED
@@ -42,5 +42,32 @@ module Handlebar::Support
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
+
def variable_stack(variables, force_as_array = true)
|
46
|
+
case (variables)
|
47
|
+
when Hash
|
48
|
+
remapped = Hash[
|
49
|
+
variables.collect do |k, v|
|
50
|
+
[ k ? k.to_sym : k, variable_stack(v, false) ]
|
51
|
+
end
|
52
|
+
]
|
53
|
+
|
54
|
+
if (default = variables.default)
|
55
|
+
remapped.default = default
|
56
|
+
end
|
57
|
+
|
58
|
+
if (default_proc = variables.default_proc)
|
59
|
+
remapped.default_proc = default_proc
|
60
|
+
end
|
61
|
+
|
62
|
+
remapped
|
63
|
+
when Array
|
64
|
+
variables.collect do |v|
|
65
|
+
variable_stack(v, false)
|
66
|
+
end
|
67
|
+
else
|
68
|
+
force_as_array ? [ variables ] : variables
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
45
72
|
extend self
|
46
73
|
end
|
data/lib/handlebar/template.rb
CHANGED
@@ -3,6 +3,8 @@ class Handlebar::Template
|
|
3
3
|
|
4
4
|
TOKEN_REGEXP = /((?:[^\{]|\{[^\{]|\{\{\{)+)|\{\{\s*([\&\%\$\.\:\*\/\=]|\?\!?)?([^\}]*)\}\}/.freeze
|
5
5
|
TOKEN_TRIGGER = /\{\{/.freeze
|
6
|
+
|
7
|
+
TO_YAML_PROPERTIES = %w[ @content @escape_method ].freeze
|
6
8
|
|
7
9
|
# == Utility Classes ======================================================
|
8
10
|
|
@@ -24,7 +26,7 @@ class Handlebar::Template
|
|
24
26
|
class RecursionError < Exception; end
|
25
27
|
|
26
28
|
# == Class Methods ========================================================
|
27
|
-
|
29
|
+
|
28
30
|
# == Instance Methods =====================================================
|
29
31
|
|
30
32
|
def initialize(content, options = nil)
|
@@ -61,20 +63,8 @@ class Handlebar::Template
|
|
61
63
|
end
|
62
64
|
|
63
65
|
def render(variables = nil, templates = nil, parents = nil)
|
64
|
-
variables =
|
65
|
-
|
66
|
-
when Array
|
67
|
-
variables
|
68
|
-
when Hash
|
69
|
-
_variables = variables
|
70
|
-
|
71
|
-
Hash.new do |h, k|
|
72
|
-
h[k] = k ? (_variables[k.to_sym] || _variables[k.to_s]) : _variables[k]
|
73
|
-
end
|
74
|
-
else
|
75
|
-
[ variables ]
|
76
|
-
end
|
77
|
-
|
66
|
+
variables = Handlebar::Support.variable_stack(variables, true)
|
67
|
+
|
78
68
|
if (templates)
|
79
69
|
# Unless the template options have already been processed, mapping
|
80
70
|
# will need to be performed.
|
@@ -206,8 +196,6 @@ class Handlebar::Template
|
|
206
196
|
sections and sections[tag] = true
|
207
197
|
when '?', '?!'
|
208
198
|
# Defines start of a ?conditional
|
209
|
-
|
210
|
-
stack[-1][2][tag.inspect]
|
211
199
|
|
212
200
|
# The stack will inherit the variable assignment locations from the
|
213
201
|
# existing stack layer.
|
@@ -277,11 +265,17 @@ class Handlebar::Template
|
|
277
265
|
true
|
278
266
|
end
|
279
267
|
|
280
|
-
def
|
268
|
+
def to_yaml_properties
|
269
|
+
TO_YAML_PROPERTIES
|
270
|
+
end
|
271
|
+
|
272
|
+
def psych_to_yaml(dump)
|
273
|
+
# Avoid serializing the generated proc by moving it to a temporary
|
274
|
+
# variable for the duration of this operation.
|
281
275
|
_proc, @_proc = @_proc, nil
|
282
276
|
|
283
277
|
super(dump)
|
284
|
-
|
278
|
+
|
285
279
|
@_proc = _proc
|
286
280
|
|
287
281
|
dump
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
class TestHandlebarSupport < Test::Unit::TestCase
|
6
|
+
def test_variable_stack
|
7
|
+
test = { :test => [ { :a => 'a', :b => 'b' }, { :c => 'c' } ] }
|
8
|
+
|
9
|
+
variables = Handlebar::Support.variable_stack(test)
|
10
|
+
|
11
|
+
assert_equal test, variables
|
12
|
+
|
13
|
+
assert_equal 'a', variables[:test][0][:a]
|
14
|
+
assert_equal 'b', variables[:test][0][:b]
|
15
|
+
assert_equal 'c', variables[:test][1][:c]
|
16
|
+
|
17
|
+
test = { 'test' => [ { 'a' => :a, 'b' => :b }, { 'c' => :c } ] }
|
18
|
+
|
19
|
+
variables = Handlebar::Support.variable_stack(test)
|
20
|
+
|
21
|
+
assert_equal :a, variables[:test][0][:a]
|
22
|
+
assert_equal :b, variables[:test][0][:b]
|
23
|
+
assert_equal :c, variables[:test][1][:c]
|
24
|
+
|
25
|
+
assert_equal 'test', Handlebar::Support.variable_stack('test', false)
|
26
|
+
assert_equal [ 'test' ], Handlebar::Support.variable_stack('test')
|
27
|
+
assert_equal [ 'test' ], Handlebar::Support.variable_stack([ 'test' ], false)
|
28
|
+
assert_equal [ 'test' ], Handlebar::Support.variable_stack([ 'test' ])
|
29
|
+
|
30
|
+
variables = Handlebar::Support.variable_stack(:head => [ { :tag => 'meta' }, { :tag => 'link' } ])
|
31
|
+
|
32
|
+
assert_equal 'meta', variables[:head][0][:tag]
|
33
|
+
assert_equal 'link', variables[:head][1][:tag]
|
34
|
+
|
35
|
+
test = { 'top' => { 'layer' => 'top', 't' => 'top', 'middle' => { 'layer' => 'middle', 'm' => 'middle', 'bottom' => { 'layer' => 'bottom', 'b' => 'bottom' } } } }
|
36
|
+
|
37
|
+
variables = Handlebar::Support.variable_stack(test)
|
38
|
+
|
39
|
+
assert_equal 'top', variables[:top][:t]
|
40
|
+
assert_equal 'middle', variables[:top][:middle][:m]
|
41
|
+
assert_equal 'bottom', variables[:top][:middle][:bottom][:b]
|
42
|
+
|
43
|
+
assert_equal 'top', variables[:top][:layer]
|
44
|
+
assert_equal 'middle', variables[:top][:middle][:layer]
|
45
|
+
assert_equal 'bottom', variables[:top][:middle][:bottom][:layer]
|
46
|
+
end
|
47
|
+
end
|
@@ -39,15 +39,34 @@ class TestHandlebarTemplate < Test::Unit::TestCase
|
|
39
39
|
assert_equal 'false', template.render
|
40
40
|
assert_equal 'true', template.render(:boolean => true)
|
41
41
|
assert_equal 'false', template.render(:boolean => false)
|
42
|
-
|
42
|
+
|
43
|
+
template = Handlebar::Template.new('{{?boolean}}{{true}}{{/boolean}}{{?!boolean}}{{false}}{{/boolean}}')
|
44
|
+
|
45
|
+
assert_equal '', template.render
|
46
|
+
assert_equal 'TRUE', template.render(:boolean => true, :true => 'TRUE')
|
47
|
+
assert_equal 'FALSE', template.render(:boolean => false, :false => 'FALSE')
|
43
48
|
|
49
|
+
template = Handlebar::Template.new('{{?boolean}}{{/boolean}}{{?!boolean}}{{/boolean}}')
|
50
|
+
|
51
|
+
assert_equal '', template.render
|
52
|
+
assert_equal '', template.render(:boolean => true)
|
53
|
+
assert_equal '', template.render(:boolean => false)
|
54
|
+
end
|
55
|
+
|
44
56
|
def test_sectioned_templates
|
45
57
|
template = Handlebar::Template.new('<head>{{:head}}<{{tag}}>{{/}}</head>')
|
46
58
|
|
47
59
|
assert_equal '<head><meta></head>', template.render(:head => 'meta')
|
60
|
+
assert_equal '<head><meta></head>', template.render('head' => 'meta')
|
48
61
|
assert_equal '<head><meta><link></head>', template.render(:head => %w[ meta link ])
|
62
|
+
assert_equal '<head><meta><link></head>', template.render('head' => [ :meta, :link ])
|
49
63
|
assert_equal '<head><meta><link></head>', template.render(:head => [ { :tag => 'meta' }, { :tag => 'link' } ])
|
64
|
+
assert_equal '<head><meta><link></head>', template.render('head' => [ { 'tag' => :meta }, { 'tag' => :link } ])
|
50
65
|
assert_equal '<head></head>', template.render
|
66
|
+
assert_equal '<head></head>', template.render([ ])
|
67
|
+
assert_equal '<head></head>', template.render({ })
|
68
|
+
assert_equal '<head><></head>', template.render('')
|
69
|
+
assert_equal '<head><></head>', template.render(:head => '')
|
51
70
|
assert_equal '<head></head>', template.render(:head => nil)
|
52
71
|
assert_equal '<head></head>', template.render(:head => [ ])
|
53
72
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handlebar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-07-18 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A simple text templating system
|
15
15
|
email: github@tadman.ca
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- notes/example.hb
|
32
32
|
- test/helper.rb
|
33
33
|
- test/test_handlebar.rb
|
34
|
+
- test/test_handlebar_support.rb
|
34
35
|
- test/test_handlebar_template.rb
|
35
36
|
homepage: http://github.com/twg/handlebar
|
36
37
|
licenses:
|
@@ -53,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
54
|
version: '0'
|
54
55
|
requirements: []
|
55
56
|
rubyforge_project:
|
56
|
-
rubygems_version: 1.8.
|
57
|
+
rubygems_version: 1.8.24
|
57
58
|
signing_key:
|
58
59
|
specification_version: 3
|
59
60
|
summary: Simple text tempating system
|