Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow integers as parameters #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ruby-handlebars/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ class Parser < Parslet::Parser
rule(:sq_string) { match("'") >> match("[^']").repeat.maybe.as(:str_content) >> match("'") }
rule(:dq_string) { match('"') >> match('[^"]').repeat.maybe.as(:str_content) >> match('"') }
rule(:string) { sq_string | dq_string }
rule(:digit) { match('[0-9]') }
rule(:integer) { digit.repeat(1).as(:integer_content) }

rule(:parameter) {
(as_kw >> space? >> pipe).absent? >>
(
(path | string).as(:parameter_name) |
(integer | path | string).as(:parameter_name) |
(str('(') >> space? >> identifier.as(:safe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> str(')'))
)
}
Expand Down
9 changes: 8 additions & 1 deletion lib/ruby-handlebars/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def _eval(context)
end
end

class Integer < TreeItem.new(:content)
def _eval(context)
return content.to_i
end
end

class Parameter < TreeItem.new(:name)
def _eval(context)
if name.is_a?(Parslet::Slice)
Expand Down Expand Up @@ -105,6 +111,7 @@ class Transform < Parslet::Transform
rule(replaced_unsafe_item: simple(:item)) {Tree::EscapedReplacement.new(item)}
rule(replaced_safe_item: simple(:item)) {Tree::Replacement.new(item)}
rule(str_content: simple(:content)) {Tree::String.new(content)}
rule(integer_content: simple(:content)) {Tree::Integer.new(content)}
rule(parameter_name: simple(:name)) {Tree::Parameter.new(name)}

rule(
Expand Down Expand Up @@ -171,7 +178,7 @@ class Transform < Parslet::Transform
) {
Tree::AsHelper.new(name, parameters, as_parameters, block_items, else_block_items)
}

rule(
partial_name: simple(:partial_name),
arguments: subtree(:arguments)
Expand Down
5 changes: 5 additions & 0 deletions spec/handlebars_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def evaluate(template, args = {})
expect(evaluate('Hello {{first-name}}', double("first-name": 'world'))).to eq('Hello world')
end

it 'handles integer parameters' do
hbs.register_helper(:add){|context, a, b| a + b}
expect(evaluate('The sum is: {{add base_value 9}}', double("base_value": 2))).to eq('The sum is: 11')
end

context 'partials' do
it 'simple' do
hbs.register_partial('plic', "Plic")
Expand Down
16 changes: 16 additions & 0 deletions spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@
})
end

it 'with integer parameters' do
result = parser.parse('{{ add 5 192 }}')
expect(result).to eq({
block_items: [
{
unsafe_helper_name: "add",
parameters: [
{parameter_name: {integer_content: '5'}},
{parameter_name: {integer_content: '192'}}
]
}
]
})

end

it 'block' do
expect(parser.parse('{{#capitalize}}plic{{/capitalize}}')).to eq({
block_items: [
Expand Down