ruby-haml-js 0.0.1

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.
Files changed (52) hide show
  1. data/.gitignore +8 -0
  2. data/Gemfile +6 -0
  3. data/Gemfile.lock +120 -0
  4. data/HISTORY.md +2 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +39 -0
  7. data/Rakefile +37 -0
  8. data/lib/ruby-haml-js.rb +2 -0
  9. data/lib/ruby-haml-js/engine.rb +13 -0
  10. data/lib/ruby-haml-js/template.rb +61 -0
  11. data/lib/ruby-haml-js/version.rb +5 -0
  12. data/lib/tasks/pakunok_tasks.rake +4 -0
  13. data/ruby-haml-js.gemspec +25 -0
  14. data/spec/dummy/Rakefile +7 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +10 -0
  16. data/spec/dummy/app/assets/javascripts/sample.jst.hamljs +3 -0
  17. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  18. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  19. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  20. data/spec/dummy/app/mailers/.gitkeep +0 -0
  21. data/spec/dummy/app/models/.gitkeep +0 -0
  22. data/spec/dummy/app/views/layouts/application.html.erb +16 -0
  23. data/spec/dummy/app/views/pages/index.html.erb +29 -0
  24. data/spec/dummy/config.ru +4 -0
  25. data/spec/dummy/config/application.rb +46 -0
  26. data/spec/dummy/config/boot.rb +10 -0
  27. data/spec/dummy/config/database.yml +25 -0
  28. data/spec/dummy/config/environment.rb +5 -0
  29. data/spec/dummy/config/environments/development.rb +27 -0
  30. data/spec/dummy/config/environments/production.rb +51 -0
  31. data/spec/dummy/config/environments/test.rb +39 -0
  32. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  33. data/spec/dummy/config/initializers/inflections.rb +10 -0
  34. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  35. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  36. data/spec/dummy/config/initializers/session_store.rb +8 -0
  37. data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
  38. data/spec/dummy/config/locales/en.yml +5 -0
  39. data/spec/dummy/config/routes.rb +60 -0
  40. data/spec/dummy/lib/assets/.gitkeep +0 -0
  41. data/spec/dummy/log/.gitkeep +0 -0
  42. data/spec/dummy/public/404.html +26 -0
  43. data/spec/dummy/public/422.html +26 -0
  44. data/spec/dummy/public/500.html +26 -0
  45. data/spec/dummy/public/favicon.ico +0 -0
  46. data/spec/dummy/script/rails +6 -0
  47. data/spec/spec_helper.rb +12 -0
  48. data/spec/support/global.rb +9 -0
  49. data/spec/support/matchers.rb +18 -0
  50. data/spec/template_spec.rb +43 -0
  51. data/vendor/assets/javascripts/haml.js +651 -0
  52. metadata +185 -0
@@ -0,0 +1,9 @@
1
+ require 'ruby-haml-js'
2
+
3
+ def assets
4
+ Rails.application.assets
5
+ end
6
+
7
+ def asset_for(name)
8
+ assets[name]
9
+ end
@@ -0,0 +1,18 @@
1
+
2
+ RSpec::Matchers.define :serve do |asset_name|
3
+ match do |sprockets|
4
+ !!sprockets[asset_name]
5
+ end
6
+
7
+ failure_message_for_should do |sprockets|
8
+ "expected #{asset_name} to be served, but it wasn't"
9
+ end
10
+
11
+ failure_message_for_should_not do |sprockets|
12
+ "expected #{asset_name} NOT to be served, but it was"
13
+ end
14
+
15
+ description do
16
+ "serve #{asset_name}"
17
+ end
18
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe RubyHamlJs::Template do
4
+ def template haml, file
5
+ RubyHamlJs::Template.new(file) { haml }
6
+ end
7
+
8
+ def render haml, file = "file.jst.js.hamljs"
9
+ template(haml, file).render
10
+ end
11
+
12
+ it 'should have default mime type' do
13
+ RubyHamlJs::Template.default_mime_type.should == 'application/javascript'
14
+ end
15
+
16
+ describe 'rendering' do
17
+ subject { render "#main= 'quotes'\n #inner= name", 'myTemplate.js.hamljs' }
18
+
19
+ it { should include "function (locals) {" }
20
+ it { should include 'function html_escape' }
21
+ end
22
+
23
+ describe 'executing' do
24
+ let(:name) { 'Dima' }
25
+ subject do
26
+ func = render "#main= 'quotes'\n #inner= name", 'myTemplate.js.hamljs'
27
+ ExecJS.compile("var f = #{func};").eval "f({name: '#{name}'})"
28
+ end
29
+
30
+ it { should include 'Dima' }
31
+
32
+ context "injecting <script> tag as parameter" do
33
+ let(:name) { '<script>' }
34
+ it { should include '&lt;script&gt;' }
35
+ it { should_not include '<script>' }
36
+ end
37
+ end
38
+
39
+ describe 'serving' do
40
+ subject { assets }
41
+ it { should serve 'sample.js' }
42
+ end
43
+ end
@@ -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
+ }