-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
94 lines (82 loc) · 2.92 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
var markdown = require('marked');
var fs = require('fs');
var jade = require('jade');
var _ = require('lodash');
var pangunode = require('pangunode');
function main(options) {
loadLanguages(options.languagesDir)
.sort(function(a, b) {
return a.id < b.id ? 1 : -1;
})
.forEach(function(lang,_,languages) {
outputLanguage(lang, languages, options);
});
}
function loadLanguages(path) {
var index = readJSON(path + '/languages.json');
var availableListedLanguages = _.intersection(index.listed, fs.readdirSync(path));
return availableListedLanguages.reduce(function(all, lang) {
var langPath = path + '/' + lang;
if (!fs.statSync(langPath).isDirectory()) return all;
var data = _.assign(loadLanguage(langPath), {id: lang});
return all.concat(data);
}, []);
}
function loadLanguage(path) {
var index = readJSON(path + '/index.json');
var language = _.assign(index, {
sections: index.sections.map(function(section) {
return _.assign(section, {
isIntro: section.link === 'Intro',
article: loadArticle(path + '/Shell13Q-' + section.link + '.md')
});
})
});
language.navigation = language.sections;
return language;
}
function loadArticle(path) {
var text = fs.readFileSync(path, "utf-8");
text = pangunode(text);
var title = text.slice(0, text.indexOf('\n'));
var content = processContent(text.slice(title.length));
title = '<span class="number">' + title.slice(0, title.indexOf('.') + 1) + '</span><span class="title">' + title.slice(title.indexOf('.') + 1) + '</span>';
title = markdown(title.replace(/\#/g, '').trim());
title = title.slice(3, title.length - 5);
return {
title: title,
content: content
};
}
function processContent(text) {
return markdown(text).replace(/'/, ''');
}
function readJSON(path) {
return JSON.parse(fs.readFileSync(path, 'utf-8'));
}
function outputLanguage(lang, languages, opts) {
var langBase = (lang.id === opts.baseLanguage
? '/'
: '/' + lang.id + '/');
fs.mkdir(opts.outDir + langBase, function(err) {
if(err && err.code != "EEXIST") {
console.error("Couldn't make dir " + opts.outDir + ": " + err);
process.exit(1);
}
var out = opts.outDir + langBase + 'index.html';
var perLangOpts = _.assign({out: out, languages: languages}, opts);
writeTemplate(perLangOpts, lang);
});
}
function writeTemplate(options, language) {
var templateData = _.assign({language: language.id}, language, options);
var jadeFile = fs.readFileSync(options.template);
var jadeTemplate = jade.compile(jadeFile, {complieDebug: false, debug: false});
var html = jadeTemplate(templateData);
fs.writeFileSync(options.out, html);
}
exports.build = function (options) {
options = _.defaults(options || {}, { languagesDir: 'doc', baseLanguage: 'zhtw', template: 'Shell13Q.jade', pathPrefix: 'Shell13Q/', outDir: 'site' });
return main(options);
};
exports.build();