-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffeebars.js
118 lines (109 loc) · 3.67 KB
/
coffeebars.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
#!/usr/bin/env node
// Generated by CoffeeScript 1.3.3
(function() {
var args, global, pimatch, render, templates;
global = (function() {
return this;
})();
pimatch = /\{\{\{?([\w\W]*?)\}\}\}?/g;
templates = {};
exports.render = render = function(context, source) {
var template, _ref;
template = (_ref = templates[source]) != null ? _ref : templates[source] = exports.toFunction(source);
return template(context);
};
exports.toFunction = function(source) {
return eval(exports.toJavaScript(source));
};
exports.toJavaScript = function(source) {
var script;
script = require('coffee-script').compile(exports.toCoffeeScript(source));
return script.replace(/;\s*$/, '');
};
exports.toCoffeeScript = function(source) {
var array, dent, escape, fixIndent, getIndent, indent, lastIndex, match, pi, push, template, writeText;
array = [];
indent = 1;
getIndent = function() {
var i, value, _i;
value = "";
for (i = _i = 0; 0 <= indent ? _i < indent : _i > indent; i = 0 <= indent ? ++_i : --_i) {
value += '\t';
}
return value;
};
push = function(text, dent) {
if (dent == null) {
dent = 0;
}
array.push(getIndent());
array.push(text, '\n');
return indent += dent;
};
lastIndex = 0;
writeText = function(match) {
var text, _ref;
text = source.substring(lastIndex, (_ref = match != null ? match.index : void 0) != null ? _ref : source.length);
lastIndex = pimatch.lastIndex;
return push("write " + (JSON.stringify(text)));
};
fixIndent = function(pi, match) {
var currentIndent, index, line, lineIndent, lines, smallest, _i, _len, _ref;
smallest = Number.MAX_VALUE;
lines = pi.split(/\r\n|\r|\n/);
for (index = _i = 0, _len = lines.length; _i < _len; index = ++_i) {
line = lines[index];
if (!(line.trim().length > 0)) {
continue;
}
if (index === 0) {
throw new Error("Multiline script cannot start on same line as opening brace:\n" + match[0]);
}
lineIndent = (_ref = line.match(/^\s*/)) != null ? _ref[0].length : void 0;
if (lineIndent != null) {
smallest = Math.min(smallest, lineIndent);
}
}
currentIndent = getIndent();
return lines.map(function(x) {
return currentIndent + x.substring(smallest);
}).join('\n');
};
while (match = pimatch.exec(source)) {
writeText(match);
pi = match[1];
dent = 0;
if (/\r|\n/.test(pi)) {
pi = fixIndent(pi, match);
} else if (/^\s*(else|catch)\b\s*([\w\W]+)/.test(pi)) {
indent--;
dent = 1;
pi = pi.trim();
} else if (/^\s*(if|for|while|unless|try)\b\s*([\w\W]+)/.test(pi) && !/\b(then|end)\b/.test(pi)) {
dent = 1;
pi = pi.trim();
} else if (/^\s*end\s*$/.test(pi)) {
indent--;
pi = null;
} else {
escape = match[0].substring(0, 3) !== '{{{';
pi = "write (" + pi + "), " + escape;
}
if (pi != null) {
push(pi, dent);
}
}
writeText(match);
source = array.join('');
template = "template = (write) ->\n" + source + "\n return\n\nreturn (context, write) ->\n buffer = null\n if not write\n buffer = []\n write = (text) -> buffer.push text if text?\n template.call context, write\n buffer?.join ''";
return template;
};
if (require.main === module) {
args = process.argv.slice(2);
if (args.length === 0) {
console.log("Usage: coffeebars template");
} else {
console.log(exports.parse(require('fs').readFileSync(args[0], 'utf8')));
}
}
}).call(this);