pakunok 0.0.1 → 0.0.2
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 +1 -0
- data/Gemfile +2 -11
- data/Gemfile.lock +16 -2
- data/HISTORY.md +7 -0
- data/MIT-LICENSE +1 -1
- data/README.md +82 -66
- data/assets.md +51 -0
- data/lib/pakunok.rb +8 -0
- data/lib/pakunok/haml_js_template.rb +83 -0
- data/lib/pakunok/version.rb +1 -1
- data/pakunok.gemspec +1 -0
- data/spec/{colorpicker_spec.rb → assets/colorpicker_spec.rb} +0 -0
- data/spec/{fileuploader_spec.rb → assets/fileuploader_spec.rb} +0 -0
- data/spec/{jquery-ui_spec.rb → assets/jquery-ui_spec.rb} +0 -0
- data/spec/{jquery_spec.rb → assets/jquery_spec.rb} +0 -0
- data/spec/{misc_spec.rb → assets/misc_spec.rb} +2 -0
- data/spec/dummy/app/assets/javascripts/application.js +3 -0
- data/spec/dummy/app/assets/javascripts/haml/haml-foo-bar.js.hamljs +2 -0
- data/spec/dummy/app/assets/javascripts/haml/not-haml.js.erb +1 -0
- data/spec/dummy/app/assets/javascripts/pages.js.coffee +8 -0
- data/spec/dummy/app/assets/javascripts/sample.js.hamljs +3 -0
- data/spec/dummy/app/views/pages/index.html.erb +11 -1
- data/spec/fixtures/name.js.haml-js +0 -0
- data/spec/haml_js_template_spec.rb +102 -0
- data/vendor/assets/javascripts/pakunok/haml.js +651 -0
- metadata +38 -14
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
alert('<%= "Hello" %>')
|
@@ -1,12 +1,22 @@
|
|
1
1
|
<h1>Samples</h1>
|
2
2
|
|
3
|
+
<h2>HAML template</h2>
|
4
|
+
<button id='haml-update'>Update template</button>
|
5
|
+
<div id='haml-placeholder'> </div>
|
6
|
+
|
7
|
+
<p>
|
8
|
+
The compiled sample HAML template is <a href='<%= asset_path 'sample.js' %>'>here</a>.
|
9
|
+
</p>
|
10
|
+
|
11
|
+
<h2>Some of the assets</h2>
|
3
12
|
<%
|
4
13
|
[
|
5
14
|
'colorpicker.js', 'colorpicker.css',
|
6
15
|
'jquery.js', 'jquery-ui.js' 'jquery-ui/pack/dialog.js',
|
7
16
|
'fileuploader.js', 'fileuploader.css', 'fileuploader/loading.gif',
|
8
17
|
'innershiv.js',
|
9
|
-
'jquery.form.js'
|
18
|
+
'jquery.form.js',
|
19
|
+
'haml/haml-foo-bar.js'
|
10
20
|
].each do |asset|
|
11
21
|
|
12
22
|
%>
|
File without changes
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tilt'
|
3
|
+
require 'pakunok/haml_js_template'
|
4
|
+
|
5
|
+
describe 'HAML-JS processor' do
|
6
|
+
def template haml, file = "file.js.hamljs"
|
7
|
+
Pakunok::HamlJsTemplate.new(file) { haml }
|
8
|
+
end
|
9
|
+
|
10
|
+
def render haml, file = "file.js.hamljs"
|
11
|
+
template(haml, file).render
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should have default mime type' do
|
15
|
+
Pakunok::HamlJsTemplate.default_mime_type.should == 'application/javascript'
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'rendering' do
|
19
|
+
subject { render "#main= 'quotes'\n #inner= name", 'myTemplate.js.hamljs' }
|
20
|
+
|
21
|
+
it { should include "function (locals) {" }
|
22
|
+
|
23
|
+
it { should include 'function html_escape' }
|
24
|
+
|
25
|
+
it 'should make template available for JavaScript' do
|
26
|
+
context = ExecJS.compile(subject)
|
27
|
+
html = context.eval("JST.myTemplate({name: 'dima'})")
|
28
|
+
html.should include '<div id="main">'
|
29
|
+
html.should include 'dima'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should be safe by default' do
|
33
|
+
context = ExecJS.compile(subject)
|
34
|
+
html = context.eval("JST.myTemplate({name: '<script>'})")
|
35
|
+
html.should_not include '<script>'
|
36
|
+
html.should include '<script>'
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with custom escape' do
|
40
|
+
before { Pakunok::HamlJsTemplate.custom_escape = 'best_escaper_ever' }
|
41
|
+
after { Pakunok::HamlJsTemplate.custom_escape = nil }
|
42
|
+
it { should include 'best_escaper_ever(' }
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'with custom root variable' do
|
46
|
+
before { Pakunok::HamlJsTemplate.root_variable = 'TheRootVariable' }
|
47
|
+
after { Pakunok::HamlJsTemplate.root_variable = nil }
|
48
|
+
it { should include 'TheRootVariable' }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'template naming for' do
|
53
|
+
{
|
54
|
+
'file' => 'file',
|
55
|
+
'file.js.erb.hamljs' => 'file',
|
56
|
+
'file-with-dash.hamljs' => 'fileWithDash',
|
57
|
+
'file_with_underscore' => 'fileWithUnderscore',
|
58
|
+
'dir/foo_bar' => 'dir_fooBar',
|
59
|
+
'win\\dir\\foo_bar' => 'win_dir_fooBar',
|
60
|
+
'd1/d2/foo_bar.js.a.b.c.d' => 'd1_d2_fooBar'
|
61
|
+
}.each_pair do |file, name|
|
62
|
+
it "#{file} should be #{name}" do
|
63
|
+
template('#main', file).client_name.should == name
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'custom prefix' do
|
68
|
+
after { Pakunok::HamlJsTemplate.name_prefix = nil }
|
69
|
+
{
|
70
|
+
nil => ['javascripts/file.js', 'file'],
|
71
|
+
nil => ['javascripts/comments/file.js', 'comments_file'],
|
72
|
+
'js/templates' => ['js/templates/x-file.js', 'xFile'],
|
73
|
+
'javascripts/backbone/templates/' => ['javascripts/backbone/templates/file.js', 'file'],
|
74
|
+
'/backbone/templates/' => ['javascripts/backbone/templates/file.js', 'file'],
|
75
|
+
'backbone/templates/' => ['javascripts/backbone/templates/file.js', 'file']
|
76
|
+
}.each_pair do |prefix, file2name|
|
77
|
+
file, name = file2name
|
78
|
+
it "#{prefix} should produce #{name} from #{file}" do
|
79
|
+
Pakunok::HamlJsTemplate.name_prefix = prefix
|
80
|
+
template('#main', file).client_name.should == name
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'with BackboneRails' do
|
85
|
+
before do
|
86
|
+
module BackboneRails
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should automatically apply rails-backbone prefix' do
|
91
|
+
template('#main', 'whatever/backbone/templates/my/foo-bar.js').client_name.should == 'my_fooBar'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe 'serving' do
|
98
|
+
subject { assets }
|
99
|
+
it { should serve 'haml/haml-foo-bar.js' }
|
100
|
+
it { should serve 'haml/not-haml.js' }
|
101
|
+
end
|
102
|
+
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, "&").
|
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
|
+
}
|