-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (181 loc) · 5.74 KB
/
index.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
var fs = require('co-fs-extra');
var path = require('path');
var cheerio = require('cheerio');
var compose = require('koa-compose');
var debug = require('debug')('template');
//debug = console.log;
// supports div and script tag, too with optional value
function dataTag(name, value) {
if (value) name += '="' + value + '"';
return 'div[data-template-' + name + '], script[data-template-' + name + ']';
}
module.exports = function (settings) {
var genRenders = {};
var isoRenders = {};
settings = settings || {};
settings.root = settings.root || __dirname;
settings.ext = settings.ext || '.html';
settings.server = settings.server || require('koa-static');
var middlewares = [];
middlewares.push(middleware);
middlewares.push(settings.server(settings.root));
return compose(middlewares);
function *genRender(viewfile) {
var name = viewfile.substr(0, viewfile.length - settings.ext.length);
var js = name + '.js';
var gen = js.substr(settings.root.length);
var r = genRenders[js];
if (!r) {
if (yield fs.exists(js)) {
r = require(js);
if (r.render) {
debug('registering gen render', js);
}
else {
r = {};
}
}
else {
r = {};
}
genRenders[js] = r;
}
if (r && r.render) {
yield r.render.call(this);
return;
}
}
function *isoRender(viewfile, $, data) {
var js = viewfile + '.js';
var iso = js.substr(settings.root.length);
var r = isoRenders[js];
if (!r) {
if (yield fs.exists(js)) {
debug('registering iso render', js);
r = require(js);
} else {
// not to check again and again file exist
// so registering an empty stub
r = function () { return false; };
}
isoRenders[js] = r;
}
if (r($, data)) {
var script = '<script src="/' + iso + '"></script>';
this.bundles = this.bundles || [];
this.bundles.push(script);
}
}
function *render(view, opts) {
var $;
opts = opts || {};
opts.blocks = opts.blocks || {};
// theoretically unlimited parent view
while (view) {
var viewfile = path.join(settings.root, view + settings.ext);
debug('rendering', viewfile);
var text = yield fs.readFile(viewfile, 'utf8');
this.$ = $ = cheerio.load(text, opts);
yield genRender.call(this, viewfile);
yield isoRender.call(this, viewfile, $, this.state);
// first: replace all include-placeholders with given contents
var includes = [];
$(dataTag('include')).each(function () {
var name = $(this).data().templateInclude;
includes.push(name);
});
for (var i=0; i<includes.length; i++) {
var name = includes[i];
var viewfile = path.join(settings.root, name + settings.ext);
debug('including', viewfile);
var text = yield fs.readFile(viewfile, 'utf8');
$(dataTag('include', name)).replaceWith(text);
yield genRender.call(this, viewfile);
yield isoRender.call(this, viewfile, $, this.state);
}
// second: replace all block-placeholders with opts.blocks
for (var name in opts.blocks) {
var block = opts.blocks[name];
var processed = false;
var $div = $(dataTag('placeholder', name));
if ($div.length) {
debug('replacing', name);
$div.replaceWith(block);
processed = true;
} else {
// if there is no named placeholder try to insert into end of a block with same name
$div = $(dataTag('block', name));
if ($div.length) {
debug('appending', name, 'to block');
$div.append(block);
processed = true;
} else {
// no placeholder, no block so try to insert into end of default place
switch (name) {
case 'head':
$div = $('head');
if ($div.length) {
debug('appending', name, 'to head');
$div.append(block);
processed = true;
}
break;
case 'body':
case 'code':
$div = $('body');
if ($div.length) {
debug('appending', name, 'to body');
$div.append(block);
processed = true;
}
break;
default:
// cannot find the proper place, maybe needed on the higher level
break;
}
}
}
if (processed) {
delete opts.blocks[name];
} else {
debug('moving up', name);
}
}
// third: remove all not replaced placeholders
$(dataTag('placeholder')).each(function () {
var name = $(this).data().templatePlaceholder;
debug('removing placeholder', name);
$(this).remove();
});
// fourth: collect constructed blocks
$(dataTag('block')).each(function () {
var name = $(this).data().templateBlock;
debug('storing', name);
opts.blocks[name] = $(this).html();
});
// fifth: check parent template
var parent = $(dataTag('extend')).first().data();
view = null;
if (parent && parent.templateExtend) {
view = parent.templateExtend;
debug('extending', view);
}
}
if (this.bundles && this.bundles.length) {
var $head = $('script[src]').last();
for (var i=0; i<this.bundles.length; i++) {
$head.after('\n' + this.bundles[i] + '\n');
};
}
this.$ = $;
return $;
}
function *middleware(next) {
this.render = render;
yield next;
if (this.$) {
this.body = this.$.html();
this.type = 'html'; // imply utf-8
}
}
};