forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
197 lines (170 loc) · 5.1 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const { src, dest, series, parallel, watch } = require('gulp');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
const header = require('gulp-header');
const concat = require('gulp-concat');
const replace = require('gulp-replace');
const pump = require('pump');
const fs = require('fs');
const paths = {
componentsFile: 'components.json',
componentsFileJS: 'components.js',
components: ['components/**/*.js', '!components/index.js', '!components/**/*.min.js'],
main: [
'components/prism-core.js',
'components/prism-markup.js',
'components/prism-css.js',
'components/prism-clike.js',
'components/prism-javascript.js',
'plugins/file-highlight/prism-file-highlight.js'
],
plugins: ['plugins/**/*.js', '!plugins/**/*.min.js'],
showLanguagePlugin: 'plugins/show-language/prism-show-language.js',
autoloaderPlugin: 'plugins/autoloader/prism-autoloader.js',
changelog: 'CHANGELOG.md'
};
const componentsPromise = new Promise((resolve, reject) => {
fs.readFile(paths.componentsFile, {
encoding: 'utf-8'
}, (err, data) => {
if (!err) {
resolve(JSON.parse(data));
} else {
reject(err);
}
});
});
function inlineRegexSource() {
return replace(
/\/((?:[^\n\r[\\\/]|\\.|\[(?:[^\n\r\\\]]|\\.)*\])*)\/\.source\b/g,
(m, source) => {
// escape backslashes
source = source.replace(/\\/g, '\\\\');
// escape single quotes
source = source.replace(/'/g, "\\'");
// unescape characters like \\n and \\t to \n and \t
source = source.replace(/(^|[^\\])\\\\([nrt0])/g, '$1\\$2');
// wrap source in single quotes
return "'" + source + "'";
}
);
}
function minifyJS() {
return [
inlineRegexSource(),
uglify()
];
}
function minifyComponents(cb) {
pump([src(paths.components), ...minifyJS(), rename({ suffix: '.min' }), dest('components')], cb);
}
function minifyPlugins(cb) {
pump([src(paths.plugins), ...minifyJS(), rename({ suffix: '.min' }), dest('plugins')], cb);
}
function build(cb) {
pump([src(paths.main), header(`
/* **********************************************
Begin <%= file.relative %>
********************************************** */
`), concat('prism.js'), dest('./')], cb);
}
function componentsJsonToJs(cb) {
componentsPromise.then(data => {
const js = `var components = ${JSON.stringify(data)};
if (typeof module !== 'undefined' && module.exports) { module.exports = components; }`;
fs.writeFile(paths.componentsFileJS, js, cb);
});
}
function watchComponentsAndPlugins() {
watch(paths.components, parallel(minifyComponents, build));
watch(paths.plugins, parallel(minifyPlugins, build));
}
function languagePlugins(cb) {
componentsPromise.then(data => {
const languagesMap = {};
const dependenciesMap = {};
/**
* Tries to guess the name of a language given its id.
*
* From `prism-show-language.js`.
*
* @param {string} id The language id.
* @returns {string}
*/
function guessTitle(id) {
if (!id) {
return id;
}
return (id.substring(0, 1).toUpperCase() + id.substring(1)).replace(/s(?=cript)/, 'S');
}
function addLanguageTitle(key, title) {
if (!languagesMap[key] && guessTitle(key) !== title) {
languagesMap[key] = title;
}
}
for (const p in data.languages) {
if (p !== 'meta') {
const title = data.languages[p].displayTitle || data.languages[p].title;
addLanguageTitle(p, title);
for (const name in data.languages[p].aliasTitles) {
addLanguageTitle(name, data.languages[p].aliasTitles[name]);
}
if (data.languages[p].alias) {
if (typeof data.languages[p].alias === 'string') {
addLanguageTitle(data.languages[p].alias, title);
} else {
data.languages[p].alias.forEach(function (alias) {
addLanguageTitle(alias, title);
});
}
}
if (data.languages[p].require) {
dependenciesMap[p] = data.languages[p].require;
}
}
}
const jsonLanguagesMap = JSON.stringify(languagesMap);
const jsonDependenciesMap = JSON.stringify(dependenciesMap);
const tasks = [
{ plugin: paths.showLanguagePlugin, map: jsonLanguagesMap },
{ plugin: paths.autoloaderPlugin, map: jsonDependenciesMap }
];
let cpt = 0;
const l = tasks.length;
const done = () => {
cpt++;
if (cpt === l) {
cb && cb();
}
};
for (const task of tasks) {
const stream = src(task.plugin)
.pipe(replace(
/\/\*languages_placeholder\[\*\/[\s\S]*?\/\*\]\*\//,
'/*languages_placeholder[*/' + task.map + '/*]*/'
))
.pipe(dest(task.plugin.substring(0, task.plugin.lastIndexOf('/'))));
stream.on('error', done);
stream.on('end', done);
}
});
}
function changelog(cb) {
return pump([
src(paths.changelog),
replace(
/#(\d+)(?![\d\]])/g,
'[#$1](https://github.com/PrismJS/prism/issues/$1)'
),
replace(
/\[[\da-f]+(?:, *[\da-f]+)*\]/g,
m => m.replace(/([\da-f]{7})[\da-f]*/g, '[`$1`](https://github.com/PrismJS/prism/commit/$1)')
),
dest('.')
], cb);
}
const components = minifyComponents;
const plugins = series(languagePlugins, minifyPlugins);
exports.watch = watchComponentsAndPlugins;
exports.default = parallel(components, plugins, componentsJsonToJs, build);
exports.changelog = changelog;