herman-haml-sprockets 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ *swp
2
+ *~
3
+ *bak
4
+ *.gem
5
+ .bundle
6
+ Gemfile.lock
7
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in haml-sprockets.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2011 Vagmi Mudumbai
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
@@ -0,0 +1,33 @@
1
+ # Using HAML-JS with Sprockets and Rails 3.1
2
+
3
+ ## About
4
+
5
+ Rails 3.1 uses sprockets for its asset pipeline. Rails 3.1 has shown considerable love for SCSS and SASS but has left [HAML] out. We at [Dharana Software Innovations][] are die hard HAML fans. We beleive that it is the best templating language around. So we would like to have it as a part of our client side toolkit as well. The [haml-js] project does exactly that. It was written for Node.js but works well with the browser too.
6
+
7
+ ## How to use it?
8
+
9
+ The gem includes [haml-js]. You would not have to download it separately. To use this gem, you need to do the following.
10
+
11
+ In the `Gemfile`, add the following line.
12
+
13
+ gem "haml-sprockets"
14
+
15
+ Now, you can create hamljs files under `app/assets/javascripts/templates` folder. You can create the templates folder, if it does not already exist.
16
+
17
+ // code for app/assets/javascripts/templates/hello.jst.hamljs
18
+ %h1 Hello HAML
19
+
20
+ You can now access the template anywhere in your javascript or coffeescript code.
21
+
22
+ JST["templates/hello"]()
23
+
24
+ This should give you back the string `"<h1>Hello HAML</h1>"`.
25
+
26
+ ## LICENSE
27
+
28
+ This is distributed under the MIT license.
29
+
30
+
31
+ [HAML]: http://haml-lang.com/
32
+ [haml-js]: https://github.com/creationix/haml-js
33
+ [Dharana Software Innovations]: http://www.dharanasoft.com/
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "haml-sprockets/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "herman-haml-sprockets"
7
+ s.version = Haml::Sprockets::VERSION
8
+ s.authors = ["vagmi", 'Herman Moreno']
9
+ s.email = ["vagmi.mudumbai@gmail.com", 'herman.moreno@crowdint.com']
10
+ s.homepage = "https://github.com/supherman/haml-sprockets"
11
+ s.summary = %q{Use the awesome haml-js javascript templating lib in Ruby}
12
+ s.description = %q{Use the JST processor and have haml code read in and appended to application.js}
13
+
14
+ s.rubyforge_project = "herman-haml-sprockets"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "tilt", "~> 1.3"
24
+ s.add_runtime_dependency "sprockets", "~> 2.0"
25
+ s.add_runtime_dependency "execjs", "~> 1.3.0"
26
+
27
+ s.add_development_dependency 'rspec'
28
+ end
@@ -0,0 +1,34 @@
1
+ require 'haml-sprockets/version'
2
+ require 'tilt'
3
+ require 'sprockets'
4
+ require 'execjs'
5
+
6
+ module Haml
7
+ module Sprockets
8
+ class Template < ::Tilt::Template
9
+ def self.engine_initialized?
10
+ true
11
+ end
12
+
13
+ def initialize_engine
14
+ end
15
+
16
+ def prepare
17
+ end
18
+
19
+ def evaluate(scope, locals, &block)
20
+ haml_code = data.dup
21
+ haml_code = haml_code.gsub(/\\/,"\\\\").gsub(/\'/,"\\\\'").gsub(/\n/,"\\n")
22
+
23
+ haml_path = File.expand_path("../../vendor/assets/javascripts/haml.js", __FILE__)
24
+ haml_lib = File.read(haml_path)
25
+ context = ExecJS.compile(haml_lib)
26
+ return context.eval("Haml.optimize(Haml.compile('#{haml_code}', {escapeHtmlByDefault: true}))")
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ Sprockets::Engines
33
+ Sprockets.register_engine '.hamljs', Haml::Sprockets::Template
34
+ require 'haml-sprockets/engine' if defined?(Rails)
@@ -0,0 +1,7 @@
1
+ module Haml
2
+ module Sprockets
3
+ class Engine < ::Rails::Engine
4
+ # just to get the vendor directories included into assets
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Haml
2
+ module Sprockets
3
+ VERSION = "0.0.8"
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Haml::Sprockets::Template do
4
+ describe 'evaluate' do
5
+ def process(data)
6
+ described_class.new { data }.render
7
+ end
8
+
9
+ describe 'string escaping' do
10
+ before do
11
+ Rails = double('Rails', :root => '/some/dir')
12
+ File.stub(:read)
13
+ @context = double('context')
14
+ ExecJS.stub(:compile).and_return(@context)
15
+ end
16
+
17
+ it 'should leave clean string alone' do
18
+ @context.should_receive(:eval).with(/'test'/)
19
+ process(%{test})
20
+ end
21
+
22
+ it 'should leave double quotes alone' do
23
+ @context.should_receive(:eval).with(/'test "test"'/)
24
+ process(%{test "test"})
25
+ end
26
+
27
+ it 'should double escape characters' do
28
+ @context.should_receive(:eval).with(/'test\\ntest'/)
29
+ process(%{test\ntest})
30
+ end
31
+
32
+ it 'should double escape single quotes' do
33
+ @context.should_receive(:eval).with(/'test \\'test\\''/)
34
+ process(%{test 'test'})
35
+ end
36
+
37
+ after do
38
+ Object.send(:remove_const, 'Rails')
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,2 @@
1
+ require 'haml-sprockets'
2
+
@@ -0,0 +1,651 @@
1
+ var Haml;
2
+
3
+ (function () {
4
+
5
+ var matchers, self_close_tags, embedder, forceXML, escaperName, escapeHtmlByDefault;
6
+
7
+ function html_escape(text) {
8
+ return (text + "").
9
+ replace(/&/g, "&amp;").
10
+ replace(/</g, "&lt;").
11
+ replace(/>/g, "&gt;").
12
+ replace(/\"/g, "&quot;");
13
+ }
14
+
15
+ function render_attribs(attribs) {
16
+ var key, value, result = [];
17
+ for (key in attribs) {
18
+ if (key !== '_content' && attribs.hasOwnProperty(key)) {
19
+ switch (attribs[key]) {
20
+ case 'undefined':
21
+ case 'false':
22
+ case 'null':
23
+ case '""':
24
+ break;
25
+ default:
26
+ try {
27
+ value = JSON.parse("[" + attribs[key] +"]")[0];
28
+ if (value === true) {
29
+ value = key;
30
+ } else if (typeof value === 'string' && embedder.test(value)) {
31
+ value = '" +\n' + parse_interpol(html_escape(value)) + ' +\n"';
32
+ } else {
33
+ value = html_escape(value);
34
+ }
35
+ result.push(" " + key + '=\\"' + value + '\\"');
36
+ } catch (e) {
37
+ result.push(" " + key + '=\\"" + '+escaperName+'(' + attribs[key] + ') + "\\"');
38
+ }
39
+ }
40
+ }
41
+ }
42
+ return result.join("");
43
+ }
44
+
45
+ // Parse the attribute block using a state machine
46
+ function parse_attribs(line) {
47
+ var attributes = {},
48
+ l = line.length,
49
+ i, c,
50
+ count = 1,
51
+ quote = false,
52
+ skip = false,
53
+ open, close, joiner, seperator,
54
+ pair = {
55
+ start: 1,
56
+ middle: null,
57
+ end: null
58
+ };
59
+
60
+ if (!(l > 0 && (line.charAt(0) === '{' || line.charAt(0) === '('))) {
61
+ return {
62
+ _content: line[0] === ' ' ? line.substr(1, l) : line
63
+ };
64
+ }
65
+ open = line.charAt(0);
66
+ close = (open === '{') ? '}' : ')';
67
+ joiner = (open === '{') ? ':' : '=';
68
+ seperator = (open === '{') ? ',' : ' ';
69
+
70
+ function process_pair() {
71
+ if (typeof pair.start === 'number' &&
72
+ typeof pair.middle === 'number' &&
73
+ typeof pair.end === 'number') {
74
+ var key = line.substr(pair.start, pair.middle - pair.start).trim(),
75
+ value = line.substr(pair.middle + 1, pair.end - pair.middle - 1).trim();
76
+ attributes[key] = value;
77
+ }
78
+ pair = {
79
+ start: null,
80
+ middle: null,
81
+ end: null
82
+ };
83
+ }
84
+
85
+ for (i = 1; count > 0; i += 1) {
86
+
87
+ // If we reach the end of the line, then there is a problem
88
+ if (i > l) {
89
+ throw "Malformed attribute block";
90
+ }
91
+
92
+ c = line.charAt(i);
93
+ if (skip) {
94
+ skip = false;
95
+ } else {
96
+ if (quote) {
97
+ if (c === '\\') {
98
+ skip = true;
99
+ }
100
+ if (c === quote) {
101
+ quote = false;
102
+ }
103
+ } else {
104
+ if (c === '"' || c === "'") {
105
+ quote = c;
106
+ }
107
+
108
+ if (count === 1) {
109
+ if (c === joiner) {
110
+ pair.middle = i;
111
+ }
112
+ if (c === seperator || c === close) {
113
+ pair.end = i;
114
+ process_pair();
115
+ if (c === seperator) {
116
+ pair.start = i + 1;
117
+ }
118
+ }
119
+ }
120
+
121
+ if (c === open || c === "(") {
122
+ count += 1;
123
+ }
124
+ if (c === close || (count > 1 && c === ")")) {
125
+ count -= 1;
126
+ }
127
+ }
128
+ }
129
+ }
130
+ attributes._content = line.substr(i, line.length);
131
+ return attributes;
132
+ }
133
+
134
+ // Split interpolated strings into an array of literals and code fragments.
135
+ function parse_interpol(value) {
136
+ var items = [],
137
+ pos = 0,
138
+ next = 0,
139
+ match;
140
+ while (true) {
141
+ // Match up to embedded string
142
+ next = value.substr(pos).search(embedder);
143
+ if (next < 0) {
144
+ if (pos < value.length) {
145
+ items.push(JSON.stringify(value.substr(pos)));
146
+ }
147
+ break;
148
+ }
149
+ items.push(JSON.stringify(value.substr(pos, next)));
150
+ pos += next;
151
+
152
+ // Match embedded string
153
+ match = value.substr(pos).match(embedder);
154
+ next = match[0].length;
155
+ if (next < 0) { break; }
156
+ if(match[1] === "#"){
157
+ items.push(escaperName+"("+(match[2] || match[3])+")");
158
+ }else{
159
+ //unsafe!!!
160
+ items.push(match[2] || match[3]);
161
+ }
162
+
163
+ pos += next;
164
+ }
165
+ return items.filter(function (part) { return part && part.length > 0}).join(" +\n");
166
+ }
167
+
168
+ // Used to find embedded code in interpolated strings.
169
+ embedder = /([#!])\{([^}]*)\}/;
170
+
171
+ self_close_tags = ["meta", "img", "link", "br", "hr", "input", "area", "base"];
172
+
173
+ // All matchers' regexps should capture leading whitespace in first capture
174
+ // and trailing content in last capture
175
+ matchers = [
176
+ // html tags
177
+ {
178
+ name: "html tags",
179
+ regexp: /^(\s*)((?:[.#%][a-z_\-][a-z0-9_:\-]*)+)(.*)$/i,
180
+ process: function () {
181
+ var line_beginning, tag, classes, ids, attribs, content, whitespaceSpecifier, whitespace={}, output;
182
+ line_beginning = this.matches[2];
183
+ classes = line_beginning.match(/\.([a-z_\-][a-z0-9_\-]*)/gi);
184
+ ids = line_beginning.match(/\#([a-z_\-][a-z0-9_\-]*)/gi);
185
+ tag = line_beginning.match(/\%([a-z_\-][a-z0-9_:\-]*)/gi);
186
+
187
+ // Default to <div> tag
188
+ tag = tag ? tag[0].substr(1, tag[0].length) : 'div';
189
+
190
+ attribs = this.matches[3];
191
+ if (attribs) {
192
+ attribs = parse_attribs(attribs);
193
+ if (attribs._content) {
194
+ var leader0 = attribs._content.charAt(0),
195
+ leader1 = attribs._content.charAt(1),
196
+ leaderLength = 0;
197
+
198
+ if(leader0 == "<"){
199
+ leaderLength++;
200
+ whitespace.inside = true;
201
+ if(leader1 == ">"){
202
+ leaderLength++;
203
+ whitespace.around = true;
204
+ }
205
+ }else if(leader0 == ">"){
206
+ leaderLength++;
207
+ whitespace.around = true;
208
+ if(leader1 == "<"){
209
+ leaderLength++;
210
+ whitespace.inside = true;
211
+ }
212
+ }
213
+ attribs._content = attribs._content.substr(leaderLength);
214
+ //once we've identified the tag and its attributes, the rest is content.
215
+ // this is currently trimmed for neatness.
216
+ this.contents.unshift(attribs._content.trim());
217
+ delete(attribs._content);
218
+ }
219
+ } else {
220
+ attribs = {};
221
+ }
222
+
223
+ if (classes) {
224
+ classes = classes.map(function (klass) {
225
+ return klass.substr(1, klass.length);
226
+ }).join(' ');
227
+ if (attribs['class']) {
228
+ try {
229
+ attribs['class'] = JSON.stringify(classes + " " + JSON.parse(attribs['class']));
230
+ } catch (e) {
231
+ attribs['class'] = JSON.stringify(classes + " ") + " + " + attribs['class'];
232
+ }
233
+ } else {
234
+ attribs['class'] = JSON.stringify(classes);
235
+ }
236
+ }
237
+ if (ids) {
238
+ ids = ids.map(function (id) {
239
+ return id.substr(1, id.length);
240
+ }).join(' ');
241
+ if (attribs.id) {
242
+ attribs.id = JSON.stringify(ids + " ") + attribs.id;
243
+ } else {
244
+ attribs.id = JSON.stringify(ids);
245
+ }
246
+ }
247
+
248
+ attribs = render_attribs(attribs);
249
+
250
+ content = this.render_contents();
251
+ if (content === '""') {
252
+ content = '';
253
+ }
254
+
255
+ if(whitespace.inside){
256
+ if(content.length==0){
257
+ content='" "'
258
+ }else{
259
+ try{ //remove quotes if they are there
260
+ content = '" '+JSON.parse(content)+' "';
261
+ }catch(e){
262
+ content = '" "+\n'+content+'+\n" "';
263
+ }
264
+ }
265
+ }
266
+
267
+ if (forceXML ? content.length > 0 : self_close_tags.indexOf(tag) == -1) {
268
+ output = '"<' + tag + attribs + '>"' +
269
+ (content.length > 0 ? ' + \n' + content : "") +
270
+ ' + \n"</' + tag + '>"';
271
+ } else {
272
+ output = '"<' + tag + attribs + ' />"';
273
+ }
274
+
275
+ if(whitespace.around){
276
+ //output now contains '"<b>hello</b>"'
277
+ //we need to crack it open to insert whitespace.
278
+ output = '" '+output.substr(1, output.length - 2)+' "';
279
+ }
280
+
281
+ return output;
282
+ }
283
+ },
284
+
285
+ // each loops
286
+ {
287
+ name: "each loop",
288
+ regexp: /^(\s*)(?::for|:each)\s+(?:([a-z_][a-z_\-]*),\s*)?([a-z_][a-z_\-]*)\s+in\s+(.*)(\s*)$/i,
289
+ process: function () {
290
+ var ivar = this.matches[2] || '__key__', // index
291
+ vvar = this.matches[3], // value
292
+ avar = this.matches[4], // array
293
+ rvar = '__result__'; // results
294
+
295
+ if (this.matches[5]) {
296
+ this.contents.unshift(this.matches[5]);
297
+ }
298
+ return '(function () { ' +
299
+ 'var ' + rvar + ' = [], ' + ivar + ', ' + vvar + '; ' +
300
+ 'for (' + ivar + ' in ' + avar + ') { ' +
301
+ 'if (' + avar + '.hasOwnProperty(' + ivar + ')) { ' +
302
+ vvar + ' = ' + avar + '[' + ivar + ']; ' +
303
+ rvar + '.push(\n' + (this.render_contents() || "''") + '\n); ' +
304
+ '} } return ' + rvar + '.join(""); }).call(this)';
305
+ }
306
+ },
307
+
308
+ // if statements
309
+ {
310
+ name: "if",
311
+ regexp: /^(\s*):if\s+(.*)\s*$/i,
312
+ process: function () {
313
+ var condition = this.matches[2];
314
+ return '(function () { ' +
315
+ 'if (' + condition + ') { ' +
316
+ 'return (\n' + (this.render_contents() || '') + '\n);' +
317
+ '} else { return ""; } }).call(this)';
318
+ }
319
+ },
320
+
321
+ // silent-comments
322
+ {
323
+ name: "silent-comments",
324
+ regexp: /^(\s*)-#\s*(.*)\s*$/i,
325
+ process: function () {
326
+ return '""';
327
+ }
328
+ },
329
+
330
+ //html-comments
331
+ {
332
+ name: "silent-comments",
333
+ regexp: /^(\s*)\/\s*(.*)\s*$/i,
334
+ process: function () {
335
+ this.contents.unshift(this.matches[2]);
336
+
337
+ return '"<!--'+this.contents.join('\\n')+'-->"';
338
+ }
339
+ },
340
+
341
+ // raw js
342
+ {
343
+ name: "rawjs",
344
+ regexp: /^(\s*)-\s*(.*)\s*$/i,
345
+ process: function () {
346
+ this.contents.unshift(this.matches[2]);
347
+ return '"";' + this.contents.join("\n")+"; _$output = _$output ";
348
+ }
349
+ },
350
+
351
+ // raw js
352
+ {
353
+ name: "pre",
354
+ regexp: /^(\s*):pre(\s+(.*)|$)/i,
355
+ process: function () {
356
+ this.contents.unshift(this.matches[2]);
357
+ return '"<pre>"+\n' + JSON.stringify(this.contents.join("\n"))+'+\n"</pre>"';
358
+ }
359
+ },
360
+
361
+ // declarations
362
+ {
363
+ name: "doctype",
364
+ regexp: /^()!!!(?:\s*(.*))\s*$/,
365
+ process: function () {
366
+ var line = '';
367
+ switch ((this.matches[2] || '').toLowerCase()) {
368
+ case '':
369
+ // XHTML 1.0 Transitional
370
+ line = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
371
+ break;
372
+ case 'strict':
373
+ case '1.0':
374
+ // XHTML 1.0 Strict
375
+ line = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
376
+ break;
377
+ case 'frameset':
378
+ // XHTML 1.0 Frameset
379
+ line = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';
380
+ break;
381
+ case '5':
382
+ // XHTML 5
383
+ line = '<!DOCTYPE html>';
384
+ break;
385
+ case '1.1':
386
+ // XHTML 1.1
387
+ line = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
388
+ break;
389
+ case 'basic':
390
+ // XHTML Basic 1.1
391
+ line = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">';
392
+ break;
393
+ case 'mobile':
394
+ // XHTML Mobile 1.2
395
+ line = '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">';
396
+ break;
397
+ case 'xml':
398
+ // XML
399
+ line = "<?xml version='1.0' encoding='utf-8' ?>";
400
+ break;
401
+ case 'xml iso-8859-1':
402
+ // XML iso-8859-1
403
+ line = "<?xml version='1.0' encoding='iso-8859-1' ?>";
404
+ break;
405
+ }
406
+ return JSON.stringify(line + "\n");
407
+ }
408
+ },
409
+
410
+ // Embedded markdown. Needs to be added to exports externally.
411
+ {
412
+ name: "markdown",
413
+ regexp: /^(\s*):markdown\s*$/i,
414
+ process: function () {
415
+ return parse_interpol(exports.Markdown.encode(this.contents.join("\n")));
416
+ }
417
+ },
418
+
419
+ // script blocks
420
+ {
421
+ name: "script",
422
+ regexp: /^(\s*):(?:java)?script\s*$/,
423
+ process: function () {
424
+ return parse_interpol('\n<script type="text/javascript">\n' +
425
+ '//<![CDATA[\n' +
426
+ this.contents.join("\n") +
427
+ "\n//]]>\n</script>\n");
428
+ }
429
+ },
430
+
431
+ // css blocks
432
+ {
433
+ name: "css",
434
+ regexp: /^(\s*):css\s*$/,
435
+ process: function () {
436
+ return JSON.stringify('<style type="text/css">\n' +
437
+ this.contents.join("\n") +
438
+ "\n</style>");
439
+ }
440
+ }
441
+
442
+ ];
443
+
444
+ function compile(lines) {
445
+ var block = false,
446
+ output = [];
447
+
448
+ // If lines is a string, turn it into an array
449
+ if (typeof lines === 'string') {
450
+ lines = lines.trim().replace(/\n\r|\r/g, '\n').split('\n');
451
+ }
452
+
453
+ lines.forEach(function(line) {
454
+ var match, found = false;
455
+
456
+ // Collect all text as raw until outdent
457
+ if (block) {
458
+ match = block.check_indent.exec(line);
459
+ if (match) {
460
+ block.contents.push(match[1] || "");
461
+ return;
462
+ } else {
463
+ output.push(block.process());
464
+ block = false;
465
+ }
466
+ }
467
+
468
+ matchers.forEach(function (matcher) {
469
+ if (!found) {
470
+ match = matcher.regexp.exec(line);
471
+ if (match) {
472
+ block = {
473
+ contents: [],
474
+ indent_level: (match[1]),
475
+ matches: match,
476
+ check_indent: new RegExp("^(?:\\s*|" + match[1] + " (.*))$"),
477
+ process: matcher.process,
478
+ render_contents: function () {
479
+ return compile(this.contents);
480
+ }
481
+ };
482
+ found = true;
483
+ }
484
+ }
485
+ });
486
+
487
+ // Match plain text
488
+ if (!found) {
489
+ output.push(function () {
490
+ // Escaped plain text
491
+ if (line[0] === '\\') {
492
+ return parse_interpol(line.substr(1, line.length));
493
+ }
494
+
495
+
496
+ function escapedLine(){
497
+ try {
498
+ return escaperName+'('+JSON.stringify(JSON.parse(line)) +')';
499
+ } catch (e2) {
500
+ return escaperName+'(' + line + ')';
501
+ }
502
+ }
503
+
504
+ function unescapedLine(){
505
+ try {
506
+ return parse_interpol(JSON.parse(line));
507
+ } catch (e) {
508
+ return line;
509
+ }
510
+ }
511
+
512
+ // always escaped
513
+ if((line.substr(0, 2) === "&=")) {
514
+ line = line.substr(2, line.length).trim();
515
+ return escapedLine();
516
+ }
517
+
518
+ //never escaped
519
+ if((line.substr(0, 2) === "!=")) {
520
+ line = line.substr(2, line.length).trim();
521
+ return unescapedLine();
522
+ }
523
+
524
+ // sometimes escaped
525
+ if ( (line[0] === '=')) {
526
+ line = line.substr(1, line.length).trim();
527
+ if(escapeHtmlByDefault){
528
+ return escapedLine();
529
+ }else{
530
+ return unescapedLine();
531
+ }
532
+ }
533
+
534
+ // Plain text
535
+ return parse_interpol(line);
536
+ }());
537
+ }
538
+
539
+ });
540
+ if (block) {
541
+ output.push(block.process());
542
+ }
543
+
544
+ var txt = output.filter(function (part) { return part && part.length > 0}).join(" +\n");
545
+ if(txt.length == 0){
546
+ txt = '""';
547
+ }
548
+ return txt;
549
+ };
550
+
551
+ function optimize(js) {
552
+ var new_js = [], buffer = [], part, end;
553
+
554
+ function flush() {
555
+ if (buffer.length > 0) {
556
+ new_js.push(JSON.stringify(buffer.join("")) + end);
557
+ buffer = [];
558
+ }
559
+ }
560
+ js.replace(/\n\r|\r/g, '\n').split('\n').forEach(function (line) {
561
+ part = line.match(/^(\".*\")(\s*\+\s*)?$/);
562
+ if (!part) {
563
+ flush();
564
+ new_js.push(line);
565
+ return;
566
+ }
567
+ end = part[2] || "";
568
+ part = part[1];
569
+ try {
570
+ buffer.push(JSON.parse(part));
571
+ } catch (e) {
572
+ flush();
573
+ new_js.push(line);
574
+ }
575
+ });
576
+ flush();
577
+ return new_js.join("\n");
578
+ };
579
+
580
+ function render(text, options) {
581
+ options = options || {};
582
+ text = text || "";
583
+ var js = compile(text, options);
584
+ if (options.optimize) {
585
+ js = Haml.optimize(js);
586
+ }
587
+ return execute(js, options.context || Haml, options.locals);
588
+ };
589
+
590
+ function execute(js, self, locals) {
591
+ return (function () {
592
+ with(locals || {}) {
593
+ try {
594
+ var _$output;
595
+ eval("_$output =" + js );
596
+ return _$output; //set in eval
597
+ } catch (e) {
598
+ return "\n<pre class='error'>" + html_escape(e.stack) + "</pre>\n";
599
+ }
600
+
601
+ }
602
+ }).call(self);
603
+ };
604
+
605
+ Haml = function Haml(haml, config) {
606
+ if(typeof(config) != "object"){
607
+ forceXML = config;
608
+ config = {};
609
+ }
610
+
611
+ var escaper;
612
+ if(config.customEscape){
613
+ escaper = "";
614
+ escaperName = config.customEscape;
615
+ }else{
616
+ escaper = html_escape.toString() + "\n";
617
+ escaperName = "html_escape";
618
+ }
619
+
620
+ escapeHtmlByDefault = (config.escapeHtmlByDefault || config.escapeHTML || config.escape_html);
621
+
622
+ var js = optimize(compile(haml));
623
+
624
+ var str = "with(locals || {}) {\n" +
625
+ " try {\n" +
626
+ " var _$output=" + js + ";\n return _$output;" +
627
+ " } catch (e) {\n" +
628
+ " return \"\\n<pre class='error'>\" + "+escaperName+"(e.stack) + \"</pre>\\n\";\n" +
629
+ " }\n" +
630
+ "}"
631
+
632
+ try{
633
+ var f = new Function("locals", escaper + str );
634
+ return f;
635
+ }catch(e){
636
+ console.error(str);
637
+ throw e;
638
+ }
639
+ }
640
+
641
+ Haml.compile = compile;
642
+ Haml.optimize = optimize;
643
+ Haml.render = render;
644
+ Haml.execute = execute;
645
+ Haml.html_escape = html_escape;
646
+ }());
647
+
648
+ // Hook into module system
649
+ if (typeof module !== 'undefined') {
650
+ module.exports = Haml;
651
+ }
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: herman-haml-sprockets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - vagmi
9
+ - Herman Moreno
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-09-25 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: tilt
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: sprockets
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '2.0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: execjs
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.0
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 1.3.0
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ description: Use the JST processor and have haml code read in and appended to application.js
80
+ email:
81
+ - vagmi.mudumbai@gmail.com
82
+ - herman.moreno@crowdint.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - LICENSE
90
+ - README.md
91
+ - Rakefile
92
+ - haml-sprockets.gemspec
93
+ - lib/haml-sprockets.rb
94
+ - lib/haml-sprockets/engine.rb
95
+ - lib/haml-sprockets/version.rb
96
+ - spec/lib/haml-sprockets_spec.rb
97
+ - spec/spec_helper.rb
98
+ - vendor/assets/javascripts/haml.js
99
+ homepage: https://github.com/supherman/haml-sprockets
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project: herman-haml-sprockets
119
+ rubygems_version: 1.8.23
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Use the awesome haml-js javascript templating lib in Ruby
123
+ test_files:
124
+ - spec/lib/haml-sprockets_spec.rb
125
+ - spec/spec_helper.rb