forked from cocos/cocos-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
182 lines (161 loc) · 5.84 KB
/
gulpfile.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
'use strict';
const Fs = require('fs');
const Path = require('path');
const Del = require('del');
const Globby = require('globby');
// const spawn = require('child_process').spawn;
// const gulpSequence = require('gulp-sequence');
const Difference = require('lodash/difference');
const { exec } = require('child_process');
const gulp = require('gulp');
const program = require('commander');
program
.option('-o, --only <items>', 'Only build these files, specrated by ","', x => x.split(','))
.option('--dest <path>', 'Copy generated document to specified path.')
.parse(process.argv);
gulp.task('publish', function (done) {
var dest = program.dest;
if (!dest) {
return done('dest not supplied');
}
var del = Path.join(dest, '*');
console.log('deleting ' + del);
Del.sync(del, { force: true });
console.log('copying _book/**/* to ' + dest);
gulp.src('_book/**/*', {
base: '_book'
})
.pipe(gulp.dest(dest))
.on('end', done);
});
const FORBID_IGNORE_ARRAY = ['index.md', 'SUMMARY.md'];
const allPagesPattern = ['zh/**/*.md', 'en/**/*.md', '!zh/*.md', '!en/*.md'];
const START_TAG = '\n\n## CC_HIDE_IN_SUMMARY_START';
const END_TAG = '## CC_HIDE_IN_SUMMARY_END\n';
const START_TAG_IGNORE = '\n\nCC_IGNORE_START';
const END_TAG_IGNORE = '\nCC_IGNORE_END';
const PRUNE_LEFT_BAR_RE = /<[^<>]*>\s*CC_HIDE_IN_SUMMARY_START\s*<\/[^<>]*>(?:\n|\r|.)*<[^<>]*>\s*CC_HIDE_IN_SUMMARY_END\s*<\/[^<>]*>/g;
const PAGE_TITLE_RE = /^\s*(?:<!--(?:\n|.)*?-->\s*)*#*\s*(.*?)\s*\n/;
function parseListedPages (summaryPath) {
var content = Fs.readFileSync(summaryPath, 'utf8');
var re = /\[[^\]]*\]\(([^\)]+)\)/g; // should recreate a new RegExp every time this function called
var res = [];
var exec = null;
while ((exec = re.exec(content)) !== null) {
res.push(Path.resolve(Path.dirname(summaryPath), exec[1]));
}
return res;
}
function getPageTitle (path) {
var content = Fs.readFileSync(path, 'utf8');
var match = PAGE_TITLE_RE.exec(content);
if (match) {
return match[1];
}
else {
return Path.parse(path).name;
}
}
function fillSummary (path) {
var cwd = Path.dirname(path);
// get missing pages
var listedPages = parseListedPages(path);
var allPagesPattern = Path.join(cwd, '*/**/*.md');
var allPages = Globby.sync(allPagesPattern, { absolute: true });
var missingPages = Difference(allPages, listedPages);
// fill summary
var content = Fs.readFileSync(path, 'utf8');
var append = missingPages.map(x => `- [${getPageTitle(x)}](${Path.relative(cwd, x)})`).join('\n');
content = `${content}${START_TAG}\n\n${append}\n\n${END_TAG}`;
Fs.writeFileSync(path, content, 'utf8');
}
// add unlisted pages into SUMMARY.md to allow gitbook to build them
gulp.task('prebuild', ['restore-summary'], function () {
fillSummary('zh/SUMMARY.md');
fillSummary('en/SUMMARY.md');
});
gulp.task('preview', ['restore-summary', 'restore-ignore'], function (done) {
var includeFiles = program.only;
if (includeFiles) {
quickPreview(includeFiles, (error) => {
if (error) {
return done(error);
}
else {
openServer(done);
}
});
}
else {
fillSummary('zh/SUMMARY.md');
fillSummary('en/SUMMARY.md');
openServer(done);
}
});
function restoreSummary (path) {
var re = new RegExp(START_TAG + '(?:\\n|.)*' + END_TAG);
var content = Fs.readFileSync(path, 'utf8');
content = content.replace(re, '');
Fs.writeFileSync(path, content, 'utf8');
}
function restoreIgnore (path) {
var re = new RegExp(START_TAG_IGNORE + '(?:\\n|.)*' + END_TAG_IGNORE);
var content = Fs.readFileSync(path, 'utf8');
content = content.replace(re, '');
Fs.writeFileSync(path, content, 'utf8');
}
function openServer (done) {
var server = exec('gitbook serve --no-watch --open');
server.stderr.on('error', error => {
if(error) {
return done(error);
process.exit(1);
}
});
server.stdout.on('data', data => {
console.log(data.toString());
});
}
//only build the target file
function quickPreview (includeFiles, done) {
includeFiles = includeFiles.concat(FORBID_IGNORE_ARRAY);
var excludePattern = includeFiles.map(x => '!**/' + Path.basename(x, '.md') + '.md');
var allIgnorePages = Globby.sync(allPagesPattern.concat(excludePattern), { absolute: true });
allIgnorePages = allIgnorePages.map(x => '/' + Path.relative(__dirname,x).replace(/\\/g, '/'));
Fs.readFile('.bookignore', 'utf8', function (error, content) {
if (error) {
return done(error);
}
const fileContent = `${content}\n${START_TAG_IGNORE}\n${allIgnorePages.join('\n')}${END_TAG_IGNORE}`;
Fs.writeFile('.bookignore', fileContent, 'utf8', done);
});
}
// restore SUMMARY.md to keep repo clean
gulp.task('restore-summary', function () {
restoreSummary('zh/SUMMARY.md');
restoreSummary('en/SUMMARY.md');
});
gulp.task('restore-ignore', function () {
restoreIgnore('.bookignore');
});
function pruneLeftBar (dir) {
var allPagesPattern = Path.join(dir, '**/*.html');
var allPages = Globby.sync(allPagesPattern);
for (var i = 0; i < allPages.length; ++i) {
var path = allPages[i];
var content = Fs.readFileSync(path, 'utf8');
var result = content.replace(PRUNE_LEFT_BAR_RE, '');
if (content !== result) {
Fs.writeFileSync(path, result, 'utf8');
}
else {
throw 'Prune Summary Failed!';
}
}
}
// remove added pages from generated index.html
gulp.task('prune-left-bar', function () {
pruneLeftBar('_book/zh');
pruneLeftBar('_book/en');
});
gulp.task('postbuild', ['restore-summary', 'prune-left-bar']);