haml-coffee 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
@@ -0,0 +1,3 @@
1
+ [submodule "vendor/haml-coffee"]
2
+ path = vendor/haml-coffee
3
+ url = https://github.com/9elements/haml-coffee.git
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@ruby-haml-coffee --create
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/NOTICE ADDED
@@ -0,0 +1,2 @@
1
+ ruby-haml-coffee
2
+ Copyright 2011 Brennan Frydl (http://bfrydl.com/)
@@ -0,0 +1,34 @@
1
+ # ruby-haml-coffee
2
+
3
+ ruby-haml-coffee wraps [haml-coffee](http://github.com/9elements/haml-coffee) to generate
4
+ JavaScript templates from HAML files using CoffeeScript instead of Ruby for the inline
5
+ code.
6
+
7
+ The following features are provided:
8
+
9
+ * Basic compiler in `HamlCoffee.compile` to compile source directly to JavaScript
10
+ * Template for [Tilt](https://github.com/rtomayko/tilt.git)
11
+ * Rails Engine for use with the 3.1 asset pipeline
12
+
13
+ ## As a Rails 3.1 Engine
14
+
15
+ To install, add ruby-haml-coffee to your Gemfile:
16
+
17
+ group :assets do
18
+ ...
19
+ gem 'ruby-haml-coffee'
20
+ ...
21
+ end
22
+
23
+ Now create template files somewhere in `app/assets/javascripts` with the extension
24
+ `.js.haml-coffee`. Templates are added to the `window.HAML` object according to their
25
+ path. For example, `app/assets/javascripts/templates/user/show.js.haml-coffee` is accessible
26
+ through `window.HAML.templates.user.show`:
27
+
28
+ //= require_tree ./templates
29
+
30
+ $(function() {
31
+ user = getCurrentUser();
32
+
33
+ $(body).append(HAML.templates.user.show(user));
34
+ });
@@ -0,0 +1,83 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011 Brennan Frydl (http://bfrydl.com/)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'bundler/gem_tasks'
18
+ require 'rspec/core/rake_task'
19
+
20
+ task :default => :build
21
+
22
+ RSpec::Core::RakeTask.new
23
+
24
+ task :generatejs do
25
+ input = Pathname.new('vendor/haml-coffee/lib/compiler.js')
26
+ output = Pathname.new('lib/haml_coffee/haml-coffee.js')
27
+
28
+ status = {}
29
+ contents = []
30
+
31
+ root = input.realpath.dirname
32
+ compiler_module = input.basename.to_s
33
+
34
+ process_file = proc do |name, path|
35
+ case status[name]
36
+ when :processed
37
+ next
38
+ when :processing
39
+ raise StandardError, "Cyclic dependency detected in #{name}: #{status}"
40
+ else
41
+ status[name] = :processing
42
+ end
43
+
44
+ content = path.read
45
+
46
+ content.gsub!(/require\('([^']*)'\)/) do |match|
47
+ _path = Pathname.new(File.expand_path(File.join('../', "#{$1}.js"), path))
48
+ _name = _path.relative_path_from(root).to_s
49
+
50
+ process_file.call(_name, _path)
51
+
52
+ "modules[#{_name.inspect}]"
53
+ end
54
+
55
+ content.gsub!(/module\.exports/, "modules[#{name.inspect}]")
56
+
57
+ contents << [name, content]
58
+ status[name] = :processed
59
+ end
60
+
61
+ process_file.call(compiler_module, input)
62
+
63
+ output.open('w') do |file|
64
+ file.puts 'compile = (function() {'
65
+ file.puts ' var modules = {};'
66
+
67
+ contents.each do |(name, content)|
68
+ file.puts
69
+ file.puts " // #{name}"
70
+ file.puts content.gsub(/^/, ' ').chomp
71
+ end
72
+
73
+ file.puts
74
+ file.puts " var Compiler = modules[#{compiler_module.inspect}];"
75
+ file.puts ' return function(namespace, filename, source) {'
76
+ file.puts ' var c = new Compiler(c);'
77
+ file.puts ' c.parse(source);'
78
+ file.puts ' return c.render(filename, namespace);'
79
+ file.puts ' }'
80
+
81
+ file.puts('}).call(this);')
82
+ end
83
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011 Brennan Frydl (http://bfrydl.com/)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ $:.push File.expand_path('../lib', __FILE__)
18
+
19
+ Gem::Specification.new do |s|
20
+ s.name = 'haml-coffee'
21
+ s.version = '0.1.0'
22
+ s.authors = ['Brennan Frydl']
23
+ s.email = ['me@bfrydl.com']
24
+ s.homepage = 'http://github.com/bfrydl/ruby-haml-coffee'
25
+ s.summary = %q{Ruby wrapper around the JavaScript haml-coffee compiler}
26
+ s.description = %q{ruby-haml-coffee is a Ruby wrapper around the JavaScript haml-coffee compiler.}
27
+
28
+ s.files = `git ls-files`.split("\n")
29
+ s.test_files = `git ls-files -- spec/*`.split("\n")
30
+ s.require_paths = ['lib']
31
+
32
+ s.add_development_dependency 'rspec'
33
+
34
+ s.add_runtime_dependency 'execjs'
35
+ s.add_runtime_dependency 'coffee-script'
36
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011 Brennan Frydl (http://bfrydl.com/)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'haml_coffee'
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011 Brennan Frydl (http://bfrydl.com/)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'execjs'
18
+ require 'coffee-script'
19
+
20
+ require 'haml_coffee/tilt_template' if defined?(Tilt)
21
+ require 'haml_coffee/engine' if defined?(Rails)
22
+
23
+ module HamlCoffee
24
+ SCRIPT_PATH = File.read(File.expand_path('../haml_coffee/haml-coffee.js', __FILE__))
25
+
26
+ def self.compile(source, options = {})
27
+ @context ||= ExecJS.compile(SCRIPT_PATH)
28
+
29
+ namespace = options[:namespace] || 'HAML'
30
+ filename = options[:filename] || 'template'
31
+
32
+ CoffeeScript.compile(@context.call('compile', namespace, filename, source))
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011 Brennan Frydl (http://bfrydl.com/)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module HamlCoffee
18
+ class Engine < Rails::Engine
19
+ initializer 'sprockets.haml_coffee', :after => 'sprockets.environment' do |app|
20
+ app.assets.register_engine('.haml-coffee', TiltTemplate)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,411 @@
1
+ compile = (function() {
2
+ var modules = {};
3
+
4
+ // helper.js
5
+ (function() {
6
+ modules["helper.js"] = {
7
+ whitespace: function(n) {
8
+ var a;
9
+ n = n * 2;
10
+ a = [];
11
+ while (a.length < n) {
12
+ a.push(' ');
13
+ }
14
+ return a.join('');
15
+ },
16
+ escape: function(s) {
17
+ return s.replace(/"/g, '\\"');
18
+ }
19
+ };
20
+ }).call(this);
21
+
22
+ // nodes/node.js
23
+ (function() {
24
+ var Node, e, w;
25
+ e = modules["helper.js"].escape;
26
+ w = modules["helper.js"].whitespace;
27
+ modules["nodes/node.js"] = Node = (function() {
28
+ function Node(expression, block_level, code_block_level) {
29
+ this.expression = expression;
30
+ this.block_level = block_level;
31
+ this.code_block_level = code_block_level;
32
+ this.children = [];
33
+ this.opener = this.closer = "";
34
+ this.cw = w(this.code_block_level);
35
+ this.hw = w(this.block_level - this.code_block_level);
36
+ }
37
+ Node.prototype.addChild = function(child) {
38
+ this.children.push(child);
39
+ return this;
40
+ };
41
+ Node.prototype.getOpener = function() {
42
+ this.evaluateIfNecessary();
43
+ return this.opener;
44
+ };
45
+ Node.prototype.getCloser = function() {
46
+ this.evaluateIfNecessary();
47
+ return this.closer;
48
+ };
49
+ Node.prototype.evaluateIfNecessary = function() {
50
+ if (!this.evaluated) {
51
+ this.evaluate();
52
+ }
53
+ return this.evaluated = true;
54
+ };
55
+ Node.prototype.evaluate = function() {};
56
+ Node.prototype.render = function() {
57
+ var child, output, _i, _len, _ref;
58
+ output = "" + (this.getOpener()) + "\n";
59
+ _ref = this.children;
60
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
61
+ child = _ref[_i];
62
+ output += "" + (child.render());
63
+ }
64
+ if (this.getCloser().length > 0) {
65
+ output += "" + (this.getCloser()) + "\n";
66
+ }
67
+ return output;
68
+ };
69
+ return Node;
70
+ })();
71
+ }).call(this);
72
+
73
+ // nodes/text.js
74
+ (function() {
75
+ var Node, Text, e;
76
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
77
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
78
+ function ctor() { this.constructor = child; }
79
+ ctor.prototype = parent.prototype;
80
+ child.prototype = new ctor;
81
+ child.__super__ = parent.prototype;
82
+ return child;
83
+ };
84
+ Node = modules["nodes/node.js"];
85
+ e = modules["helper.js"].escape;
86
+ modules["nodes/text.js"] = Text = (function() {
87
+ function Text() {
88
+ Text.__super__.constructor.apply(this, arguments);
89
+ }
90
+ __extends(Text, Node);
91
+ Text.prototype.evaluate = function() {
92
+ return this.opener = "" + this.cw + "o.push \"" + this.hw + this.expression + "\"";
93
+ };
94
+ return Text;
95
+ })();
96
+ }).call(this);
97
+
98
+ // nodes/haml.js
99
+ (function() {
100
+ var Haml, Node, e;
101
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
102
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
103
+ function ctor() { this.constructor = child; }
104
+ ctor.prototype = parent.prototype;
105
+ child.prototype = new ctor;
106
+ child.__super__ = parent.prototype;
107
+ return child;
108
+ };
109
+ Node = modules["nodes/node.js"];
110
+ e = modules["helper.js"].escape;
111
+ modules["nodes/haml.js"] = Haml = (function() {
112
+ function Haml() {
113
+ Haml.__super__.constructor.apply(this, arguments);
114
+ }
115
+ __extends(Haml, Node);
116
+ Haml.selfCloseTags = ["meta", "img", "link", "br", "hr", "input", "area", "base"];
117
+ Haml.prototype.evaluate = function() {
118
+ var htmlTagPrefix, parsedExpression;
119
+ parsedExpression = this.parseExpression(this.expression);
120
+ htmlTagPrefix = this.buildHtmlTag(parsedExpression);
121
+ if (this.isSelfClosing(parsedExpression.tag)) {
122
+ this.opener = "" + this.cw + "o.push \"" + this.hw + (e(htmlTagPrefix)) + ">";
123
+ this.closer = "" + this.cw + "o.push \"" + this.hw + "</" + parsedExpression.tag + ">\"";
124
+ } else {
125
+ this.opener = "" + this.cw + "o.push \"" + this.hw + (e(htmlTagPrefix)) + " />";
126
+ }
127
+ if (parsedExpression.assignment) {
128
+ this.opener += '#{' + parsedExpression.assignment + '}';
129
+ }
130
+ return this.opener += '"';
131
+ };
132
+ Haml.prototype.parseExpression = function(exp) {
133
+ var optionProperties, tagProperties;
134
+ tagProperties = this.parseTag(exp);
135
+ optionProperties = this.parseOptions(exp);
136
+ return {
137
+ tag: tagProperties.tag,
138
+ ids: tagProperties.ids,
139
+ classes: tagProperties.classes,
140
+ pairs: optionProperties.pairs,
141
+ assignment: optionProperties.assignment
142
+ };
143
+ };
144
+ Haml.prototype.buildHtmlTag = function(parsedExpression) {
145
+ var pair, tagParts, _i, _len, _ref;
146
+ tagParts = ["<" + parsedExpression.tag];
147
+ if (parsedExpression.ids) {
148
+ tagParts.push("id=\"" + (parsedExpression.ids.join(' ')) + "\"");
149
+ }
150
+ if (parsedExpression.classes) {
151
+ tagParts.push("class=\"" + (parsedExpression.classes.join(' ')) + "\"");
152
+ }
153
+ if (parsedExpression.pairs.length > 0) {
154
+ _ref = parsedExpression.pairs;
155
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
156
+ pair = _ref[_i];
157
+ tagParts.push("" + pair.key + "=" + pair.value);
158
+ }
159
+ }
160
+ return tagParts.join(' ');
161
+ };
162
+ Haml.prototype.parseTag = function(exp) {
163
+ var classes, id, ids, klass, tag, tagExp;
164
+ try {
165
+ tagExp = exp.match(/^((?:[.#%][a-z_\-][a-z0-9_:\-]*)+)(.*)$/i)[1];
166
+ tag = tagExp.match(/\%([a-z_\-][a-z0-9_:\-]*)/i);
167
+ tag = tag ? tag[1] : 'div';
168
+ ids = tagExp.match(/\#([a-z_\-][a-z0-9_\-]*)/gi);
169
+ classes = tagExp.match(/\.([a-z_\-][a-z0-9_\-]*)/gi);
170
+ return {
171
+ tag: tag,
172
+ ids: ids ? (function() {
173
+ var _i, _len, _results;
174
+ _results = [];
175
+ for (_i = 0, _len = ids.length; _i < _len; _i++) {
176
+ id = ids[_i];
177
+ _results.push(id.substr(1));
178
+ }
179
+ return _results;
180
+ })() : void 0,
181
+ classes: classes ? (function() {
182
+ var _i, _len, _results;
183
+ _results = [];
184
+ for (_i = 0, _len = classes.length; _i < _len; _i++) {
185
+ klass = classes[_i];
186
+ _results.push(klass.substr(1));
187
+ }
188
+ return _results;
189
+ })() : void 0
190
+ };
191
+ } catch (error) {
192
+ throw "Unable to parse tag from " + exp + ": " + error;
193
+ }
194
+ };
195
+ Haml.prototype.parseOptions = function(exp) {
196
+ var assignment, attributesExp, optionsExp, pairs;
197
+ optionsExp = exp.match(/[\{\s=].*/i);
198
+ if (optionsExp) {
199
+ optionsExp = optionsExp[0];
200
+ if (optionsExp[0] === "{") {
201
+ attributesExp = optionsExp.match(/\{(.*)\}/);
202
+ if (attributesExp) {
203
+ attributesExp = attributesExp[1];
204
+ }
205
+ assignment = optionsExp.match(/\{.*\}\s*=\s*(.*)/);
206
+ } else {
207
+ assignment = optionsExp.match(/\.*=\s*(.*)/);
208
+ }
209
+ if (assignment) {
210
+ assignment = assignment[1];
211
+ }
212
+ pairs = this.parseAttributes(attributesExp);
213
+ }
214
+ return {
215
+ assignment: assignment,
216
+ pairs: pairs || []
217
+ };
218
+ };
219
+ Haml.prototype.parseAttributes = function(attributesExp) {
220
+ var attribute, attributes, key, pair, pairs, result, value, valueIsLiteral, _i, _len;
221
+ pairs = [];
222
+ if (attributesExp == null) {
223
+ return pairs;
224
+ }
225
+ attributes = attributesExp.match(/(:[^\s|=]+\s*=>\s*(("[^"]+")|('[^']+')|[^\s]+))/g);
226
+ for (_i = 0, _len = attributes.length; _i < _len; _i++) {
227
+ attribute = attributes[_i];
228
+ pair = attribute.split('=>');
229
+ key = pair[0].trim().substr(1);
230
+ result = key.match(/^'(.+)'$/);
231
+ if (result) {
232
+ key = result[1];
233
+ }
234
+ value = pair[1].trim();
235
+ valueIsLiteral = value.match(/("|')/);
236
+ pairs.push({
237
+ key: key,
238
+ value: valueIsLiteral ? value : '"#{' + value + '}"'
239
+ });
240
+ }
241
+ return pairs;
242
+ };
243
+ Haml.prototype.isSelfClosing = function(tag) {
244
+ return Haml.selfCloseTags.indexOf(tag) === -1;
245
+ };
246
+ return Haml;
247
+ })();
248
+ }).call(this);
249
+
250
+ // nodes/code.js
251
+ (function() {
252
+ var Code, Node, e;
253
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
254
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
255
+ function ctor() { this.constructor = child; }
256
+ ctor.prototype = parent.prototype;
257
+ child.prototype = new ctor;
258
+ child.__super__ = parent.prototype;
259
+ return child;
260
+ };
261
+ Node = modules["nodes/node.js"];
262
+ e = modules["helper.js"].escape;
263
+ modules["nodes/code.js"] = Code = (function() {
264
+ function Code() {
265
+ Code.__super__.constructor.apply(this, arguments);
266
+ }
267
+ __extends(Code, Node);
268
+ Code.prototype.evaluate = function() {
269
+ var code, expression, identifier, _ref;
270
+ _ref = this.expression.match(/(-|=)\s(.*)/), expression = _ref[0], identifier = _ref[1], code = _ref[2];
271
+ return this.opener = identifier === '-' ? "" + this.cw + code : "" + this.cw + "o.push \"" + this.hw + "\#{" + code + "}\"";
272
+ };
273
+ return Code;
274
+ })();
275
+ }).call(this);
276
+
277
+ // compiler.js
278
+ (function() {
279
+ var Code, Compiler, Haml, Node, Text;
280
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
281
+ Node = modules["nodes/node.js"];
282
+ Text = modules["nodes/text.js"];
283
+ Haml = modules["nodes/haml.js"];
284
+ Code = modules["nodes/code.js"];
285
+ modules["compiler.js"] = Compiler = (function() {
286
+ function Compiler() {}
287
+ Compiler.prototype.node_factory = function(expression, current_block_level, current_code_block_level) {
288
+ var node;
289
+ if (expression.match(/^(-|=)\s*(.*)/)) {
290
+ node = new Code(expression, current_block_level, current_code_block_level);
291
+ } else if (expression.match(/^(%|#|\.)(.*)/)) {
292
+ node = new Haml(expression, current_block_level, current_code_block_level);
293
+ } else {
294
+ node = new Text(expression, current_block_level, current_code_block_level);
295
+ }
296
+ return node;
297
+ };
298
+ Compiler.prototype.update_code_block_level = function(node) {
299
+ if (node instanceof Code) {
300
+ return this.current_code_block_level = node.code_block_level + 1;
301
+ } else {
302
+ return this.current_code_block_level = node.code_block_level;
303
+ }
304
+ };
305
+ Compiler.prototype.indent_changed = function() {
306
+ return this.current_indent !== this.previous_indent;
307
+ };
308
+ Compiler.prototype.is_indent = function() {
309
+ return this.current_indent > this.previous_indent;
310
+ };
311
+ Compiler.prototype.update_tab_size = function() {
312
+ if (this.tab_size === 0) {
313
+ return this.tab_size = this.current_indent - this.previous_indent;
314
+ }
315
+ };
316
+ Compiler.prototype.update_block_level = function() {
317
+ this.current_block_level = this.current_indent / this.tab_size;
318
+ if (this.current_block_level - Math.floor(this.current_block_level) > 0) {
319
+ throw "Indentation error in line " + this.line_number;
320
+ }
321
+ if ((this.current_indent - this.previous_indent) / this.tab_size > 1) {
322
+ throw "Block level too deep in line " + this.line_number;
323
+ }
324
+ return this.delta = this.previous_block_level - this.current_block_level;
325
+ };
326
+ Compiler.prototype.push_parent = function() {
327
+ this.stack.push(this.parent_node);
328
+ return this.parent_node = this.node;
329
+ };
330
+ Compiler.prototype.pop_parent = function() {
331
+ var i, _ref, _results;
332
+ _results = [];
333
+ for (i = 0, _ref = this.delta - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
334
+ _results.push(this.parent_node = this.stack.pop());
335
+ }
336
+ return _results;
337
+ };
338
+ Compiler.prototype.parse = function(source) {
339
+ this.line_number = 0;
340
+ this.previous_indent = 0;
341
+ this.tab_size = 0;
342
+ this.current_block_level = this.previous_block_level = 0;
343
+ this.current_code_block_level = this.previous_code_block_level = 2;
344
+ this.root = this.parent_node = new Node("", this.current_block_level, this.current_code_block_level);
345
+ this.node = null;
346
+ this.stack = [];
347
+ return source.split("\n").forEach(__bind(function(line) {
348
+ var expression, result, whitespace;
349
+ result = line.match(/^(\s*)(.*)/);
350
+ whitespace = result[1];
351
+ expression = result[2];
352
+ if (expression.length > 0) {
353
+ if (!expression.match(/^\//)) {
354
+ this.current_indent = whitespace.length;
355
+ if (this.indent_changed()) {
356
+ this.update_tab_size();
357
+ this.update_block_level();
358
+ if (this.is_indent()) {
359
+ this.push_parent();
360
+ } else {
361
+ this.pop_parent();
362
+ }
363
+ this.update_code_block_level(this.parent_node);
364
+ }
365
+ this.node = this.node_factory(expression, this.current_block_level, this.current_code_block_level);
366
+ this.parent_node.addChild(this.node);
367
+ this.previous_block_level = this.current_block_level;
368
+ this.previous_indent = this.current_indent;
369
+ this.line_number++;
370
+ }
371
+ }
372
+ }, this));
373
+ };
374
+ Compiler.prototype.parameterize = function(s) {
375
+ s = s.replace(/(\s|-)+/g, "_");
376
+ return s;
377
+ };
378
+ Compiler.prototype.render = function(filename, namespace) {
379
+ var name, output, segment, segments, _i, _len;
380
+ if (namespace != null) {
381
+ namespace;
382
+ } else {
383
+ namespace = "HAML";
384
+ };
385
+ output = "window." + namespace + " ?= {}\n";
386
+ segments = this.parameterize(filename).split('/');
387
+ name = segments.pop();
388
+ for (_i = 0, _len = segments.length; _i < _len; _i++) {
389
+ segment = segments[_i];
390
+ namespace += "." + segment;
391
+ output += "window." + namespace + " ?= {}\n";
392
+ }
393
+ output += "window." + namespace + "." + name + " = (context) ->\n";
394
+ output += " fn = (context) ->\n";
395
+ output += " o = []";
396
+ output += this.root.render();
397
+ output += " return o.join(\"\\n\")\n";
398
+ output += " return fn.call(context)\n";
399
+ return output;
400
+ };
401
+ return Compiler;
402
+ })();
403
+ }).call(this);
404
+
405
+ var Compiler = modules["compiler.js"];
406
+ return function(namespace, filename, source) {
407
+ var c = new Compiler(c);
408
+ c.parse(source);
409
+ return c.render(filename, namespace);
410
+ }
411
+ }).call(this);
@@ -0,0 +1,37 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011 Brennan Frydl (http://bfrydl.com/)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module HamlCoffee
18
+ class TiltTemplate < Tilt::Template
19
+ def self.default_mime_type
20
+ 'application/javascript'
21
+ end
22
+
23
+ def prepare ; end
24
+
25
+ def evaluate(scope, locals = {}, &block)
26
+ filename = if scope.respond_to?(:logical_path) # Sprockets
27
+ scope.logical_path
28
+ elsif scope.respond_to?(:filename)
29
+ scope.filename
30
+ elsif locals.key?(:filename)
31
+ locals[:filename]
32
+ end
33
+
34
+ HamlCoffee.compile(data, :filename => filename)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ require 'haml-coffee'
2
+
3
+ describe HamlCoffee do
4
+ it 'generates a template using haml-coffee.js' do
5
+ source = "%div{ :class => 'test' }\n" +
6
+ " - if @which == 'first'\n" +
7
+ " Hello!\n" +
8
+ " - else\n" +
9
+ " Good-bye!"
10
+
11
+ result = "<div class='test'>\n" +
12
+ "$text\n" +
13
+ "</div>"
14
+
15
+ compiled = HamlCoffee.compile(source, :namespace => 'Testing', :filename => 'template')
16
+ compiled = "window = {};\n#{compiled}"
17
+
18
+ context = ExecJS.compile(compiled)
19
+
20
+ context.call('window.Testing.template', :which => 'first').should == result.sub('$text', 'Hello!')
21
+ context.call('window.Testing.template', :which => 'second').should == result.sub('$text', 'Good-bye!')
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haml-coffee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brennan Frydl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &18691300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *18691300
25
+ - !ruby/object:Gem::Dependency
26
+ name: execjs
27
+ requirement: &18690860 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *18690860
36
+ - !ruby/object:Gem::Dependency
37
+ name: coffee-script
38
+ requirement: &18690440 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *18690440
47
+ description: ruby-haml-coffee is a Ruby wrapper around the JavaScript haml-coffee
48
+ compiler.
49
+ email:
50
+ - me@bfrydl.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .gitmodules
57
+ - .rvmrc
58
+ - Gemfile
59
+ - LICENSE
60
+ - NOTICE
61
+ - README.md
62
+ - Rakefile
63
+ - haml-coffee.gemspec
64
+ - lib/haml-coffee.rb
65
+ - lib/haml_coffee.rb
66
+ - lib/haml_coffee/engine.rb
67
+ - lib/haml_coffee/haml-coffee.js
68
+ - lib/haml_coffee/tilt_template.rb
69
+ - spec/haml_coffee_spec.rb
70
+ homepage: http://github.com/bfrydl/ruby-haml-coffee
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.5
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Ruby wrapper around the JavaScript haml-coffee compiler
94
+ test_files:
95
+ - spec/haml_coffee_spec.rb