forked from aaronblohowiak/haml-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaml.js
executable file
·399 lines (360 loc) · 10.1 KB
/
haml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* ex:ts=2:et: */
/*jslint bitwise: true, browser: true, eqeqeq: true, evil: true, immed: true, newcap: true,
nomen: true, plusplus: true, regexp: true, undef: true, white: true, indent: 2 */
/*globals */
var Haml = {};
// Bind to the exports object if it exists. (CommonJS and NodeJS)
if (exports) {
Haml = exports;
}
Haml.to_html = function (json) {
if (typeof json === 'string') {
if (json.substr(0, 3) === '!!!') {
switch (json.substr(4).toLowerCase()) {
case 'xml':
return "<?xml version='1.0' encoding='utf-8' ?>\n";
case '':
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n';
case '1.1':
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n';
case 'strict':
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n';
}
}
return json;
}
if (typeof json === 'object' && json.length !== undefined) {
if (typeof json[0] === 'object')
{
if (json[0].tag !== undefined) {
return (function () {
var tag, element, html, attributes;
element = json.shift();
tag = element.tag;
html = tag;
for (var key in element) {
if (element.hasOwnProperty(key) && key !== 'tag') {
html += " " + key + "=\"" + element[key].
replace("&", "&").
replace("<", "<").
replace(">", ">").
replace("\"", """) + "\"";
}
}
if (json.length === 0 && (tag === "link" || tag === 'br' || tag === 'input')) {
return "\n<" + html + " />\n";
}
return "\n<" + html + ">" + Haml.to_html(json) + "</" + tag + ">\n";
}());
}
if (json[0].plugin !== undefined) {
switch (json.shift().plugin) {
case "javascript":
return '\n<script type="text/javascript">\n//<![CDATA[\n ' + json[0].split("\n").join("\n ") + "\n//]]>\n</script>\n";
case "css":
return '\n<style type="text/css">\n ' + json[0].split("\n").join("\n ") + "\n</style>\n";
}
}
}
return json.map(Haml.to_html).join('');
}
return JSON.stringify(json);
}
Haml.parse = function (text, locals) {
var empty_regex = new RegExp("^[ \t]*$"),
indent_regex = new RegExp("^ *"),
element_regex = new RegExp("^(?::[a-z]+|(?:[%][a-z][a-z0-9]*)?(?:[#.][a-z0-9_-]+)*)", "i"),
scope = this,
haml, element, stack, indent, buffer, old_indent, mode, last_insert;
// Sortof an instance_eval for Javascript
function instance_eval(input) {
var block;
block = function () { with(scope, locals || {}) { return eval("(" + input + ")"); } };
return block.call(scope);
}
function process_embedded_code(string) {
if (typeof string !== 'string') {
return string;
}
var matches = string.match(/#{([^}]*)}/g);
if (matches) {
matches.forEach(function (match) {
var pair = match.match(/#{([^}]*)}/);
string = string.replace(pair[0], instance_eval(pair[1]));
})
}
return string;
}
function flush_buffer() {
if (buffer.length > 0) {
mode = "NORMAL";
element.push(process_embedded_code(buffer.join("\n")));
buffer = [];
}
}
function parse_push() {
stack.push({element: element, mode: mode});
var new_element = [];
mode = "NORMAL";
element.push(new_element);
element = new_element;
}
function parse_pop() {
var last = stack.pop();
if (element.length === 1) {
if (typeof element[0] === "string") {
if (element[0].match(element_regex)[0].length === 0) {
// Collapse arrays with single string literal
last.element[last.element.length - 1] = element[0];
}
}
}
element = last.element;
mode = last.mode;
}
function get_indent(line) {
if (line === undefined) {
return 0;
}
var i = line.match(indent_regex);
return i[0].length / 2;
}
function parse_attribs(line) {
// Parse the attribute block using a state machine
if (!(line.length > 0 && line.charAt(0) === '{')) {
return line;
}
var l = line.length;
var count = 1;
var quote = false;
var skip = false;
for (var i = 1; count > 0; i += 1) {
// If we reach the end of the line, then there is a problem
if (i > l) {
throw "Malformed attribute block";
}
var c = line.charAt(i);
if (skip) {
skip = false;
} else {
if (quote) {
if (c === '\\') {
skip = true;
}
if (c === quote) {
quote = false;
}
} else {
if (c === '"' || c === "'") {
quote = c;
}
if (c === '{') {
count += 1;
}
if (c === '}') {
count -= 1;
}
}
}
}
var block = line.substr(0, i);
(function () {
element.push(instance_eval(block))
}.call(this));
return line.substr(i);
}
function parse_content(line) {
// Strip off leading whitespace
line = line.replace(indent_regex, '');
// Ignore blank lines
if (line.length === 0) {
return;
}
if (mode === 'ELEMENT') {
parse_pop();
}
switch (line.charAt(0)) {
case '/':
break;
case '=':
(function () {
buffer.push(instance_eval(line.substr(1)));
}.call(this));
break;
case "\\":
buffer.push(line.substr(1));
break;
default:
buffer.push(line);
break;
}
}
function parse_element(line, selector) {
flush_buffer();
if (element.length > 0) {
if (mode === 'ELEMENT') {
parse_pop();
}
parse_push();
}
mode = 'ELEMENT';
var classes = selector.match(/\.[^\.#]+/g),
ids = selector.match(/#[^\.#]+/g),
tag = selector.match(/^%([^\.#]+)/g),
plugin = selector.match(/^:([^\.#]+)/g);
tag = tag ? tag[0].substr(1) : (plugin ? null : 'div');
plugin = plugin ? plugin[0].substr(1) : null;
line = parse_attribs.call(this, line.substr(selector.length));
var attrs;
if (typeof element[element.length - 1] === "object") {
attrs = element[element.length - 1];
} else {
attrs = {};
element.push(attrs);
}
if (tag) {
attrs.tag = tag;
}
if (plugin) {
attrs.plugin = plugin;
}
if (ids) {
for (var i = 0, l = ids.length; i < l; i += 1) {
ids[i] = ids[i].substr(1);
}
if (attrs.id) {
ids.push(attrs.id);
}
attrs.id = ids.join(" ");
}
if (classes) {
for (var i = 0, l = classes.length; i < l; i += 1) {
classes[i] = classes[i].substr(1);
}
if (attrs['class']) {
classes.push(attrs['class']);
}
attrs['class'] = classes.join(" ");
}
if (selector.charAt(0) === ':') {
mode = 'RAW';
} else {
if (!line.match(empty_regex)) {
parse_push();
parse_content.call(this, line, true);
flush_buffer();
parse_pop();
}
}
}
function process_plugins() {
var contents, i;
switch (element[0].plugin) {
case 'if':
var condition = element[0].condition;
contents = element[1];
for (i in element) {
if (element.hasOwnProperty(i)) {
delete element[i];
}
}
if (condition) {
var new_element = Haml.parse.call(this, contents);
for (i in new_element) {
if (new_element.hasOwnProperty(i)) {
element[i] = new_element[i];
}
}
element.length = new_element.length;
}
break;
case 'foreach':
var array, key, value, key_name, value_name;
array = element[0].array;
key_name = element[0].key;
value_name = element[0].value;
contents = element[1];
for (i in element) {
if (element.hasOwnProperty(i)) {
delete element[i];
}
}
element.length = 0;
for (key in array) {
if (array.hasOwnProperty(key)) {
value = array[key];
this[key_name] = key;
this[value_name] = value;
element.push(Haml.parse.call(this, contents));
}
}
break;
}
}
haml = [];
element = haml;
stack = [];
buffer = [];
mode = 'NORMAL';
parse_push(); // Prime the pump so we can have multiple root elements
indent = 0;
old_indent = indent;
var lines = text.split("\n"),
line, line_index, line_count;
line_count = lines.length;
for (line_index = 0; line_index <= line_count; line_index += 1) {
line = lines[line_index];
switch (mode) {
case 'ELEMENT':
case 'NORMAL':
// Do indentation
indent = get_indent(line);
if (indent === old_indent + 1) {
parse_push();
old_indent = indent;
}
while (indent < old_indent) {
flush_buffer();
parse_pop();
old_indent -= 1;
}
if (line === undefined) {
continue;
}
line = line.substr(indent * 2);
// Pass doctype commands through as is
if (line.substr(0, 3) === '!!!') {
element.push(line);
continue;
}
// Check for special element characters
var match = line.match(element_regex);
if (match && match[0].length > 0) {
parse_element.call(this, line, match[0]);
} else {
parse_content.call(this, line);
}
break;
case 'RAW':
if (get_indent(line) <= indent) {
flush_buffer();
process_plugins.call(this);
mode = "ELEMENT";
line_index -= 1;
continue;
}
line = line.substr((indent + 1) * 2);
buffer.push(line);
break;
}
}
if (haml.length === 1 && typeof haml[0] !== 'string') {
haml = haml[0];
}
return haml;
};
Haml.render = function(text, options) {
options = options || {};
var json = Haml.parse.call(options.context || GLOBAL, text, options.locals);
return Haml.to_html(json).replace('\n\n', '\n');
}