Skip to content
This repository has been archived by the owner on May 25, 2018. It is now read-only.

Improve readability of outputted hbs #69

Open
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion lib/hamlbars/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def make(expression, options)
def express(demarcation,expression,options={},&block)
if block.respond_to? :call
content = capture_haml(&block)
output = "#{demarcation.first}##{make(expression, options)}#{demarcation.last}#{content.strip}#{demarcation.first}/#{expression.split(' ').first}#{demarcation.last}"
output = "#{demarcation.first}##{make(expression, options)}#{demarcation.last}\n#{content.strip.gsub(/^/, ' ')}\n#{demarcation.first}/#{expression.split(' ').first}#{demarcation.last}"
else
output = "#{demarcation.first}#{make(expression, options)}#{demarcation.last}"
end
Expand Down
25 changes: 22 additions & 3 deletions spec/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,29 @@ def to_handlebars(s)

it "should render block expressions" do
expect(to_handlebars("= hb 'hello' do\n world.")).to eq(
"{{#hello}}world.{{/hello}}"
"{{#hello}}\n world.\n{{/hello}}"
)
end

it "should keep newlines on block expressions" do
handlebars = to_handlebars <<EOF
= hb 'if something' do
%div
One
%div two
EOF
expected = <<EOF
{{#if something}}
<div>
One
<div>two</div>
</div>
{{/if}}
EOF

expect(handlebars).to eq(expected.strip)
end

it "should render expression options" do
expect(to_handlebars('= hb "hello",:whom => "world"')).to eq(
"{{hello whom=\"world\"}}"
Expand All @@ -76,7 +95,7 @@ def to_handlebars(s)

it "should render tripple-stash block expressions" do
expect(to_handlebars("= hb! 'hello' do\n world.")).to eq(
"{{{#hello}}}world.{{{/hello}}}"
"{{{#hello}}}\n world.\n{{{/hello}}}"
)
end

Expand All @@ -92,7 +111,7 @@ def to_handlebars(s)
= hb 'hello'
%a{:bind => {:href => 'aController'}}
EOF
expect(handlebars).to eq("{{#if a_thing_is_true}}{{hello}}\n<a {{bind-attr href=\"aController\"}}></a>{{/if}}")
expect(handlebars).to eq("{{#if a_thing_is_true}}\n {{hello}}\n<a {{bind-attr href=\"aController\"}}></a>\n{{/if}}")
end

it "should not mark expressions as html_safe when XSS protection is disabled" do
Expand Down