-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjadeCompiler.js
More file actions
70 lines (61 loc) · 2.68 KB
/
jadeCompiler.js
File metadata and controls
70 lines (61 loc) · 2.68 KB
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
/* Sagemec : Système de gestion des membres et des contacts
* Copyright (C) 2013 CODE3 Coopérative de solidarité
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var jade = require('jade');
module.exports = function (ns) {
return {
match: /\.jade$/,
compileSync: function (sourcePath, source) {
/*
* Given:
* ns === 'App'
* sourcePath === '~/my_project/assets/js/section/views/template/my-template.jade'
*
* Produce:
* (function(){
* var _ref0;this.App = (_ref0 = this.App) != null ? _ref0 : {};
* var _ref1;this.App.Section = (_ref1 = this.App.Section) != null ? _ref1 : {};
* var _ref2;this.App.Section.Views = (_ref2 = this.App.Section.Views) != null ? _ref2 : {};
* var _ref3;this.App.Section.Views.Templates = (_ref3 = this.App.Section.Views.Templates) != null ? _ref3 : {};
* this.App.Section.Views.Templates.my_template = function anonymous () { ...compiled jade... };
* }).apply(this);
*/
var code = [], obj = "this", refCount = 0;
// wrap code in function
code.push("(function(){");
// Initialize classpath steps.
sourcePath.replace(/^.+\/js/, "/"+ns).replace(/([^.\/]+)\//g, function ($0, $1) {
var ref = "_ref"+refCount++;
var step = $1.slice(0,1).toUpperCase() + $1.slice(1);
step = step.replace(/-/g, '_');
obj += "."+step;
code.push("var "+ref+";"+obj+" = ("+ref+" = "+obj+") != null ? "+ref+" : {};");
});
// Extract object property name from file path.
sourcePath.replace(/[_A-Z][-_$A-Z0-9]*(?=\.jade$)/i, function ($0) {
// replace dashs with underscores.
var name = $0.replace(/-/g, '_');
// Actually compile the jade template.
var templateFn = jade.compile(source, {client:true, compileDebug:false, filename:sourcePath});
// Assign template to property.
code.push(obj+"."+name+" = "+templateFn+";");
});
// Close and call wrapper function;
code.push("}).apply(this);");
return code.join('');
}
};
};