forked from BonsaiDen/JavaScript-Garden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
206 lines (173 loc) · 6.12 KB
/
build.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
var fs = require('fs'),
path = require('path');
jade = require('jade'),
md = require('node-markdown'),
Class = require('neko').Class;
var format = new require('fomatto').Formatter();
// Garden Generator -------------------------------------------------------------
// ------------------------------------------------------------------------------
var Garden = Class(function(options) {
var languages = fs.readdirSync(options.dir);
this.languages = {};
this.options = options;
this.options.language = this.json([this.options.dir, 'language.json'].join('/'));
var that = this;
languages.forEach(function(lang) {
if (fs.statSync(that.options.dir + '/' + lang).isDirectory()) {
that.log('Parsing language "{}"...', lang);
that.lang = {
id: lang,
navigation: [],
index: []
};
if (that.loadIndex()) {
that.languages[lang] = that.lang;
that.log(' Done.')
} else {
that.log(' Error: Could not find "index.json"!')
}
}
});
delete this.lang;
this.log('');
this.generateAll();
}, {
log: function() {
console.log(format.apply(null, arguments));
},
loadIndex: function() {
var that = this;
this.lang.index = this.json([this.options.dir,
this.lang.id, 'index.json'].join('/'));
if (this.lang.index === null) {
return false;
}
that.lang.title = that.lang.index.langTitle;
this.lang.navigation = [];
this.lang.index.sections.forEach(function(section, i) {
that.loadSection(section);
that.lang.navigation.push({
title: section.title,
link: section.dir,
articles: section.articles,
parsed: section.parsed
});
});
return true;
},
loadSection: function(section) {
var files = fs.readdirSync(this.folder(section.dir));
section.parsed = {};
section.link = section.dir;
var that = this;
section.articles = section.articles || [];
section.articles.concat('index').forEach(function(article, e) {
if (files.indexOf(article + '.md') !== -1) {
var parsed = that.parseArticle(that.md(section.dir, article));
section.parsed[article] = parsed;
if (section.articles.indexOf(article) !== -1) {
section.articles[e] = {
id: article,
link: section.link + '.' + article,
title: parsed.title,
parsed: parsed
};
}
}
});
},
parseArticle: function(text) {
var title = text.substring(0, text.indexOf('\n'));
text = text.substring(title.length);
title = md.Markdown(title.replace(/\#/g, '').trim());
text = this.toMarkdown(text);
var parts = text.split('<h3>');
var subs = [];
for(var i = 0, l = parts.length; i < l; i++) {
var sub = parts[i];
subs.push((i > 0 ? '<h3>' : '') + sub);
}
return {
title: title.substring(3, title.length - 4),
text: text,
subs: subs
};
},
toMarkdown: function(text) {
text = md.Markdown(text).replace(/'/g,''');
text = text.replace(/<blockquote>/g, '<aside>').
replace(/<\/blockquote>/g, '</aside>');
return text.replace(/<aside>\s+<p><strong>ES5/g,
'<aside class="es5"><p><strong>ES5');
},
json: function(file) {
try {
return JSON.parse(fs.readFileSync(file).toString());
} catch (err) {
return null;
}
},
md: function(section, article) {
var file = [this.folder(section), article].join('/') + '.md';
return fs.readFileSync(file).toString();
},
folder: function(section) {
return [this.options.dir, this.lang.id, section].join('/');
},
render: function(language, template, out) {
var lang = this.languages[language];
if (lang) {
this.log('Rendering "{}" to "{}"...', language, out);
var languages = [];
for(var i in this.languages) {
if (this.languages.hasOwnProperty(i)) {
if (this.options.language.listed.indexOf(i) !== -1) {
languages.push(this.languages[i]);
}
}
}
var options = {
pathPrefix: this.options.pathPrefix,
baseLanguage: this.options.language.default,
language: language,
languages: languages,
title: lang.index.title,
description: lang.index.description,
navigation: lang.navigation,
sections: lang.index.sections,
top: lang.navigation[0]
};
jade.renderFile(template, {locals: options}, function(err, html){
if (err) throw err;
fs.writeFileSync(out, html);
});
this.log(' Done.');
}
},
generateAll: function() {
for(var i in this.languages) {
if (this.languages.hasOwnProperty(i)) {
this.generate(i);
}
}
},
generate: function(lang) {
var that = this;
var dir = [this.options.out];
if (lang !== this.options.language.default) {
dir.push(lang);
}
dir = dir.join('/');
path.exists(dir, function(exists) {
if (!exists) {
fs.mkdirSync(dir, '777');
}
that.render(lang, that.options.template, dir + '/index.html');
});
}
});
exports.build = function (options) {
options = options || {dir: 'doc', pathPrefix: 'JavaScript-Garden/', template: 'garden.jade', out: 'site'};
new Garden(options);
}
exports.build();