forked from SimonDegraeve/meteor-jade-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
package.js
191 lines (166 loc) · 5.19 KB
/
package.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
Package.describe({
summary: "Write your templates using Handlebars and Jade instead of HTML and Handlebars"
});
Npm.depends({
'jade': '0.29.0',
'StringScanner': '0.0.3'
});
var fs = Npm.require('fs');
var path = Npm.require('path');
// find jade-handlebars package dir
var packageDir = null;
(function () {
var packageDirs = process.env.PACKAGE_DIRS.split(':');
for (var i = 0; i < packageDirs.length; i++) {
var dir = path.join(packageDirs[i], 'jade-handlebars');
var stat = fs.statSync(dir);
if (stat && stat.isDirectory()) {
packageDir = dir;
break;
}
}
}());
Package.on_use(function (api) {
api.use('templating', 'client');
});
Package.register_extension(
"jade", function(bundle, source_path, serve_path, where) {
var jade = Npm.require('jade');
var StringScanner = Npm.require("StringScanner");
var html_scanner = Npm.require(path.join(packageDir, 'html_scanner'));
// Variables
var lines = [];
var json = [];
var handlebarsPattern = /\s*(\{\{.*(?!\{\{)\}\})/;
// Handlebars hack
// Read the file content and create JSON
try{
// Create the string scanner with the .jade file content
var ss = new StringScanner(fs.readFileSync(source_path, "utf8"));
ss.reset();
// Parse the file content until the end
while(!ss.endOfString()){
// Scan content per line
ss.scan(/^(\ *)(.*)\n+/);
// Get the indentation of the line
indent = ss.captures()[0].length;
// Get the content of the line
value = ss.captures()[1];
// Variables for json
var child = [];
var tags = []
// Scan the content of the line to find handlebars tags
ss_line = new StringScanner(value);
ss_line.reset();
while(ss_line.checkUntil(handlebarsPattern)){
ss_line.scanUntil(handlebarsPattern);
tags.push({"position": ss_line.pointer()-ss_line.captures()[0].length, "value": ss_line.captures()[0]});
}
// End scan
ss_line.terminate();
// Create the JSON for the line
line = {"indent": indent, "content": value, "tags": tags, "child": child};
// Find arborescence
// If the line is root
if(line.indent == 0){
// Add to the main JSON
json.push(line);
}else{
// Add the child to the parent
for(var i=lines.length-1; i >= 0; i--){
if(lines[i].indent < line.indent){
lines[i].child.push(line);
break;
}
}
}
lines.push(line);
}
// End scan
ss.terminate();
} catch(err) {
return bundle.error(err.message);
}
// Fix indentation
json = jsonParser(json);
// JSON to string
global.contents_tmp = ""; // used in jsonToContents() function
contents = jsonToContents(json);
// Jade parser
var jade_options = { pretty: true };
jade.render(contents, jade_options, function(err, html){
if (err) throw err;
contents = html;
});
// From meteor/templating package
if (where !== "client")
return;
var results = html_scanner.scan(contents.toString('utf8'), source_path);
if (results.head)
bundle.add_resource({
type: "head",
data: results.head,
where: where
});
if (results.body)
bundle.add_resource({
type: "body",
data: results.body,
where: where
});
if (results.js) {
var path_part = path.dirname(serve_path);
if (path_part === '.')
path_part = '';
if (path_part.length && path_part !== path.sep)
path_part = path_part + path.sep;
var ext = path.extname(source_path);
var basename = path.basename(serve_path, ext);
serve_path = path_part + "template." + basename + ".js";
bundle.add_resource({
type: "js",
path: serve_path,
data: new Buffer(results.js),
source_file: source_path,
where: where
});
}
}
);
function jsonParser(json) {
// Number fo indentation
n = 2;
// Start the loop
json.forEach(function(root){
root.child.forEach(function(child){
// If line doesn't have HB tag
if(root.tags.length < 1){
child.indent = root.indent+n;
}
// If line has HB tag and start with HB tag and not comment
else if(root.tags.length > 0 && root.tags[0].position == 0 && !root.content.match(/^\/\/\-*.*/)) {
child.indent = root.indent;
}
else if(root.tags.length > 0 && root.tags[0].position != 0 && !root.content.match(/^\/\/\-*.*/)) {
child.indent = root.indent+n;
}
// If child has child, recursive call
if(child.child.length > 0)
jsonParser([child]);
});
});
return json;
}
function jsonToContents(json) {
for(line in json){
for(attr in json[line]){
if(attr == "content"){
global.contents_tmp += Array(json[line].indent+1).join(" ") + json[line][attr] + "\n";
}
else if(attr == "child" && typeof(json[line][attr]) == "object") {
jsonToContents(json[line][attr]);
}
}
}
return global.contents_tmp;
}