-
Notifications
You must be signed in to change notification settings - Fork 0
/
markup.js
387 lines (323 loc) · 10.9 KB
/
markup.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*!
* Axml
* Copyright 2014-2015 Fabien Bavent
* Released under the BSD license
*/
(function () {
var previous_mod, root = this
if (root != null)
previous_mod = root.markup
var DEBUG = true
// Dependancies ===========================================================
var fs = root.fs
if( typeof fs === 'undefined' ) {
if( typeof require !== 'undefined' ) {
fs = require('fs')
}
else throw new Error('markup requires fs');
}
var async = root.async
if( typeof async === 'undefined' ) {
if( typeof require !== 'undefined' ) {
async = require('async')
}
else throw new Error('markup requires async');
}
var markupCtx = root.markupCtx
if( typeof markupCtx === 'undefined' ) {
if( typeof require !== 'undefined' ) {
markupCtx = require('./markupCtx')
}
else throw new Error('markup requires markupCtx');
}
// Module markup =========================================================
var markup = function (options) {
if (!(this instanceof markup))
return new markup(options);
this.super_ = stream.Transform
stream.Transform.call (this)
this._init (options)
}
// ======================================================================
// axml.prototype = Object.create (stream.Transform.prototype, {})
markup.commands = {
extends:function (mark, ctx) {
if (markup.debug) console.log (' ' + mark.literal)
if (mark.value.length != 1) {
console.error ('The command \'extends\' take one argument');
return null;
}
ctx.setParent (ctx.eval (mark.value[0]))
return null;
},
set:function (mark, ctx, callback) {
if (markup.debug) console.log (' ' + mark.literal)
if (mark.value.length != 2) {
callback ('The command \'set\' take two arguments');
return null;
}
ctx.varSet(mark.value[0], ctx.eval (mark.value[1]))
callback();
},
get:function (mark, ctx, callback) {
if (markup.debug) console.log (' ' + mark.literal)
if (mark.value.length != 1) {
callback ('The command \'get\' take one argument');
return null;
}
mark.data = ctx.eval (mark.value[0])
callback()
},
get_if:function (mark, ctx, callback) {
if (markup.debug) console.log (' ' + mark.literal)
if (mark.value.length != 2 && mark.value.length != 3) {
callback ('The command \'get_if\' take two or three arguments');
return null;
}
var cdt = ctx.eval (mark.value[0]);
var v = (cdt != '' && cdt != false) ? 1 : 2
if (v == 2 && mark.value.length == 2)
mark.data = ''
else
mark.data = ctx.eval (mark.value[v])
callback()
},
doLayout:function (mark, ctx, callback) {
if (markup.debug) console.log (' ' + mark.literal)
if (mark.value.length != 1) {
callback ('The command \'doLayout\' take one argument');
return null;
} else {
mark.data = ctx.childData
callback();
}
},
include:function (mark, ctx, callback) {
if (markup.debug) console.log (' ' + mark.literal)
if (mark.value.length != 1) {
callback ('The command \'include\' take one argument');
return null;
} else {
markup.renderFile (ctx.eval(mark.value[0]), ctx, function (err, data) {
mark.data = data
callback();
})
}
}
}
// ----------------------------------------------------------------------
// axml.prototype._transform = function(chunk, encoding, callback) {
markup.directory = './views'
// ----------------------------------------------------------------------
markup.renderer = function (dir) {
return function (req, res, next) {
var ctx = new markupCtx(req)
ctx.directory = dir + '/'
markup.renderFile (dir + req.path, ctx, function (err, data) {
if (err) {
if (next) next()
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
}
})
}
}
// ----------------------------------------------------------------------
markup.renderFile = function (path, options, callback) {
if (!path.startswith (options.directory))
path = options.directory + path;
if (options == null) {
options = new markupCtx({});
} else if (typeof options === 'function') {
callback = options
options = new markupCtx({});
} else if (!options.getParent)
options = new markupCtx(options)
if (markup.debug) console.log ('renderFile', path)
fs.readFile (path, function (err, data) {
if (err && !path.endswith ('/500.html')) {
if (err.code == 'EISDIR')
markup.renderFile (path + '/index.html', options, callback)
else {
if (markup.debug) console.error (path, err)
markup.renderFile ('/500.html', options, callback)
}
} else if (err) {
callback (err, null)
} else {
// Note: sending a buffer will put the file to download, send a string!
markup.parseFile (data, options, callback)
}
})
// callback (null, '<h1>Yep !?</h1>\n')
}
// ----------------------------------------------------------------------
markup.finish = function (array, options, callback) {
data = ''
for (var i=0; i < array.length; ++i) {
if (typeof array[i] === 'string')
data += array[i]
else if (array[i].data != null)
data += array[i].data
}
if (options.getParent() == null) {
callback (null, data)
} else {
options.childData = data
var url = options.getParent()
options.setParent(null)
markup.renderFile (url, options, callback)
}
}
// ----------------------------------------------------------------------
markup.doCommand = function (cmd, options, callback) {
// if (cmd == 'extends')
markup.commands[cmd.type] (cmd, options, callback)
// cmd.data = cmd.type
// callback ()
}
// ----------------------------------------------------------------------
markup.parseFile = function (data, options, callback) {
// console.log (data.toString())
var array = []
var parent = null;
data = data.toString()
var commands = []
// Split data and commands
for (;;) {
var l = data.match(/#\{.*\/\}/)
if (l == null) break
if (l.index > 0)
array.push (data.substring(0, l.index))
data = data.substring (l.index + l[0].length)
var cmd = markup.parseCommand(l[0])
array.push (cmd)
if (cmd.type != 'extends')
commands.push (cmd)
else
parent = cmd
}
array.push (data)
if (markup.debug) console.log ('File made of '+array.length+' blocks using '+commands.length+' commands')
if (markup.debug && parent) console.log ('File have parent: '+ parent.value[0])
// Regroup page
if (commands.length == 0) {
if (parent)
markup.commands.extends (parent, options)
markup.finish (array, options, callback)
} else {
async.each (commands, function (cmd, callback) {
if (markup.commands[cmd.type] == null)
callback('No command named ' + cmd.type)
else
markup.commands[cmd.type] (cmd, options, callback)
}, function (err) {
if (err) callback (err, null)
else {
if (parent)
markup.commands.extends (parent, options)
markup.finish (array, options, callback)
}
})
}
}
// ----------------------------------------------------------------------
markup.parseCommand = function (mark) {
var tag = mark.substring(2);
var c = '';
while (tag[0] <= ' ')
tag = tag.substring(1);
while (tag[0] > ' ') {
c += tag.substring(0, 1);
tag = tag.substring(1);
}
if (c == '/}') {
if (markup.debug) console.warn('Empty tag')
}
var command = c;
var args = [];
c = '';
for (;;) {
while (tag[0] <= ' ')
tag = tag.substring(1);
while (tag[0] != ':' && (tag[0] != '/' || tag[1] != '}')) {
c += tag.substring(0, 1);
tag = tag.substring(1);
}
if (c == '/}' || c == '}') {
if (markup.debug) console.log('Cmd: '+ args);
return;
}
c = c.trim();
args.push(c);
c = '';
if (tag[0] == '/' && tag[1] == '}')
return {type:command, value:args, literal:mark };
tag = tag.substring(1);
}
}
// ----------------------------------------------------------------------
markup.lookFor = function(req, res) {
// if (req.url == '/')
// res.render ('index');
// else {
// Note: As express doesn't allow to recover we try to check
// everything first, but it's a bad design !
fs.exists (markup.directory + req.url, function (exist) {
if (req.url.endswith('/')) {
res.render (req.url.substring(1) + 'index');
} else if (exist) {
var name = req.url.substring(1, req.url.length - 5) // Seriously !? (REMOVE .html)
res.render (name);
} else {
if (markup.debug) console.log ('Could not find the file', markup.directory + req.url)
res.render ('404'); // File not found!
}
})
// }
}
// ----------------------------------------------------------------------
markup.lookAt = function(directory) {
return function (req, res) {
if (markup.debug) console.log ('LOOK AT', directory, req.url)
if (req.url == '/')
res.render ('index');
else {
// Note: As express doesn't allow to recover we try to check
// everything first, but it's a bad design !
fs.exists (directory + req.url, function (exist) {
var name = req.url.substring(1, req.url.length - 5) // Seriously !? (REMOVE .html)
if (exist) {
res.render (name);
} else {
if (markup.debug) console.log ('Could not find the file', directory + req.url)
res.render ('404'); // File not found!
}
})
}
}
}
// Export the module ======================================================
markup.noConflict = function () {
root.markup = previous_mod
return markup
}
if (typeof module !== 'undefined' && module.exports) // Node.js
module.exports = markup
else if (typeof exports !== 'undefined')
exports = module.exports = markup
else if (typeof define !== 'undefined' && define.amd) // AMD / RequireJS
define([], function () { return markup })
else // Browser
root.markup = markup
if (require && require.main === module) {
// console.log (process.argv[2])
markup.renderFile(process.argv[2], null, function (err, data) {
console.log ('DATA:')
console.log (data)
})
}
}).call(this)
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------