ruby-handlebars 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 2237e25fe7e159de9f12860cf7a832200e1154e5c1bf4b3ae13208d17a6d5a86
4
- data.tar.gz: b98b8c72ea564f7a35bd71029ad7ff8105c42f2b464e59dde0ef13b89f9aa102
2
+ SHA1:
3
+ metadata.gz: 172b5ec24e44155491b10c90400ffdfe5b194ae5
4
+ data.tar.gz: 7d9b4798b10bb3ef81e7544ca11b29c39b4f7138
5
5
  SHA512:
6
- metadata.gz: 377a11ba8a636bb05b030509111fb35ba5a92b3662b0c6bbbebd96c05d47d906236fc5b9788e26d9ee38b4e5b9fd462b386e114e19c6fdb0816c5394294dcaa0
7
- data.tar.gz: 31d29b8da30b04404a30f21d567fda3506551651092dd3b050fc072a919d7693c238eb4c45fb2f2cc7394a34d458eebe576a8ecc99cdb07176baf11131f3b98d
6
+ metadata.gz: 36fbea8427cbb7938bbe2f565b365f74ae74a635614a408c89b6ba251ccfac8733d08fb2933f52494a3db6ccd2ba4f67c1b6deb118310592566e1373c92182e4
7
+ data.tar.gz: 6053e070453271400590c0668093de272036097a9ef6edace30f67f2a58e2599e5a1665541cf86dae50f584a63b72a5c682382b76096f7e03ada8997b66d7e8d
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  ruby-handlebars
2
2
  ===============
3
3
 
4
- [![Build Status](https://travis-ci.org/vincent-psarga/ruby-handlebars.svg?branch=master)](https://travis-ci.org/vincent-psarga/ruby-handlebars)
5
- [![Code Climate](https://codeclimate.com/github/vincent-psarga/ruby-handlebars/badges/gpa.svg)](https://codeclimate.com/github/vincent-psarga/ruby-handlebars)
6
- [![Test Coverage](https://codeclimate.com/github/vincent-psarga/ruby-handlebars/badges/coverage.svg)](https://codeclimate.com/github/vincent-psarga/ruby-handlebars)
4
+ [![Build Status](https://travis-ci.org/SmartBear/ruby-handlebars.svg?branch=master)](https://travis-ci.org/SmartBear/ruby-handlebars)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/22dbe81de487cb27097c/maintainability)](https://codeclimate.com/github/SmartBear/ruby-handlebars/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/22dbe81de487cb27097c/test_coverage)](https://codeclimate.com/github/SmartBear/ruby-handlebars/test_coverage)
7
7
 
8
8
  Pure Ruby library for [Handlebars](http://handlebarsjs.com/) template system.
9
9
  The main goal of this library is to simplify the use of Ruby and Handlebars on Windows machine. If you do not have any need of working on Windows, take a look at [handlebars.rb](https://github.com/cowboyd/handlebars.rb) that uses the real Handlebars library.
@@ -100,6 +100,16 @@ template = [
100
100
  ].join("\n")
101
101
  ```
102
102
 
103
+ 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.
104
+
105
+ For example:
106
+
107
+ ```ruby
108
+ hbs.register_helper('helperMissing') do |context, name|
109
+ puts "No helper found with name #{name}"
110
+ end
111
+ ```
112
+
103
113
  Limitations and roadmap
104
114
  -----------------------
105
115
 
@@ -114,4 +124,4 @@ Aknowledgements
114
124
  This gem would simply not exist if the handlebars team was not here. Thanks a lot for this awesome templating system.
115
125
  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 ;) )
116
126
 
117
- Thanks a lot to the contributors @mvz and @schuetzm for making it a way better Handlebars renderer :)
127
+ Thanks a lot to the contributors @mvz and @schuetzm for making it a way better Handlebars renderer :)
@@ -2,6 +2,7 @@ require_relative 'ruby-handlebars/parser'
2
2
  require_relative 'ruby-handlebars/tree'
3
3
  require_relative 'ruby-handlebars/template'
4
4
  require_relative 'ruby-handlebars/helper'
5
+ require_relative 'ruby-handlebars/helpers/register_default_helpers'
5
6
  require_relative 'ruby-handlebars/escapers/html_escaper'
6
7
 
7
8
  module Handlebars
@@ -54,40 +55,7 @@ module Handlebars
54
55
  end
55
56
 
56
57
  def register_default_helpers
57
- register_if_helper
58
- register_each_helper
59
- end
60
-
61
- def register_if_helper
62
- register_helper('if') do |context, condition, block, else_block|
63
- condition = !condition.empty? if condition.respond_to?(:empty?)
64
-
65
- if condition
66
- block.fn(context)
67
- elsif else_block
68
- else_block.fn(context)
69
- else
70
- ""
71
- end
72
- end
73
- end
74
-
75
- def register_each_helper
76
- register_helper('each') do |context, items, block, else_block|
77
- if (items.nil? || items.empty?)
78
- if else_block
79
- result = else_block.fn(context)
80
- end
81
- else
82
- context.with_temporary_context(:this => nil, :@index => 0, :@first => false, :@last => false) do
83
- result = items.each_with_index.map do |item, index|
84
- context.add_items(:this => item, :@index => index, :@first => (index == 0), :@last => (index == items.length - 1))
85
- block.fn(context)
86
- end.join('')
87
- end
88
- end
89
- result
90
- end
58
+ Helpers.register_default_helpers(self)
91
59
  end
92
60
  end
93
61
  end
@@ -7,35 +7,27 @@ module Handlebars
7
7
  @fn = fn
8
8
  end
9
9
 
10
- def apply(context, arguments = [], block = [])
10
+ def apply(context, arguments = [], block = [], else_block = [])
11
11
  arguments = [arguments] unless arguments.is_a? Array
12
- args = [context] + arguments.map {|arg| arg.eval(context)} + split_block(block || [])
12
+ args = [context] + arguments.map {|arg| arg.eval(context)} + split_block(block, else_block)
13
13
 
14
14
  @fn.call(*args)
15
15
  end
16
16
 
17
- def split_block(block)
18
- helper_block = Tree::Block.new([])
19
- inverse_block = Tree::Block.new([])
17
+ private
20
18
 
21
- receiver = helper_block
22
- else_found = false
23
-
24
- block.each do |item|
25
- if item.is_a?(Tree::Replacement) && item.is_else?
26
- receiver = inverse_block
27
- else_found = true
28
- next
29
- end
30
-
31
- receiver.add_item(item)
32
- end
33
-
34
- if else_found
35
- return [helper_block, inverse_block]
19
+ def split_block(block, else_block)
20
+ if else_block
21
+ [ensure_block(block), ensure_block(else_block)]
36
22
  else
37
- return [helper_block]
23
+ [ensure_block(block)]
38
24
  end
39
25
  end
26
+
27
+ def ensure_block(block)
28
+ new_block = Tree::Block.new([])
29
+ block.each {|item| new_block.add_item(item) } unless block.nil?
30
+ new_block
31
+ end
40
32
  end
41
- end
33
+ end
@@ -0,0 +1,20 @@
1
+ module Handlebars
2
+ module Helpers
3
+ class DefaultHelper
4
+ def self.register(hbs)
5
+ hbs.register_helper(self.registry_name) do |context, parameters, block, else_block|
6
+ self.apply(context, parameters, block, else_block)
7
+ end
8
+ end
9
+
10
+ # Should be implemented by sub-classes
11
+ # def self.registry_name
12
+ # 'myHelperName'
13
+ # end
14
+
15
+ # def self.apply(context, parameters, block, else_block)
16
+ # # Do things and stuff
17
+ # end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'default_helper'
2
+
3
+ module Handlebars
4
+ module Helpers
5
+ class EachHelper < DefaultHelper
6
+ def self.registry_name
7
+ 'each'
8
+ end
9
+
10
+ def self.apply(context, items, block, else_block)
11
+ if (items.nil? || items.empty?)
12
+ if else_block
13
+ result = else_block.fn(context)
14
+ end
15
+ else
16
+ context.with_temporary_context(:this => nil, :@index => 0, :@first => false, :@last => false) do
17
+ result = items.each_with_index.map do |item, index|
18
+ context.add_items(:this => item, :@index => index, :@first => (index == 0), :@last => (index == items.length - 1))
19
+ block.fn(context)
20
+ end.join('')
21
+ end
22
+ end
23
+ result
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'default_helper'
2
+
3
+ module Handlebars
4
+ class UnknownHelper < StandardError
5
+ end
6
+
7
+ module Helpers
8
+ class HelperMissingHelper < DefaultHelper
9
+ def self.registry_name
10
+ 'helperMissing'
11
+ end
12
+
13
+ def self.apply(context, name, block, else_block)
14
+ raise(::Handlebars::UnknownHelper, "Helper \"#{name}\" does not exist" )
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'default_helper'
2
+
3
+ module Handlebars
4
+ module Helpers
5
+ class IfHelper < DefaultHelper
6
+ def self.registry_name
7
+ 'if'
8
+ end
9
+
10
+ def self.apply(context, condition, block, else_block)
11
+ condition = !condition.empty? if condition.respond_to?(:empty?)
12
+
13
+ if condition
14
+ block.fn(context)
15
+ elsif else_block
16
+ else_block.fn(context)
17
+ else
18
+ ""
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'each_helper'
2
+ require_relative 'helper_missing_helper'
3
+ require_relative 'if_helper'
4
+ require_relative 'unless_helper'
5
+
6
+ module Handlebars
7
+ module Helpers
8
+ def self.register_default_helpers(hbs)
9
+ EachHelper.register(hbs)
10
+ HelperMissingHelper.register(hbs)
11
+ IfHelper.register(hbs)
12
+ UnlessHelper.register(hbs)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'default_helper'
2
+
3
+ module Handlebars
4
+ module Helpers
5
+ class UnlessHelper < DefaultHelper
6
+ def self.registry_name
7
+ 'unless'
8
+ end
9
+
10
+ def self.apply(context, condition, block, else_block)
11
+ condition = !condition.empty? if condition.respond_to?(:empty?)
12
+
13
+ unless condition
14
+ block.fn(context)
15
+ else
16
+ else_block ? else_block.fn(context) : ""
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -16,7 +16,8 @@ module Handlebars
16
16
  rule(:tocurly) { ocurly >> ocurly >> ocurly }
17
17
  rule(:tccurly) { ccurly >> ccurly >> ccurly }
18
18
 
19
- rule(:identifier) { match['@a-zA-Z0-9_\?'].repeat(1) }
19
+ rule(:else_kw) { str('else') }
20
+ rule(:identifier) { (else_kw >> space? >> dccurly).absent? >> match['@\-a-zA-Z0-9_\?'].repeat(1) }
20
21
  rule(:path) { identifier >> (dot >> identifier).repeat }
21
22
 
22
23
  rule(:nocurly) { match('[^{}]') }
@@ -57,6 +58,9 @@ module Handlebars
57
58
  scope {
58
59
  block
59
60
  } >>
61
+ scope {
62
+ docurly >> space? >> else_kw >> space? >> dccurly >> scope { block_item.repeat.as(:else_block_items) }
63
+ }.maybe >>
60
64
  dynamic { |src, scope|
61
65
  docurly >> slash >> str(scope.captures[:helper_name]) >> dccurly
62
66
  }
@@ -71,7 +75,8 @@ module Handlebars
71
75
  dccurly
72
76
  }
73
77
 
74
- rule(:block) { (template_content | unsafe_replacement | safe_replacement | helper | partial | block_helper ).repeat.as(:block_items) }
78
+ rule(:block_item) { (template_content | unsafe_replacement | safe_replacement | helper | partial | block_helper ) }
79
+ rule(:block) { block_item.repeat.as(:block_items) }
75
80
 
76
81
  root :block
77
82
  end
@@ -20,10 +20,6 @@ module Handlebars
20
20
  context.get_helper(item.to_s).apply(context)
21
21
  end
22
22
  end
23
-
24
- def is_else?
25
- item.to_s == 'else'
26
- end
27
23
  end
28
24
 
29
25
  class EscapedReplacement < Replacement
@@ -48,9 +44,14 @@ module Handlebars
48
44
  end
49
45
  end
50
46
 
51
- class Helper < TreeItem.new(:name, :parameters, :block)
47
+ class Helper < TreeItem.new(:name, :parameters, :block, :else_block)
52
48
  def _eval(context)
53
- context.get_helper(name.to_s).apply(context, parameters, block)
49
+ helper = context.get_helper(name.to_s)
50
+ if helper.nil?
51
+ context.get_helper('helperMissing').apply(context, String.new(name.to_s))
52
+ else
53
+ helper.apply(context, parameters, block, else_block)
54
+ end
54
55
  end
55
56
  end
56
57
 
@@ -75,6 +76,7 @@ module Handlebars
75
76
  def add_item(i)
76
77
  items << i
77
78
  end
79
+
78
80
  end
79
81
  end
80
82
 
@@ -91,6 +93,7 @@ module Handlebars
91
93
  ) {
92
94
  Tree::EscapedHelper.new(name, parameters)
93
95
  }
96
+
94
97
  rule(
95
98
  safe_helper_name: simple(:name),
96
99
  parameters: subtree(:parameters)
@@ -100,20 +103,38 @@ module Handlebars
100
103
 
101
104
  rule(
102
105
  helper_name: simple(:name),
103
- block_items: subtree(:block_items)
106
+ block_items: subtree(:block_items),
104
107
  ) {
105
108
  Tree::Helper.new(name, [], block_items)
106
109
  }
107
110
 
111
+ rule(
112
+ helper_name: simple(:name),
113
+ block_items: subtree(:block_items),
114
+ else_block_items: subtree(:else_block_items)
115
+ ) {
116
+ Tree::Helper.new(name, [], block_items, else_block_items)
117
+ }
118
+
108
119
  rule(
109
120
  helper_name: simple(:name),
110
121
  parameters: subtree(:parameters),
111
- block_items: subtree(:block_items)
122
+ block_items: subtree(:block_items),
112
123
  ) {
113
124
  Tree::Helper.new(name, parameters, block_items)
114
125
  }
115
126
 
127
+ rule(
128
+ helper_name: simple(:name),
129
+ parameters: subtree(:parameters),
130
+ block_items: subtree(:block_items),
131
+ else_block_items: subtree(:else_block_items)
132
+ ) {
133
+ Tree::Helper.new(name, parameters, block_items, else_block_items)
134
+ }
135
+
116
136
  rule(partial_name: simple(:partial_name)) {Tree::Partial.new(partial_name)}
117
137
  rule(block_items: subtree(:block_items)) {Tree::Block.new(block_items)}
138
+ rule(else_block_items: subtree(:else_block_items)) {Tree::Block.new(block_items)}
118
139
  end
119
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-handlebars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Pretre
@@ -9,68 +9,68 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-06-26 00:00:00.000000000 Z
12
+ date: 2019-08-30 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'
21
18
  - - ">="
22
19
  - !ruby/object:Gem::Version
23
20
  version: 1.6.2
21
+ - - "~>"
22
+ - !ruby/object:Gem::Version
23
+ version: '1.6'
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'
31
28
  - - ">="
32
29
  - !ruby/object:Gem::Version
33
30
  version: 1.6.2
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
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'
41
38
  - - ">="
42
39
  - !ruby/object:Gem::Version
43
40
  version: 0.10.1
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '0.10'
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'
51
48
  - - ">="
52
49
  - !ruby/object:Gem::Version
53
50
  version: 0.10.1
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.10'
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'
61
58
  - - ">="
62
59
  - !ruby/object:Gem::Version
63
60
  version: 0.4.9.1
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '0.4'
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'
71
68
  - - ">="
72
69
  - !ruby/object:Gem::Version
73
70
  version: 0.4.9.1
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '0.4'
74
74
  - !ruby/object:Gem::Dependency
75
75
  name: rspec
76
76
  requirement: !ruby/object:Gem::Requirement
@@ -95,62 +95,42 @@ dependencies:
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'
101
98
  - - ">="
102
99
  - !ruby/object:Gem::Version
103
100
  version: 3.1.3
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
101
  - - "~>"
109
102
  - !ruby/object:Gem::Version
110
103
  version: '3.1'
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- version: 3.1.3
114
- - !ruby/object:Gem::Dependency
115
- name: codeclimate-test-reporter
116
- requirement: !ruby/object:Gem::Requirement
117
- requirements:
118
- - - "~>"
119
- - !ruby/object:Gem::Version
120
- version: '0.4'
121
- - - ">="
122
- - !ruby/object:Gem::Version
123
- version: 0.4.6
124
104
  type: :development
125
105
  prerelease: false
126
106
  version_requirements: !ruby/object:Gem::Requirement
127
107
  requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: '0.4'
131
108
  - - ">="
132
109
  - !ruby/object:Gem::Version
133
- version: 0.4.6
110
+ version: 3.1.3
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '3.1'
134
114
  - !ruby/object:Gem::Dependency
135
115
  name: jeweler
136
116
  requirement: !ruby/object:Gem::Requirement
137
117
  requirements:
138
- - - "~>"
139
- - !ruby/object:Gem::Version
140
- version: '2.0'
141
118
  - - ">="
142
119
  - !ruby/object:Gem::Version
143
120
  version: 2.0.1
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '2.0'
144
124
  type: :development
145
125
  prerelease: false
146
126
  version_requirements: !ruby/object:Gem::Requirement
147
127
  requirements:
148
- - - "~>"
149
- - !ruby/object:Gem::Version
150
- version: '2.0'
151
128
  - - ">="
152
129
  - !ruby/object:Gem::Version
153
130
  version: 2.0.1
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '2.0'
154
134
  description:
155
135
  email: v.pretre@hiptest.net
156
136
  executables: []
@@ -166,10 +146,16 @@ files:
166
146
  - lib/ruby-handlebars/escapers/dummy_escaper.rb
167
147
  - lib/ruby-handlebars/escapers/html_escaper.rb
168
148
  - lib/ruby-handlebars/helper.rb
149
+ - lib/ruby-handlebars/helpers/default_helper.rb
150
+ - lib/ruby-handlebars/helpers/each_helper.rb
151
+ - lib/ruby-handlebars/helpers/helper_missing_helper.rb
152
+ - lib/ruby-handlebars/helpers/if_helper.rb
153
+ - lib/ruby-handlebars/helpers/register_default_helpers.rb
154
+ - lib/ruby-handlebars/helpers/unless_helper.rb
169
155
  - lib/ruby-handlebars/parser.rb
170
156
  - lib/ruby-handlebars/template.rb
171
157
  - lib/ruby-handlebars/tree.rb
172
- homepage: https://github.com/vincent-psarga/ruby-handlebars
158
+ homepage: https://github.com/smartbear/ruby-handlebars
173
159
  licenses: []
174
160
  metadata: {}
175
161
  post_install_message:
@@ -187,7 +173,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
173
  - !ruby/object:Gem::Version
188
174
  version: '0'
189
175
  requirements: []
190
- rubygems_version: 3.0.4
176
+ rubyforge_project:
177
+ rubygems_version: 2.6.10
191
178
  signing_key:
192
179
  specification_version: 4
193
180
  summary: Pure Ruby library for Handlebars templates