forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
204 lines (180 loc) · 5.21 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
198
199
200
201
202
203
204
var gulp = require('gulp'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
header = require('gulp-header'),
concat = require('gulp-concat'),
replace = require('gulp-replace'),
pump = require('pump'),
fs = require('fs'),
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'
},
componentsPromise = new Promise(function (resolve, reject) {
fs.readFile(paths.componentsFile, {
encoding: 'utf-8'
}, function (err, data) {
if (!err) {
resolve(JSON.parse(data));
} else {
reject(err);
}
});
}),
inlineRegexSource = function () {
return replace(
/\/((?:[^\n\r[\\\/]|\\.|\[(?:[^\n\r\\\]]|\\.)*\])*)\/\.source\b/g,
function (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 + "'";
}
);
};
gulp.task('components', function(cb) {
pump(
[
gulp.src(paths.components),
inlineRegexSource(),
uglify(),
rename({ suffix: '.min' }),
gulp.dest('components')
],
cb
);
});
gulp.task('build', function() {
return gulp.src(paths.main)
.pipe(header('\n/* **********************************************\n' +
' Begin <%= file.relative %>\n' +
'********************************************** */\n\n'))
.pipe(concat('prism.js'))
.pipe(gulp.dest('./'));
});
function plugins(cb) {
pump(
[
gulp.src(paths.plugins),
inlineRegexSource(),
uglify(),
rename({ suffix: '.min' }),
gulp.dest('plugins')
],
cb
);
}
gulp.task('components-json', function (cb) {
componentsPromise.then(function (data) {
data = 'var components = ' + JSON.stringify(data) + ';\n' +
'if (typeof module !== \'undefined\' && module.exports) { module.exports = components; }';
fs.writeFile(paths.componentsFileJS, data, cb);
});
});
gulp.task('watch', function() {
gulp.watch(paths.components, gulp.parallel('components', 'build'));
gulp.watch(paths.plugins, gulp.parallel('plugins', 'build'));
});
gulp.task('languages-plugins', function (cb) {
componentsPromise.then(function (data) {
var languagesMap = {};
var 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 (var p in data.languages) {
if (p !== 'meta') {
var title = data.languages[p].displayTitle || data.languages[p].title;
addLanguageTitle(p, title);
for (var 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;
}
}
}
var jsonLanguagesMap = JSON.stringify(languagesMap);
var jsonDependenciesMap = JSON.stringify(dependenciesMap);
var tasks = [
{plugin: paths.showLanguagePlugin, map: jsonLanguagesMap},
{plugin: paths.autoloaderPlugin, map: jsonDependenciesMap}
];
var cpt = 0;
var l = tasks.length;
var done = function() {
cpt++;
if(cpt === l) {
cb && cb();
}
};
tasks.forEach(function(task) {
var stream = gulp.src(task.plugin)
.pipe(replace(
/\/\*languages_placeholder\[\*\/[\s\S]*?\/\*\]\*\//,
'/*languages_placeholder[*/' + task.map + '/*]*/'
))
.pipe(gulp.dest(task.plugin.substring(0, task.plugin.lastIndexOf('/'))));
stream.on('error', done);
stream.on('end', done);
});
});
});
gulp.task('plugins', gulp.series('languages-plugins', plugins));
gulp.task('changelog', function (cb) {
return gulp.src(paths.changelog)
.pipe(replace(
/#(\d+)(?![\d\]])/g,
'[#$1](https://github.com/PrismJS/prism/issues/$1)'
))
.pipe(replace(
/\[[\da-f]+(?:, *[\da-f]+)*\]/g,
function (match) {
return match.replace(/([\da-f]{7})[\da-f]*/g, '[`$1`](https://github.com/PrismJS/prism/commit/$1)');
}
))
.pipe(gulp.dest('.'));
});
gulp.task('default', gulp.parallel('components', 'components-json', 'plugins', 'build'));