haml-sprockets 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.
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/haml-sprockets.gemspec +25 -0
- data/lib/haml-sprockets.rb +21 -0
- data/lib/haml-sprockets/engine.rb +9 -0
- data/lib/haml-sprockets/version.rb +5 -0
- data/vendor/assets/javascripts/haml.js +651 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Using HAML-JS with Sprockets and Rails 3.1
|
2
|
+
|
3
|
+
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.
|
4
|
+
|
5
|
+
The gem includes [haml-js]. You would not have to download it separately. To use this gem, you need to do the following.
|
6
|
+
|
7
|
+
In the `Gemfile`, add the following line.
|
8
|
+
|
9
|
+
gem "haml-sprockets"
|
10
|
+
|
11
|
+
In `app/assets/javascripts/application.js` add the following line before `//= require_tree .`
|
12
|
+
|
13
|
+
//=require haml
|
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
|
+
|
27
|
+
[HAML]: http://haml-lang.com/
|
28
|
+
[haml-js]: https://github.com/creationix/haml-js
|
29
|
+
[Dharana Software Innovations]: http://www.dharanasoft.com/
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
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 = "haml-sprockets"
|
7
|
+
s.version = Haml::Sprockets::VERSION
|
8
|
+
s.authors = ["vagmi"]
|
9
|
+
s.email = ["vagmi.mudumbai@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/dharanasoft/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 = "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.0"
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "haml-sprockets/version"
|
2
|
+
require 'tilt'
|
3
|
+
module Haml
|
4
|
+
module Sprockets
|
5
|
+
class Template < ::Tilt::Template
|
6
|
+
def self.engine_initialized?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
def initialize_engine
|
10
|
+
end
|
11
|
+
def prepare
|
12
|
+
end
|
13
|
+
def evaluate(scope,locals,&block)
|
14
|
+
haml_code = data.dup
|
15
|
+
haml_code = haml_code.gsub(/\\/,"\\\\").gsub(/\'/,"\\'").gsub(/\n/,"\\n")
|
16
|
+
"Haml('#{haml_code}')"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
require "haml-sprockets/engine" if defined?(Rails)
|
@@ -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, "&").
|
10
|
+
replace(/</g, "<").
|
11
|
+
replace(/>/g, ">").
|
12
|
+
replace(/\"/g, """);
|
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,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: haml-sprockets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- vagmi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-09 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: tilt
|
16
|
+
requirement: &2152621180 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2152621180
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sprockets
|
27
|
+
requirement: &2152620680 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.0.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2152620680
|
36
|
+
description: Use the JST processor and have haml code read in and appended to application.js
|
37
|
+
email:
|
38
|
+
- vagmi.mudumbai@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- haml-sprockets.gemspec
|
48
|
+
- lib/haml-sprockets.rb
|
49
|
+
- lib/haml-sprockets/engine.rb
|
50
|
+
- lib/haml-sprockets/version.rb
|
51
|
+
- vendor/assets/javascripts/haml.js
|
52
|
+
homepage: https://github.com/dharanasoft/haml-sprockets
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project: haml-sprockets
|
72
|
+
rubygems_version: 1.8.6
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Use the awesome haml-js javascript templating lib in Ruby
|
76
|
+
test_files: []
|