-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhaml.rb
58 lines (51 loc) · 1.36 KB
/
haml.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# TODO: This feature is deprecated.
#
# Example usage:
#
# Add:
# gem 'ruby2js'
# to your gemfile and bundle
# Add:
# require "ruby2js/haml"
# to any initializer ot config/application.rb
#
# Use :ruby2js filter in your templates like
#
# :ruby2js
# alert "Hello"
#
# (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
end
end
end
Haml::Filters.registered[:ruby2js] ||= Haml::Filters::Ruby2JS