Skip to content

fix haml filter and update spec to include interpolation #198

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

Merged
merged 1 commit into from
Jan 30, 2023
Merged
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
41 changes: 35 additions & 6 deletions lib/ruby2js/haml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,42 @@
# (Note missing brackets: ruby syntax, js sematics)
#
require "haml"
require "haml/filters"
require "haml/filters/base"

module Haml
class Filters
class Ruby2JS < Base
def compile(node)
temple = [:multi]
temple << [:static, "<script type='text/javascript'>\n"]
compile_ruby!( temple , node )
temple << [:static, "\n</script>"]
temple
end

#Copird from text base, added ruby2js convert
def compile_ruby!(temple, node)
text = node.value[:text]
if ::Haml::Util.contains_interpolation?(node.value[:text])
# original: Haml::Filters#compile
text = ::Haml::Util.unescape_interpolation(text).gsub(/(\\+)n/) do |s|
escapes = $1.size
next s if escapes % 2 == 0
"#{'\\' * (escapes - 1)}\n"
end
text.prepend("\n")

temple << [:dynamic, "::Ruby2JS.convert(#{text} ).to_s"]
else
temple << [:static, ::Ruby2JS.convert(text).to_s]
end
end


module Ruby2JS
module Haml::Ruby2JS
include Haml::Filters::Base
def render(text)
converted = Ruby2JS.convert(text).to_s
"<script type='text/javascript'>\n#{converted}\n</script>"
end
end
end


Haml::Filters.registered[:ruby2js] ||= Haml::Filters::Ruby2JS
38 changes: 31 additions & 7 deletions spec/haml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
gem 'minitest'
require 'minitest/autorun'
require 'ruby2js/filter/functions'

require 'haml'
require 'ruby2js/haml'

describe 'HAML filter' do
it 'should convert ruby to javascript' do
template = %{
haml = %{
:ruby2js
alert 'Hello'
}

# unindent template so that the first line starts in column 1
template.gsub!(/^#{template[/\A\s+/]}/, '')
# As in, it is valid haml
haml.gsub!(/^#{haml[/\A\s+/]}/, '')

#copied from from haml tests, module RenderHelper

haml_engine = Haml::Engine.new(template)
output = _(haml_engine.render)
output = Haml::Template.new({}) { haml }.render(Object.new, {})

output.must_include "<script type='text/javascript'>"
output.must_include 'alert("Hello")'
output.must_include '</script>'
_(output).must_include "<script type='text/javascript'>"
_(output).must_include 'alert("Hello")'
_(output).must_include '</script>'
end

it 'should convert ruby with interpolation to javascript' do
haml = %{
:ruby2js
alert HASH{2 + 2}
}

# unindent template so that the first line starts in column 1
# As in, it is valid haml
haml.gsub!(/^#{haml[/\A\s+/]}/, '')
haml.gsub!("HASH", "#") #stop ruby interpreteting the 2 + 2

#copied from from haml tests, module RenderHelper
output = Haml::Template.new({}) { haml }.render(Object.new, {})

_(output).must_include 'alert(4)'
end

end