-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.js
More file actions
240 lines (194 loc) · 5.55 KB
/
web.js
File metadata and controls
240 lines (194 loc) · 5.55 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
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
var fs = require('fs'),
marked = require('marked'),
jsdom = require("jsdom"),
$ = require('jquery'),
less = require('less');
var src = __dirname + '/sources';
var pub = __dirname + '/public';
var struct = {
styles : [],
templates : [],
pages : []
};
var fileType = {
'html' : 'templates',
'less' : 'styles',
'mdwn' : 'pages',
};
var counter = 0;
port = process.env.PORT || 5000;
function Join(cb, arg){
this.counter = 0;
this.newJob = function(){
this.counter += 1;
};
this.callback = function(latestArg){
this.counter -= 1;
if (this.counter === 0)
cb(latestArg ? latestArg : arg);
}
return this;
}
function getFileExt(file){
var fileName = file.split('.');
var result = {};
if (fileName.length > 1){
result.base = fileName.splice(0, fileName.length - 1).join('.');
result.ext = fileName[0];
} else {
result.ext = undefined;
result.base = file;
}
return result;
};
function scrap(path){
join.newJob();
fs.readdir(src + path, function(err, files){
if (err) throw err;
files.forEach(function(file){
join.newJob();
fs.stat(src + path + '/' + file, function(err, stat) {
if (!stat)
console.log('Error : ' + stat);
if (stat.isDirectory())
scrap(path + '/' + file);
if (stat.isFile()) {
fileName = getFileExt(file);
if (fileType[fileName.ext]) {
struct[fileType[fileName.ext]][fileName.base] = path + '/' + file;
}
}
join.callback();
});
});
join.callback();
});
};
function changeFileExtension(path, extension) {
path = path.split('.');
path[path.length - 1] = extension;
path = path.join('.');
return path;
};
function createPath(root, path) {
path.split('/').forEach(function(pathPart){
if(pathPart.length === 0) return;
if (fs.existsSync(root)) {
// serve file
} else {
fs.mkdirSync(root, 0750);
}
root += '/' + pathPart;
});
};
function writeFile(fileName, data) {
fs.writeFile(fileName, data, function (err) {
if (err) throw err;
console.log('>>> ' + fileName + ' generated');
});
};
function extractMetaData(data) {
// Extract Meta data
var match;
var meta = [];
// TODO REPAIR THIS
// regexp won't stop until =
while(match = data.match(/^\s*([\#.a-z0-9_-]+)\s*:?\s*(.*)\s*\n/i)) {
data = data.substr(match[0].length);
meta[match[1]] = match[2] === '' ? true : match[2];
console.log(match[1]);
}
meta['content'] = data;
return meta;
};
function bind(meta, callback) {
var doc = jsdom.jsdom(meta.template? meta.template : '').createWindow().document,
$doc = $(doc);
var join = new Join(function(doc){
var content = $(doc).find('content');
// In a very single
content.after(content.html());
content.remove();
callback(doc.innerHTML);
});
join.newJob();
for( var key in meta ) {
if (key === 'header'){
var headers = meta[key].split(', ');
for (var header in headers) (function(header){
join.newJob();
buildPage(header,function(meta){
bind(meta, function(html){
var header = $doc.find('header');
if (header.length) header.append(html);
else $doc.find('content').before(html);
join.callback(doc);
});
});
})(headers[header]);
} else if (key === 'footer'){
var footers = meta[key].split(', ');
for (var footer in footers) (function(footer){
join.newJob();
buildPage(footer,function(meta){
bind(meta, function(html){
var footer = $doc.find('footer');
if (footer.length) footer.append(html);
else $doc.find('content').after(html);
join.callback(doc);
});
});
})(footers[footer]);
} else if (key === 'assets') {
// TODO assets
} else if (key === 'template' ) {
// just do nothing
} else if (key === 'collection') {
// TODO collection
} else if (key === 'content') {
$doc.find('content').html(meta.content);
} else {
$doc.find(key).html(meta[key]);
}
}
join.callback(doc);
};
function buildPage(page, callback){
fs.readFile(src + struct.pages[page], 'utf8', function (err, data) {
if (err) throw err;
var meta = extractMetaData(data);
// Markdown
meta.content = marked(meta.content);
// Template
if (meta['template'] && struct.templates[meta['template']]) {
meta.template = fs.readFileSync(src + struct.templates[meta['template']], 'utf8');
} else {
meta.template = '<content />';
}
callback(meta);
});
};
function make(pub) {
console.log('--- MAKE ---');
for (var style in struct.styles) (function (style) {
fs.readFile(src + struct.styles[style], 'utf8', function (err, data) {
less.render(data, function(err, css){
if (err) throw err;
createPath(pub, struct.styles[style])
writeFile(pub + changeFileExtension(struct.styles[style], 'css'), css);
});
});
})(style);
for (var page in struct.pages) (function (page) {
buildPage(page, function(meta){
if(!meta.included)
bind(meta, function(html){
createPath(pub, struct.pages[page]);
writeFile(pub + changeFileExtension(struct.pages[page], 'html'), '<!DOCTYPE html>' + html);
});
});
})(page);
};
// TODO refactor join
var join = new Join(make, pub);
scrap('');