Skip to content

Commit 39bd827

Browse files
committed
Add gulp task to build languages map in Show language plugin (Fix #671)
1 parent 2cb1326 commit 39bd827

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

components.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ var components = {
5454
"option": "default",
5555
"require": "clike"
5656
},
57+
58+
59+
5760
"actionscript": {
5861
"title": "ActionScript",
5962
"require": "javascript",

gulpfile.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ var gulp = require('gulp'),
33
uglify = require('gulp-uglify'),
44
header = require('gulp-header'),
55
concat = require('gulp-concat'),
6+
replace = require('gulp-replace'),
7+
fs = require('fs'),
68

79
paths = {
10+
componentsFile: 'components.js',
811
components: ['components/**/*.js', '!components/**/*.min.js'],
912
main: [
1013
'components/prism-core.js',
@@ -14,7 +17,8 @@ var gulp = require('gulp'),
1417
'components/prism-javascript.js',
1518
'plugins/file-highlight/prism-file-highlight.js'
1619
],
17-
plugins: ['plugins/**/*.js', '!plugins/**/*.min.js']
20+
plugins: ['plugins/**/*.js', '!plugins/**/*.min.js'],
21+
showLanguagePlugin: 'plugins/show-language/prism-show-language.js'
1822
};
1923

2024
gulp.task('components', function() {
@@ -33,7 +37,7 @@ gulp.task('build', function() {
3337
.pipe(gulp.dest('./'));
3438
});
3539

36-
gulp.task('plugins', function() {
40+
gulp.task('plugins', ['show-language-plugin'], function() {
3741
return gulp.src(paths.plugins)
3842
.pipe(uglify())
3943
.pipe(rename({ suffix: '.min' }))
@@ -45,4 +49,43 @@ gulp.task('watch', function() {
4549
gulp.watch(paths.plugins, ['plugins', 'build']);
4650
});
4751

52+
gulp.task('show-language-plugin', function (cb) {
53+
fs.readFile(paths.componentsFile, {
54+
encoding: 'utf-8'
55+
}, function (err, data) {
56+
if (!err) {
57+
data = data.replace(/^var\s+components\s*=\s*|;\s*$/g, '');
58+
try {
59+
data = JSON.parse(data);
60+
61+
var languagesMap = {};
62+
for (var p in data.languages) {
63+
if (p !== 'meta') {
64+
var title = data.languages[p].displayTitle || data.languages[p].title;
65+
var ucfirst = p.substring(0, 1).toUpperCase() + p.substring(1);
66+
if (title !== ucfirst) {
67+
languagesMap[p] = title;
68+
}
69+
}
70+
}
71+
72+
var jsonLanguages = JSON.stringify(languagesMap);
73+
var stream = gulp.src(paths.showLanguagePlugin)
74+
.pipe(replace(
75+
/\/\*languages_placeholder\[\*\/[\s\S]*?\/\*\]\*\//,
76+
'/*languages_placeholder[*/' + jsonLanguages + '/*]*/'
77+
))
78+
.pipe(gulp.dest(paths.showLanguagePlugin.substring(0, paths.showLanguagePlugin.lastIndexOf('/'))));
79+
stream.on('error', cb);
80+
stream.on('end', cb);
81+
82+
} catch (e) {
83+
cb(e);
84+
}
85+
} else {
86+
cb(err);
87+
}
88+
});
89+
});
90+
4891
gulp.task('default', ['components', 'plugins', 'build']);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"gulp-header": "^1.0.5",
2525
"gulp-rename": "^1.2.0",
2626
"gulp-uglify": "^0.3.1",
27+
"gulp-replace": "^0.5.4",
2728
"mocha": "^2.2.5"
2829
}
2930
}

plugins/show-language/prism-show-language.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ if (!self.Prism) {
44
return;
55
}
66

7-
var Languages = {
8-
'csharp': 'C#',
9-
'cpp': 'C++'
10-
};
7+
// The languages map is built automatically with gulp
8+
var Languages = /*languages_placeholder[*/{"css":"CSS","clike":"C-like","javascript":"JavaScript","actionscript":"ActionScript","apacheconf":"Apache Configuration","applescript":"AppleScript","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","http":"HTTP","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","nasm":"NASM","nsis":"NSIS","objectivec":"Objective-C","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","wiki":"Wiki markup","yaml":"YAML"}/*]*/;
119
Prism.hooks.add('before-highlight', function(env) {
1210
var pre = env.element.parentNode;
1311
if (!pre || !/pre/i.test(pre.nodeName)) {
1412
return;
1513
}
16-
var language = Languages[env.language] || env.language;
14+
var language = Languages[env.language] || (env.language.substring(0, 1).toUpperCase() + env.language.substring(1));
1715
pre.setAttribute('data-language', language);
1816
});
1917

plugins/show-language/prism-show-language.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)