ruby-haml-js 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/README.md +62 -1
- data/lib/ruby-haml-js/template.rb +4 -2
- data/lib/ruby-haml-js/version.rb +1 -1
- data/ruby-haml-js.gemspec +1 -1
- data/spec/template_spec.rb +7 -0
- metadata +14 -16
- data/Gemfile.lock +0 -120
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,53 @@ Precompiles the HAML-JS templates into a function.
|
|
4
4
|
|
5
5
|
This can be used as a standalone template or as part of Rails 3.1 assets pipeline.
|
6
6
|
|
7
|
-
|
7
|
+
For example, if you have a file `app/assets/javascripts/templates/comment.jst.hamljs` with the following content:
|
8
|
+
|
9
|
+
```haml
|
10
|
+
.comment
|
11
|
+
.author= author
|
12
|
+
.text= text
|
13
|
+
```
|
14
|
+
|
15
|
+
Then it will be *compiled* on the server side into a plain JavaScript function.
|
16
|
+
It doesn't require you to do anything on the client side.
|
17
|
+
And the result will look like this:
|
18
|
+
|
19
|
+
```javascript
|
20
|
+
(function() {
|
21
|
+
this.JST || (this.JST = {});
|
22
|
+
this.JST["templates/comment"] = function (locals) { function html_escape(text) {
|
23
|
+
return (text + "").
|
24
|
+
replace(/&/g, "&").
|
25
|
+
replace(/</g, "<").
|
26
|
+
replace(/>/g, ">").
|
27
|
+
replace(/\"/g, """);
|
28
|
+
}
|
29
|
+
with(locals || {}) {
|
30
|
+
try {
|
31
|
+
var _$output="<div class=\"comment\"><div class=\"author\">" +
|
32
|
+
html_escape(author) +
|
33
|
+
"</div><div class=\"text\">" +
|
34
|
+
html_escape(text) +
|
35
|
+
"</div></div>";
|
36
|
+
return _$output; } catch (e) {
|
37
|
+
return "\n<pre class='error'>" + html_escape(e.stack) + "</pre>\n";
|
38
|
+
}
|
39
|
+
}
|
40
|
+
};
|
41
|
+
}).call(this);
|
42
|
+
```
|
43
|
+
|
44
|
+
Then you access that template like so:
|
45
|
+
|
46
|
+
```javascript
|
47
|
+
var commentView = JST['templates/comment'];
|
48
|
+
// ...
|
49
|
+
// Somewhere in your code later:
|
50
|
+
var html = commentView({author: 'Dima', text: 'Looks nice'});
|
51
|
+
```
|
52
|
+
|
53
|
+
# Installation
|
8
54
|
|
9
55
|
```ruby
|
10
56
|
# Gemfile
|
@@ -24,6 +70,21 @@ bundle
|
|
24
70
|
4. Use the template from the JavaScript: `JST['my-template']({your: 'local', variables: 'go here'})`
|
25
71
|
|
26
72
|
|
73
|
+
# Configuration
|
74
|
+
|
75
|
+
## Custom HTML escape
|
76
|
+
|
77
|
+
You can change the escape function from the built-in, autogenerated to your own:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
# Set it somewhere, before generating the template.
|
81
|
+
# Good place can be a Rails initializer.
|
82
|
+
RubyHamlJs::Template.custom_escape = "App.escape_html"
|
83
|
+
```
|
84
|
+
|
85
|
+
This will use the given function and will not generate custom escape code inside each template.
|
86
|
+
But you need to make sure that the function is defined before using the templates in JavaScript.
|
87
|
+
|
27
88
|
# Development
|
28
89
|
|
29
90
|
- Source hosted at [GitHub](https://github.com/dnagir/ruby-haml-js)
|
@@ -44,16 +44,18 @@ module RubyHamlJs
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def js_custom_escape
|
47
|
-
|
48
|
-
escape_function = nil
|
47
|
+
escape_function = self.class.custom_escape
|
49
48
|
escape_function ? "'#{js_string escape_function}'" : 'null'
|
50
49
|
end
|
51
50
|
|
52
51
|
class << self
|
52
|
+
attr_accessor :custom_escape
|
53
|
+
|
53
54
|
def haml_source
|
54
55
|
# Haml source is an asset
|
55
56
|
@haml_source ||= IO.read File.expand_path('../../../vendor/assets/javascripts/haml.js', __FILE__)
|
56
57
|
end
|
58
|
+
|
57
59
|
end
|
58
60
|
|
59
61
|
end
|
data/lib/ruby-haml-js/version.rb
CHANGED
data/ruby-haml-js.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.email = ["dnagir@gmail.com"]
|
10
10
|
s.homepage = "http://github.com/dnagir/ruby-haml-js"
|
11
11
|
s.summary = %q{Precompile HAML-JS templates with or without Rails 3.1 assets pipeline}
|
12
|
-
s.description = %q{ruby-haml-js provides a Tilt template that you can use
|
12
|
+
s.description = %q{ruby-haml-js provides a Tilt template that you can use to compile HAML-JS templates into JS functions. Handy for using it wth Bakcbone.js, Spine.js etc.}
|
13
13
|
|
14
14
|
s.rubyforge_project = "ruby-haml-js"
|
15
15
|
|
data/spec/template_spec.rb
CHANGED
@@ -18,6 +18,13 @@ describe RubyHamlJs::Template do
|
|
18
18
|
|
19
19
|
it { should include "function (locals) {" }
|
20
20
|
it { should include 'function html_escape' }
|
21
|
+
|
22
|
+
context "with custom HTML escape function" do
|
23
|
+
before { @original_escape = RubyHamlJs::Template.custom_escape }
|
24
|
+
before { RubyHamlJs::Template.custom_escape = "App.custom_escape" }
|
25
|
+
after { RubyHamlJs::Template.custom_escape = @original_escape }
|
26
|
+
it { should include "App.custom_escape(" }
|
27
|
+
end
|
21
28
|
end
|
22
29
|
|
23
30
|
describe 'executing' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-haml-js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-11-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sprockets
|
16
|
-
requirement: &
|
16
|
+
requirement: &70093427604120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70093427604120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: execjs
|
27
|
-
requirement: &
|
27
|
+
requirement: &70093427628260 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70093427628260
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70093427627680 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70093427627680
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rails
|
49
|
-
requirement: &
|
49
|
+
requirement: &70093427627020 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,9 @@ dependencies:
|
|
54
54
|
version: 3.1.1
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
58
|
-
description: ruby-haml-js provides a Tilt template that you can use
|
59
|
-
|
60
|
-
etc.
|
57
|
+
version_requirements: *70093427627020
|
58
|
+
description: ruby-haml-js provides a Tilt template that you can use to compile HAML-JS
|
59
|
+
templates into JS functions. Handy for using it wth Bakcbone.js, Spine.js etc.
|
61
60
|
email:
|
62
61
|
- dnagir@gmail.com
|
63
62
|
executables: []
|
@@ -66,7 +65,6 @@ extra_rdoc_files: []
|
|
66
65
|
files:
|
67
66
|
- .gitignore
|
68
67
|
- Gemfile
|
69
|
-
- Gemfile.lock
|
70
68
|
- HISTORY.md
|
71
69
|
- MIT-LICENSE
|
72
70
|
- README.md
|
@@ -129,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
127
|
version: '0'
|
130
128
|
segments:
|
131
129
|
- 0
|
132
|
-
hash:
|
130
|
+
hash: 387273018365343158
|
133
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
132
|
none: false
|
135
133
|
requirements:
|
@@ -138,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
136
|
version: '0'
|
139
137
|
segments:
|
140
138
|
- 0
|
141
|
-
hash:
|
139
|
+
hash: 387273018365343158
|
142
140
|
requirements: []
|
143
141
|
rubyforge_project: ruby-haml-js
|
144
142
|
rubygems_version: 1.8.10
|
data/Gemfile.lock
DELETED
@@ -1,120 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
ruby-haml-js (0.0.1)
|
5
|
-
execjs
|
6
|
-
sprockets (>= 2.0.0)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: http://rubygems.org/
|
10
|
-
specs:
|
11
|
-
actionmailer (3.1.1)
|
12
|
-
actionpack (= 3.1.1)
|
13
|
-
mail (~> 2.3.0)
|
14
|
-
actionpack (3.1.1)
|
15
|
-
activemodel (= 3.1.1)
|
16
|
-
activesupport (= 3.1.1)
|
17
|
-
builder (~> 3.0.0)
|
18
|
-
erubis (~> 2.7.0)
|
19
|
-
i18n (~> 0.6)
|
20
|
-
rack (~> 1.3.2)
|
21
|
-
rack-cache (~> 1.1)
|
22
|
-
rack-mount (~> 0.8.2)
|
23
|
-
rack-test (~> 0.6.1)
|
24
|
-
sprockets (~> 2.0.2)
|
25
|
-
activemodel (3.1.1)
|
26
|
-
activesupport (= 3.1.1)
|
27
|
-
builder (~> 3.0.0)
|
28
|
-
i18n (~> 0.6)
|
29
|
-
activerecord (3.1.1)
|
30
|
-
activemodel (= 3.1.1)
|
31
|
-
activesupport (= 3.1.1)
|
32
|
-
arel (~> 2.2.1)
|
33
|
-
tzinfo (~> 0.3.29)
|
34
|
-
activeresource (3.1.1)
|
35
|
-
activemodel (= 3.1.1)
|
36
|
-
activesupport (= 3.1.1)
|
37
|
-
activesupport (3.1.1)
|
38
|
-
multi_json (~> 1.0)
|
39
|
-
arel (2.2.1)
|
40
|
-
builder (3.0.0)
|
41
|
-
coderay (0.9.8)
|
42
|
-
diff-lcs (1.1.2)
|
43
|
-
erubis (2.7.0)
|
44
|
-
execjs (1.2.9)
|
45
|
-
multi_json (~> 1.0)
|
46
|
-
hike (1.2.1)
|
47
|
-
i18n (0.6.0)
|
48
|
-
json (1.6.1)
|
49
|
-
mail (2.3.0)
|
50
|
-
i18n (>= 0.4.0)
|
51
|
-
mime-types (~> 1.16)
|
52
|
-
treetop (~> 1.4.8)
|
53
|
-
method_source (0.6.0)
|
54
|
-
ruby_parser (>= 2.0.5)
|
55
|
-
mime-types (1.16)
|
56
|
-
multi_json (1.0.3)
|
57
|
-
polyglot (0.3.2)
|
58
|
-
pry (0.9.3)
|
59
|
-
coderay (>= 0.9.8)
|
60
|
-
method_source (>= 0.6.0)
|
61
|
-
ruby_parser (>= 2.0.5)
|
62
|
-
slop (~> 1.9.0)
|
63
|
-
rack (1.3.5)
|
64
|
-
rack-cache (1.1)
|
65
|
-
rack (>= 0.4)
|
66
|
-
rack-mount (0.8.3)
|
67
|
-
rack (>= 1.0.0)
|
68
|
-
rack-ssl (1.3.2)
|
69
|
-
rack
|
70
|
-
rack-test (0.6.1)
|
71
|
-
rack (>= 1.0)
|
72
|
-
rails (3.1.1)
|
73
|
-
actionmailer (= 3.1.1)
|
74
|
-
actionpack (= 3.1.1)
|
75
|
-
activerecord (= 3.1.1)
|
76
|
-
activeresource (= 3.1.1)
|
77
|
-
activesupport (= 3.1.1)
|
78
|
-
bundler (~> 1.0)
|
79
|
-
railties (= 3.1.1)
|
80
|
-
railties (3.1.1)
|
81
|
-
actionpack (= 3.1.1)
|
82
|
-
activesupport (= 3.1.1)
|
83
|
-
rack-ssl (~> 1.3.2)
|
84
|
-
rake (>= 0.8.7)
|
85
|
-
rdoc (~> 3.4)
|
86
|
-
thor (~> 0.14.6)
|
87
|
-
rake (0.9.2)
|
88
|
-
rdoc (3.10)
|
89
|
-
json (~> 1.4)
|
90
|
-
rspec (2.6.0)
|
91
|
-
rspec-core (~> 2.6.0)
|
92
|
-
rspec-expectations (~> 2.6.0)
|
93
|
-
rspec-mocks (~> 2.6.0)
|
94
|
-
rspec-core (2.6.4)
|
95
|
-
rspec-expectations (2.6.0)
|
96
|
-
diff-lcs (~> 1.1.2)
|
97
|
-
rspec-mocks (2.6.0)
|
98
|
-
ruby_parser (2.1.0)
|
99
|
-
sexp_processor (~> 3.0)
|
100
|
-
sexp_processor (3.0.5)
|
101
|
-
slop (1.9.1)
|
102
|
-
sprockets (2.0.3)
|
103
|
-
hike (~> 1.2)
|
104
|
-
rack (~> 1.0)
|
105
|
-
tilt (~> 1.1, != 1.3.0)
|
106
|
-
thor (0.14.6)
|
107
|
-
tilt (1.3.3)
|
108
|
-
treetop (1.4.10)
|
109
|
-
polyglot
|
110
|
-
polyglot (>= 0.3.1)
|
111
|
-
tzinfo (0.3.30)
|
112
|
-
|
113
|
-
PLATFORMS
|
114
|
-
ruby
|
115
|
-
|
116
|
-
DEPENDENCIES
|
117
|
-
pry
|
118
|
-
rails (>= 3.1.1)
|
119
|
-
rspec
|
120
|
-
ruby-haml-js!
|