ruby-handlebars 0.2.1 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +72 -0
- data/README.md +50 -16
- data/lib/ruby-handlebars/context.rb +22 -1
- data/lib/ruby-handlebars/escapers/html_escaper.rb +3 -1
- data/lib/ruby-handlebars/helper.rb +8 -0
- data/lib/ruby-handlebars/helpers/default_helper.rb +9 -1
- data/lib/ruby-handlebars/helpers/each_helper.rb +6 -2
- data/lib/ruby-handlebars/helpers/register_default_helpers.rb +2 -0
- data/lib/ruby-handlebars/helpers/with_helper.rb +25 -0
- data/lib/ruby-handlebars/parser.rb +39 -6
- data/lib/ruby-handlebars/template.rb +6 -4
- data/lib/ruby-handlebars/tree.rb +47 -1
- data/lib/ruby-handlebars.rb +9 -5
- metadata +43 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8504965f0af446e8722e221cf88adf7b4c1268c9900817cbd6485c0e4d4d62d5
|
4
|
+
data.tar.gz: 54254d370a06fbd6baed8f1eb6a7b0fc80b668f61bd3971ea6acf381afbca592
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b5a6f3c377ed3bc265093d9eb55c17c9bca7204c8a0911b348047f3bc184eebc2297d8cbc4155fc0d30a4858b4e55307d48237b583275972ef1b5db8a5ab89d
|
7
|
+
data.tar.gz: a141269c77826d0821eb7bb34b955a9265476e4fc2fef0b2c18ab592b48f4b1c16a8003753fe8895f3e8bc1abab3e85ca47326ac433b3e6b45aeb9bc4d006167
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
ruby-handlebars changelog
|
2
|
+
=========================
|
3
|
+
|
4
|
+
[Unreleased]
|
5
|
+
------------
|
6
|
+
|
7
|
+
|
8
|
+
[0.4.1] (2022/06/01)
|
9
|
+
--------------------
|
10
|
+
|
11
|
+
- bump dependencies
|
12
|
+
- move CI to GitHub actions
|
13
|
+
- use separate context for each template evaluation ([#33](https://github.com/SmartBear/ruby-handlebars/pull/33) [@mvz])
|
14
|
+
- added `with`helper ([#28](https://github.com/SmartBear/ruby-handlebars/pull/28) [@creature])
|
15
|
+
- Faster parsing ([#26](https://github.com/SmartBear/ruby-handlebars/pull/26) - [@mvz])
|
16
|
+
|
17
|
+
[0.4.0] (2019/10/22)
|
18
|
+
--------------------
|
19
|
+
|
20
|
+
- Allow slash character in partial names ([#18](https://github.com/SmartBear/ruby-handlebars/pull/18) - [@d316])
|
21
|
+
- Add parameters for partials ([#19](https://github.com/SmartBear/ruby-handlebars/pull/19) [#20](https://github.com/SmartBear/ruby-handlebars/pull/20) - [@d316])
|
22
|
+
|
23
|
+
[0.3.0] (2019/10/11)
|
24
|
+
--------------------
|
25
|
+
|
26
|
+
- Support helpers with "as" notation (`{{each items as |item|}}`)
|
27
|
+
|
28
|
+
[0.2.1] (2019/8/30)
|
29
|
+
-------------------
|
30
|
+
|
31
|
+
- allow "else" word as being part of a path (eg: {{ my.something.else }} is okay)
|
32
|
+
|
33
|
+
[0.2.0] (2019/8/30)
|
34
|
+
-------------------
|
35
|
+
|
36
|
+
- allow dash in identifiers ([#15](https://github.com/SmartBear/ruby-handlebars/pull/15) - [@stewartmckee])
|
37
|
+
- add "unless" helper
|
38
|
+
- add "helperMissing" helper, called when a helper is missing
|
39
|
+
- "else" keyword is now handled by the parser directly
|
40
|
+
|
41
|
+
[0.1.1] (2019/6/26)
|
42
|
+
-------------------
|
43
|
+
|
44
|
+
- with_temporary_context returns the result produced by the block
|
45
|
+
|
46
|
+
[0.1.0] (2019/6/26)
|
47
|
+
-------------------
|
48
|
+
|
49
|
+
- add 'with_temporary_context' in context to define temporary variables
|
50
|
+
- enable @index, @first and @last variables in "each" helper ([#10](https://github.com/SmartBear/ruby-handlebars/pull/10) - [@schuetzm])
|
51
|
+
- allow specifying escaper when using double curly braces
|
52
|
+
- allow using helper calls as arguments ([#11](https://github.com/SmartBear/ruby-handlebars/pull/11) - [@schuetzm])
|
53
|
+
- escape trice-braces replacements ([#9](https://github.com/SmartBear/ruby-handlebars/pull/9) - [@schuetzm])
|
54
|
+
- allow non-hash data ([#8](https://github.com/SmartBear/ruby-handlebars/pull/8) - [@mvz])
|
55
|
+
- allow single curly braces in content ([#7](https://github.com/SmartBear/ruby-handlebars/pull/7) - [@mvz])
|
56
|
+
- allow empty literal string arguments ([pr6]https://github.com/SmartBear/ruby-handlebars/pull/6) - [@mvz])
|
57
|
+
|
58
|
+
<!-- Contributors lists -->
|
59
|
+
[@stewartmckee]: https://github.com/stewartmckee
|
60
|
+
[@schuetzm]: https://github.com/schuetzm
|
61
|
+
[@mvz]: https://github.com/mvz
|
62
|
+
[@d316]: https://github.com/d316
|
63
|
+
[@creature]: https://github.com/creature
|
64
|
+
|
65
|
+
<!-- Releases diffs -->
|
66
|
+
[Unreleased]: https://github.com/smartbear/ruby-handlebars/compare/v0.4.0...master
|
67
|
+
[0.4.0]: https://github.com/smartbear/ruby-handlebars/compare/v0.3.0...v0.4.0
|
68
|
+
[0.3.0]: https://github.com/smartbear/ruby-handlebars/compare/v0.2.1...v0.3.0
|
69
|
+
[0.2.1]: https://github.com/smartbear/ruby-handlebars/compare/v0.2.0...v0.2.1
|
70
|
+
[0.2.0]: https://github.com/smartbear/ruby-handlebars/compare/v0.1.1...v0.2.0
|
71
|
+
[0.1.1]: https://github.com/smartbear/ruby-handlebars/compare/v0.1.0...v0.1.1
|
72
|
+
[0.1.0]: https://github.com/smartbear/ruby-handlebars/compare/v0.0.6...v0.1.0
|
data/README.md
CHANGED
@@ -41,6 +41,13 @@ hbs.compile("Hello {{> full_name}}").call({person: {first_name: 'Pinkie', last_n
|
|
41
41
|
# Gives: "Hello Pinkie Pie"
|
42
42
|
```
|
43
43
|
|
44
|
+
Partials support parameters:
|
45
|
+
```ruby
|
46
|
+
hbs.register_partial('full_name', "{{fname}} {{lname}}")
|
47
|
+
hbs.compile("Hello {{> full_name fname='jon' lname='doe'}}")
|
48
|
+
# Gives: "Hello jon doe"
|
49
|
+
```
|
50
|
+
|
44
51
|
You can also register inline helpers:
|
45
52
|
|
46
53
|
```ruby
|
@@ -82,22 +89,49 @@ hbs.compile(template).call({description: my_description})
|
|
82
89
|
# Output will depend on the validity of the 'my_description' variable
|
83
90
|
```
|
84
91
|
|
85
|
-
|
92
|
+
Default helpers:
|
93
|
+
----------------
|
86
94
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
95
|
+
Three default helpers are provided: ``each``, ``if`` and ``unless``.
|
96
|
+
|
97
|
+
The each helper let you walk through a list. You can either use the basic notation and referencing the current item as ``this``:
|
98
|
+
|
99
|
+
```
|
100
|
+
{{#each items}}
|
101
|
+
{{{ this }}}
|
102
|
+
{{else}}
|
103
|
+
No items
|
104
|
+
{{/each}}
|
105
|
+
```
|
106
|
+
|
107
|
+
or the "as |name|" notation:
|
108
|
+
|
109
|
+
```
|
110
|
+
{{#each items as |item| }}
|
111
|
+
{{{ item }}}
|
112
|
+
{{else}}
|
113
|
+
No items
|
114
|
+
{{/each}}
|
115
|
+
```
|
116
|
+
|
117
|
+
The ``if`` helper can be used to write conditionnal templates:
|
118
|
+
|
119
|
+
```
|
120
|
+
{{#if my_condition}}
|
121
|
+
It's ok
|
122
|
+
{{else}}
|
123
|
+
or maybe not
|
124
|
+
{{/if}}
|
125
|
+
```
|
126
|
+
|
127
|
+
The ``unless`` helper works the opposite way to ``if``:
|
128
|
+
|
129
|
+
```
|
130
|
+
{{#unless my_condition}}
|
131
|
+
It's not ok
|
132
|
+
{{else}}
|
133
|
+
or maybe it is
|
134
|
+
{{/unless}}
|
101
135
|
```
|
102
136
|
|
103
137
|
Currently, if you call an unknown helper, it will raise an exception. You can override that by registering your own version of the ``helperMissing`` helper. Note that only the name of the missing helper will be provided.
|
@@ -124,4 +158,4 @@ Aknowledgements
|
|
124
158
|
This gem would simply not exist if the handlebars team was not here. Thanks a lot for this awesome templating system.
|
125
159
|
Thanks a lot to @cowboyd for the [handlebars.rb](https://github.com/cowboyd/handlebars.rb) gem. We used it for a while and it's great (and as told at the beginning of the README, if you do not need any Windows support, use handlebars.rb instead ;) )
|
126
160
|
|
127
|
-
Thanks a lot to the contributors @mvz and @
|
161
|
+
Thanks a lot to the contributors @mvz, @schuetzm and @stewartmckee for making it a way better Handlebars renderer :)
|
@@ -1,5 +1,10 @@
|
|
1
1
|
module Handlebars
|
2
|
-
|
2
|
+
class Context
|
3
|
+
def initialize(hbs, data)
|
4
|
+
@hbs = hbs
|
5
|
+
@data = data
|
6
|
+
end
|
7
|
+
|
3
8
|
def get(path)
|
4
9
|
items = path.split('.'.freeze)
|
5
10
|
if locals.key? items.first.to_sym
|
@@ -15,6 +20,22 @@ module Handlebars
|
|
15
20
|
current
|
16
21
|
end
|
17
22
|
|
23
|
+
def escaper
|
24
|
+
@hbs.escaper
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_helper(name)
|
28
|
+
@hbs.get_helper(name)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_as_helper(name)
|
32
|
+
@hbs.get_as_helper(name)
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_partial(name)
|
36
|
+
@hbs.get_partial(name)
|
37
|
+
end
|
38
|
+
|
18
39
|
def add_item(key, value)
|
19
40
|
locals[key.to_sym] = value
|
20
41
|
end
|
@@ -14,6 +14,14 @@ module Handlebars
|
|
14
14
|
@fn.call(*args)
|
15
15
|
end
|
16
16
|
|
17
|
+
def apply_as(context, arguments = [], as_arguments = [], block = [], else_block = [])
|
18
|
+
arguments = [arguments] unless arguments.is_a? Array
|
19
|
+
as_arguments = [as_arguments] unless as_arguments.is_a? Array
|
20
|
+
args = [context] + arguments.map {|arg| arg.eval(context)} + as_arguments.map(&:name) + split_block(block, else_block)
|
21
|
+
|
22
|
+
@fn.call(*args)
|
23
|
+
end
|
24
|
+
|
17
25
|
private
|
18
26
|
|
19
27
|
def split_block(block, else_block)
|
@@ -4,7 +4,11 @@ module Handlebars
|
|
4
4
|
def self.register(hbs)
|
5
5
|
hbs.register_helper(self.registry_name) do |context, parameters, block, else_block|
|
6
6
|
self.apply(context, parameters, block, else_block)
|
7
|
-
end
|
7
|
+
end if self.respond_to?(:apply)
|
8
|
+
|
9
|
+
hbs.register_as_helper(self.registry_name) do |context, parameters, as_names, block, else_block|
|
10
|
+
self.apply_as(context, parameters, as_names, block, else_block)
|
11
|
+
end if self.respond_to?(:apply_as)
|
8
12
|
end
|
9
13
|
|
10
14
|
# Should be implemented by sub-classes
|
@@ -15,6 +19,10 @@ module Handlebars
|
|
15
19
|
# def self.apply(context, parameters, block, else_block)
|
16
20
|
# # Do things and stuff
|
17
21
|
# end
|
22
|
+
|
23
|
+
# def self.apply_as(context, parameters, as_names, block, else_block)
|
24
|
+
# # Do things and stuffa, but with 'as |param| notation'
|
25
|
+
# end
|
18
26
|
end
|
19
27
|
end
|
20
28
|
end
|
@@ -8,14 +8,18 @@ module Handlebars
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.apply(context, items, block, else_block)
|
11
|
+
self.apply_as(context, items, :this, block, else_block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.apply_as(context, items, name, block, else_block)
|
11
15
|
if (items.nil? || items.empty?)
|
12
16
|
if else_block
|
13
17
|
result = else_block.fn(context)
|
14
18
|
end
|
15
19
|
else
|
16
|
-
context.with_temporary_context(
|
20
|
+
context.with_temporary_context(name => nil, :@index => 0, :@first => false, :@last => false) do
|
17
21
|
result = items.each_with_index.map do |item, index|
|
18
|
-
context.add_items(
|
22
|
+
context.add_items(name => item, :@index => index, :@first => (index == 0), :@last => (index == items.length - 1))
|
19
23
|
block.fn(context)
|
20
24
|
end.join('')
|
21
25
|
end
|
@@ -2,6 +2,7 @@ require_relative 'each_helper'
|
|
2
2
|
require_relative 'helper_missing_helper'
|
3
3
|
require_relative 'if_helper'
|
4
4
|
require_relative 'unless_helper'
|
5
|
+
require_relative 'with_helper'
|
5
6
|
|
6
7
|
module Handlebars
|
7
8
|
module Helpers
|
@@ -10,6 +11,7 @@ module Handlebars
|
|
10
11
|
HelperMissingHelper.register(hbs)
|
11
12
|
IfHelper.register(hbs)
|
12
13
|
UnlessHelper.register(hbs)
|
14
|
+
WithHelper.register(hbs)
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'default_helper'
|
2
|
+
|
3
|
+
module Handlebars
|
4
|
+
module Helpers
|
5
|
+
class WithHelper < DefaultHelper
|
6
|
+
def self.registry_name
|
7
|
+
'with'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.apply(context, data, block, else_block)
|
11
|
+
if data
|
12
|
+
context.with_temporary_context(data) do
|
13
|
+
block.fn(context)
|
14
|
+
end
|
15
|
+
else
|
16
|
+
else_block.fn(context)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.apply_as(context, data, name, block, else_block)
|
21
|
+
self.apply(context, { name.to_sym => data }, block, else_block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -10,6 +10,9 @@ module Handlebars
|
|
10
10
|
rule(:slash) { str('/')}
|
11
11
|
rule(:ocurly) { str('{')}
|
12
12
|
rule(:ccurly) { str('}')}
|
13
|
+
rule(:pipe) { str('|')}
|
14
|
+
rule(:eq) { str('=')}
|
15
|
+
|
13
16
|
|
14
17
|
rule(:docurly) { ocurly >> ocurly }
|
15
18
|
rule(:dccurly) { ccurly >> ccurly }
|
@@ -17,17 +20,20 @@ module Handlebars
|
|
17
20
|
rule(:tccurly) { ccurly >> ccurly >> ccurly }
|
18
21
|
|
19
22
|
rule(:else_kw) { str('else') }
|
23
|
+
rule(:as_kw) { str('as') }
|
24
|
+
|
20
25
|
rule(:identifier) { (else_kw >> space? >> dccurly).absent? >> match['@\-a-zA-Z0-9_\?'].repeat(1) }
|
26
|
+
rule(:directory) { (else_kw >> space? >> dccurly).absent? >> match['@\-a-zA-Z0-9_\/\?'].repeat(1) }
|
21
27
|
rule(:path) { identifier >> (dot >> (identifier | else_kw)).repeat }
|
22
28
|
|
23
29
|
rule(:nocurly) { match('[^{}]') }
|
24
30
|
rule(:eof) { any.absent? }
|
25
31
|
rule(:template_content) {
|
26
32
|
(
|
33
|
+
nocurly.repeat(1) | # A sequence of non-curlies
|
27
34
|
ocurly >> nocurly | # Opening curly that doesn't start a {{}}
|
28
|
-
ocurly >> eof | # Opening curly that doesn't start a {{}} because it's the end
|
29
35
|
ccurly | # Closing curly that is not inside a {{}}
|
30
|
-
|
36
|
+
ocurly >> eof # Opening curly that doesn't start a {{}} because it's the end
|
31
37
|
).repeat(1).as(:template_content) }
|
32
38
|
|
33
39
|
rule(:unsafe_replacement) { docurly >> space? >> path.as(:replaced_unsafe_item) >> space? >> dccurly }
|
@@ -38,16 +44,41 @@ module Handlebars
|
|
38
44
|
rule(:string) { sq_string | dq_string }
|
39
45
|
|
40
46
|
rule(:parameter) {
|
41
|
-
(
|
42
|
-
(
|
47
|
+
(as_kw >> space? >> pipe).absent? >>
|
48
|
+
(
|
49
|
+
(path | string).as(:parameter_name) |
|
50
|
+
(str('(') >> space? >> identifier.as(:safe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> str(')'))
|
51
|
+
)
|
43
52
|
}
|
44
53
|
rule(:parameters) { parameter >> (space >> parameter).repeat }
|
45
54
|
|
55
|
+
rule(:argument) { identifier.as(:key) >> space? >> eq >> space? >> parameter.as(:value) }
|
56
|
+
rule(:arguments) { argument >> (space >> argument).repeat }
|
57
|
+
|
46
58
|
rule(:unsafe_helper) { docurly >> space? >> identifier.as(:unsafe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> dccurly }
|
47
59
|
rule(:safe_helper) { tocurly >> space? >> identifier.as(:safe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> tccurly }
|
48
60
|
|
49
61
|
rule(:helper) { unsafe_helper | safe_helper }
|
50
62
|
|
63
|
+
rule(:as_block_helper) {
|
64
|
+
docurly >>
|
65
|
+
hash >>
|
66
|
+
identifier.capture(:helper_name).as(:helper_name) >>
|
67
|
+
space >> parameters.as(:parameters) >>
|
68
|
+
space >> as_kw >> space >> pipe >> space? >> parameters.as(:as_parameters) >> space? >> pipe >>
|
69
|
+
space? >>
|
70
|
+
dccurly >>
|
71
|
+
scope {
|
72
|
+
block
|
73
|
+
} >>
|
74
|
+
scope {
|
75
|
+
docurly >> space? >> else_kw >> space? >> dccurly >> scope { block_item.repeat.as(:else_block_items) }
|
76
|
+
}.maybe >>
|
77
|
+
dynamic { |src, scope|
|
78
|
+
docurly >> slash >> str(scope.captures[:helper_name]) >> dccurly
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
51
82
|
rule(:block_helper) {
|
52
83
|
docurly >>
|
53
84
|
hash >>
|
@@ -70,12 +101,14 @@ module Handlebars
|
|
70
101
|
docurly >>
|
71
102
|
gt >>
|
72
103
|
space? >>
|
73
|
-
|
104
|
+
directory.as(:partial_name) >>
|
105
|
+
space? >>
|
106
|
+
arguments.as(:arguments).maybe >>
|
74
107
|
space? >>
|
75
108
|
dccurly
|
76
109
|
}
|
77
110
|
|
78
|
-
rule(:block_item) { (template_content | unsafe_replacement | safe_replacement | helper | partial | block_helper ) }
|
111
|
+
rule(:block_item) { (template_content | unsafe_replacement | safe_replacement | helper | partial | block_helper | as_block_helper) }
|
79
112
|
rule(:block) { block_item.repeat.as(:block_items) }
|
80
113
|
|
81
114
|
root :block
|
@@ -8,11 +8,13 @@ module Handlebars
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def call(args = nil)
|
11
|
-
|
12
|
-
@hbs.set_context(args)
|
13
|
-
end
|
11
|
+
ctx = Context.new(@hbs, args)
|
14
12
|
|
15
|
-
@ast.eval(
|
13
|
+
@ast.eval(ctx)
|
14
|
+
end
|
15
|
+
|
16
|
+
def call_with_context(ctx)
|
17
|
+
@ast.eval(ctx)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
data/lib/ruby-handlebars/tree.rb
CHANGED
@@ -55,6 +55,17 @@ module Handlebars
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
+
class AsHelper < TreeItem.new(:name, :parameters, :as_parameters, :block, :else_block)
|
59
|
+
def _eval(context)
|
60
|
+
helper = context.get_as_helper(name.to_s)
|
61
|
+
if helper.nil?
|
62
|
+
context.get_helper('helperMissing').apply(context, String.new(name.to_s))
|
63
|
+
else
|
64
|
+
helper.apply_as(context, parameters, as_parameters, block, else_block)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
58
69
|
class EscapedHelper < Helper
|
59
70
|
def _eval(context)
|
60
71
|
context.escaper.escape(super(context).to_s)
|
@@ -63,7 +74,16 @@ module Handlebars
|
|
63
74
|
|
64
75
|
class Partial < TreeItem.new(:partial_name)
|
65
76
|
def _eval(context)
|
66
|
-
context.get_partial(partial_name.to_s).
|
77
|
+
context.get_partial(partial_name.to_s).call_with_context(context)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class PartialWithArgs < TreeItem.new(:partial_name, :arguments)
|
82
|
+
def _eval(context)
|
83
|
+
[arguments].flatten.map(&:values).map do |vals|
|
84
|
+
context.add_item vals.first.to_s, vals.last._eval(context)
|
85
|
+
end
|
86
|
+
context.get_partial(partial_name.to_s).call_with_context(context)
|
67
87
|
end
|
68
88
|
end
|
69
89
|
|
@@ -133,6 +153,32 @@ module Handlebars
|
|
133
153
|
Tree::Helper.new(name, parameters, block_items, else_block_items)
|
134
154
|
}
|
135
155
|
|
156
|
+
rule(
|
157
|
+
helper_name: simple(:name),
|
158
|
+
parameters: subtree(:parameters),
|
159
|
+
as_parameters: subtree(:as_parameters),
|
160
|
+
block_items: subtree(:block_items),
|
161
|
+
) {
|
162
|
+
Tree::AsHelper.new(name, parameters, as_parameters, block_items)
|
163
|
+
}
|
164
|
+
|
165
|
+
rule(
|
166
|
+
helper_name: simple(:name),
|
167
|
+
parameters: subtree(:parameters),
|
168
|
+
as_parameters: subtree(:as_parameters),
|
169
|
+
block_items: subtree(:block_items),
|
170
|
+
else_block_items: subtree(:else_block_items)
|
171
|
+
) {
|
172
|
+
Tree::AsHelper.new(name, parameters, as_parameters, block_items, else_block_items)
|
173
|
+
}
|
174
|
+
|
175
|
+
rule(
|
176
|
+
partial_name: simple(:partial_name),
|
177
|
+
arguments: subtree(:arguments)
|
178
|
+
) {
|
179
|
+
Tree::PartialWithArgs.new(partial_name, arguments)
|
180
|
+
}
|
181
|
+
|
136
182
|
rule(partial_name: simple(:partial_name)) {Tree::Partial.new(partial_name)}
|
137
183
|
rule(block_items: subtree(:block_items)) {Tree::Block.new(block_items)}
|
138
184
|
rule(else_block_items: subtree(:else_block_items)) {Tree::Block.new(block_items)}
|
data/lib/ruby-handlebars.rb
CHANGED
@@ -7,10 +7,10 @@ require_relative 'ruby-handlebars/escapers/html_escaper'
|
|
7
7
|
|
8
8
|
module Handlebars
|
9
9
|
class Handlebars
|
10
|
-
include Context
|
11
10
|
attr_reader :escaper
|
12
11
|
|
13
12
|
def initialize()
|
13
|
+
@as_helpers = {}
|
14
14
|
@helpers = {}
|
15
15
|
@partials = {}
|
16
16
|
register_default_helpers
|
@@ -25,10 +25,18 @@ module Handlebars
|
|
25
25
|
@helpers[name.to_s] = Helper.new(self, fn)
|
26
26
|
end
|
27
27
|
|
28
|
+
def register_as_helper(name, &fn)
|
29
|
+
@as_helpers[name.to_s] = Helper.new(self, fn)
|
30
|
+
end
|
31
|
+
|
28
32
|
def get_helper(name)
|
29
33
|
@helpers[name.to_s]
|
30
34
|
end
|
31
35
|
|
36
|
+
def get_as_helper(name)
|
37
|
+
@as_helpers[name.to_s]
|
38
|
+
end
|
39
|
+
|
32
40
|
def register_partial(name, content)
|
33
41
|
@partials[name.to_s] = Template.new(self, template_to_ast(content))
|
34
42
|
end
|
@@ -37,10 +45,6 @@ module Handlebars
|
|
37
45
|
@partials[name.to_s]
|
38
46
|
end
|
39
47
|
|
40
|
-
def set_context(ctx)
|
41
|
-
@data = ctx
|
42
|
-
end
|
43
|
-
|
44
48
|
def set_escaper(escaper = nil)
|
45
49
|
@escaper = escaper || Escapers::HTMLEscaper
|
46
50
|
end
|
metadata
CHANGED
@@ -1,144 +1,140 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-handlebars
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vincent Pretre
|
8
8
|
- Hiptest R&D
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-06-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parslet
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - ">="
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: 1.6.2
|
21
18
|
- - "~>"
|
22
19
|
- !ruby/object:Gem::Version
|
23
20
|
version: '1.6'
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.6.2
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
27
|
requirements:
|
28
|
-
- - ">="
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: 1.6.2
|
31
28
|
- - "~>"
|
32
29
|
- !ruby/object:Gem::Version
|
33
30
|
version: '1.6'
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.6.2
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: pry
|
36
36
|
requirement: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 0.10.1
|
41
38
|
- - "~>"
|
42
39
|
- !ruby/object:Gem::Version
|
43
40
|
version: '0.10'
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.10.1
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
46
|
version_requirements: !ruby/object:Gem::Requirement
|
47
47
|
requirements:
|
48
|
-
- - ">="
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
version: 0.10.1
|
51
48
|
- - "~>"
|
52
49
|
- !ruby/object:Gem::Version
|
53
50
|
version: '0.10'
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.10.1
|
54
54
|
- !ruby/object:Gem::Dependency
|
55
55
|
name: pry-stack_explorer
|
56
56
|
requirement: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - ">="
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
version: 0.4.9.1
|
61
58
|
- - "~>"
|
62
59
|
- !ruby/object:Gem::Version
|
63
60
|
version: '0.4'
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 0.4.9.1
|
64
64
|
type: :development
|
65
65
|
prerelease: false
|
66
66
|
version_requirements: !ruby/object:Gem::Requirement
|
67
67
|
requirements:
|
68
|
-
- - ">="
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
version: 0.4.9.1
|
71
68
|
- - "~>"
|
72
69
|
- !ruby/object:Gem::Version
|
73
70
|
version: '0.4'
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 0.4.9.1
|
74
74
|
- !ruby/object:Gem::Dependency
|
75
75
|
name: rspec
|
76
76
|
requirement: !ruby/object:Gem::Requirement
|
77
77
|
requirements:
|
78
|
-
- - ">="
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
version: 3.1.0
|
81
78
|
- - "~>"
|
82
79
|
- !ruby/object:Gem::Version
|
83
80
|
version: '3.1'
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 3.1.0
|
84
84
|
type: :development
|
85
85
|
prerelease: false
|
86
86
|
version_requirements: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
|
-
- - ">="
|
89
|
-
- !ruby/object:Gem::Version
|
90
|
-
version: 3.1.0
|
91
88
|
- - "~>"
|
92
89
|
- !ruby/object:Gem::Version
|
93
90
|
version: '3.1'
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 3.1.0
|
94
94
|
- !ruby/object:Gem::Dependency
|
95
95
|
name: rspec-mocks
|
96
96
|
requirement: !ruby/object:Gem::Requirement
|
97
97
|
requirements:
|
98
|
-
- - ">="
|
99
|
-
- !ruby/object:Gem::Version
|
100
|
-
version: 3.1.3
|
101
98
|
- - "~>"
|
102
99
|
- !ruby/object:Gem::Version
|
103
100
|
version: '3.1'
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.1.3
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: 3.1.3
|
111
108
|
- - "~>"
|
112
109
|
- !ruby/object:Gem::Version
|
113
110
|
version: '3.1'
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: 3.1.3
|
114
114
|
- !ruby/object:Gem::Dependency
|
115
|
-
name:
|
115
|
+
name: juwelier
|
116
116
|
requirement: !ruby/object:Gem::Requirement
|
117
117
|
requirements:
|
118
|
-
- - ">="
|
119
|
-
- !ruby/object:Gem::Version
|
120
|
-
version: 2.0.1
|
121
118
|
- - "~>"
|
122
119
|
- !ruby/object:Gem::Version
|
123
|
-
version: '2.
|
120
|
+
version: '2.4'
|
124
121
|
type: :development
|
125
122
|
prerelease: false
|
126
123
|
version_requirements: !ruby/object:Gem::Requirement
|
127
124
|
requirements:
|
128
|
-
- - ">="
|
129
|
-
- !ruby/object:Gem::Version
|
130
|
-
version: 2.0.1
|
131
125
|
- - "~>"
|
132
126
|
- !ruby/object:Gem::Version
|
133
|
-
version: '2.
|
134
|
-
description:
|
127
|
+
version: '2.4'
|
128
|
+
description:
|
135
129
|
email: v.pretre@hiptest.net
|
136
130
|
executables: []
|
137
131
|
extensions: []
|
138
132
|
extra_rdoc_files:
|
133
|
+
- CHANGELOG.md
|
139
134
|
- LICENSE
|
140
135
|
- README.md
|
141
136
|
files:
|
137
|
+
- CHANGELOG.md
|
142
138
|
- LICENSE
|
143
139
|
- README.md
|
144
140
|
- lib/ruby-handlebars.rb
|
@@ -152,13 +148,14 @@ files:
|
|
152
148
|
- lib/ruby-handlebars/helpers/if_helper.rb
|
153
149
|
- lib/ruby-handlebars/helpers/register_default_helpers.rb
|
154
150
|
- lib/ruby-handlebars/helpers/unless_helper.rb
|
151
|
+
- lib/ruby-handlebars/helpers/with_helper.rb
|
155
152
|
- lib/ruby-handlebars/parser.rb
|
156
153
|
- lib/ruby-handlebars/template.rb
|
157
154
|
- lib/ruby-handlebars/tree.rb
|
158
155
|
homepage: https://github.com/smartbear/ruby-handlebars
|
159
156
|
licenses: []
|
160
157
|
metadata: {}
|
161
|
-
post_install_message:
|
158
|
+
post_install_message:
|
162
159
|
rdoc_options: []
|
163
160
|
require_paths:
|
164
161
|
- lib
|
@@ -173,9 +170,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
170
|
- !ruby/object:Gem::Version
|
174
171
|
version: '0'
|
175
172
|
requirements: []
|
176
|
-
|
177
|
-
|
178
|
-
signing_key:
|
173
|
+
rubygems_version: 3.2.33
|
174
|
+
signing_key:
|
179
175
|
specification_version: 4
|
180
176
|
summary: Pure Ruby library for Handlebars templates
|
181
177
|
test_files: []
|