-
Notifications
You must be signed in to change notification settings - Fork 42
/
compiler.js
202 lines (169 loc) · 5.92 KB
/
compiler.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
const fs = require('fs-extra');
const path = require('path');
const Mustache = require('mustache');
const sass = require('node-sass');
const babel = require('babel-core');
const minify = require('html-minifier').minify;
const { execSync } = require('child_process');
const Package = require('./package');
const Markdown = require('./markdown');
const log = require('./log');
class Templater {
constructor(compiler) {
this.templates = {};
this.compiler = compiler;
this.readTemplates();
}
readTemplates() {
log.log('Loading templates.');
fs.readdirSync(this.compiler.srcPath('templates')).forEach((filename) => {
const name = path.basename(filename, '.mustache');
this.templates[name] = fs.readFileSync(this.compiler.srcPath('templates', filename), 'utf8');
Mustache.parse(this.templates[name]);
});
}
writeFile(opath, html) {
fs.writeFile(opath, minify(html, { collapseWhitespace: true, sortAttributes: true }));
}
template(template, outputPath, current, view,
{ string = false, noLayout = false, isDocumentation = true } = {}) {
view.isDocumentation = isDocumentation;
const tempstring = string ? template : this.get(template);
const content = Mustache.render(tempstring, view, this.templates);
if (noLayout) {
this.writeFile(outputPath, content);
return;
}
const html = Mustache.render(this.templates.main, {
content,
title: view.title,
isDocumentation,
activeClass() {
return text => (current === text ? 'class="active"' : '');
},
}, this.templates);
this.writeFile(outputPath, html);
}
get(name) {
return this.templates[name];
}
}
class Compiler {
constructor(out, src) {
this.out = out;
this.src = src;
this.templater = new Templater(this);
this.branchName = process.env.BRANCH || execSync('git symbolic-ref --short HEAD', { encoding: 'utf8' }).trim();
}
outPath(...components) {
components.unshift(this.out);
return path.join(...components);
}
srcPath(...components) {
components.unshift(this.src);
return path.join(...components);
}
template(...args) {
this.templater.template(...args);
}
createStatic(scssFileNames, es6FileNames, copy) {
log.log('Copying static folders.');
fs.ensureDirSync(this.outPath('static', 'js'));
fs.ensureDirSync(this.outPath('static', 'css'));
copy.forEach((name) => {
fs.copySync(this.srcPath('static', name), this.outPath('static', name));
});
scssFileNames.forEach((name) => {
sass.render({
file: this.srcPath('static', 'css', `${name}.scss`),
outputStyle: 'compressed',
}, (err, result) => {
if (err) {
log.error(`Sass compilation errored: ${err}`);
}
else {
fs.writeFile(this.outPath('static', 'css', `${name}.css`), result.css);
}
});
});
es6FileNames.forEach((name) => {
babel.transformFile(this.srcPath('static', 'js', `${name}.es6`), {
presets: ['es2015'],
minified: true,
},
(err, { code }) => fs.writeFile(this.outPath('static', 'js', `${name}.js`), code));
});
}
createPages() {
const pages = require(this.srcPath('pages', 'pages.json'));
for (const page of pages) {
log.log(`Templating page ${page.source}.`);
page.string = true;
const data = fs.readFileSync(this.srcPath('pages', page.source), 'utf8');
fs.ensureDirSync(this.outPath(...page.destination.slice(0, -1)));
this.template(data, this.outPath(...page.destination),
page, {}, page);
}
}
createBook(name, bookTitle) {
log.log(`Creating ${name}.`);
fs.ensureDirSync(this.outPath('docs', name));
const rulesParser = new Markdown.RulesParser();
const chapters = require(this.srcPath(name, 'chapters.json')).map((file) => {
const markdown = fs.readFileSync(this.srcPath(name, file)).toString();
const content = Markdown.parseContent(markdown);
const fileName = path.basename(file, '.md');
rulesParser.parse(markdown, fileName);
return {
name: fileName,
sourcePath: path.join('src', name, file),
markdown,
title: content.title,
sections: content.sections,
};
});
for (const [i, chapter] of chapters.entries()) {
chapter.current = true;
const out = this.outPath('docs', name, `${chapter.name}.html`);
this.template('chapter', out, name, {
bookTitle,
chapters,
title: chapter.title,
content: Markdown.toHtml(chapter.markdown, rulesParser, chapter.name),
sections: chapter.sections,
sourcePath: chapter.sourcePath,
branchName: this.branchName,
prev: i > 0 ? `${chapters[i - 1].name}.html` : null,
next: i + 1 < chapters.length ? { title: chapters[i + 1].title, href: `${chapters[i + 1].name}.html` } : null,
thisHeadingID() {
return this.toLowerCase().replace(/[^\w]+/g, '-');
},
});
chapter.current = false;
}
rulesParser.warn('document-statement');
const indexPath = this.outPath('docs', name, 'index.html');
if (fs.existsSync(indexPath)) {
fs.unlinkSync(indexPath);
}
fs.copy(this.outPath('docs', name, `${chapters[0].name}.html`), indexPath);
}
createPackages() {
fs.ensureDirSync(this.outPath('docs', 'packages'));
const packagesJson = require(this.srcPath('packages', 'packages.json'));
const packages = packagesJson.default.map((name) => {
const sentence = new Package(this, name, false).build();
return { name, sentence };
});
const externalPackages = packagesJson.external.map((name) => {
const sentence = new Package(this, name, true).build();
return { name, sentence };
});
this.template('packages', this.outPath('docs', 'packages', 'index.html'), 'packages', {
packages,
externalPackages,
title: 'Packages',
});
}
}
module.exports = Compiler;