Skip to content

Commit

Permalink
Add gulp task to build languages map in Show language plugin (Fix #671)
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Aug 18, 2015
1 parent 2cb1326 commit 39bd827
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
3 changes: 3 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ var components = {
"option": "default",
"require": "clike"
},



"actionscript": {
"title": "ActionScript",
"require": "javascript",
Expand Down
47 changes: 45 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ var gulp = require('gulp'),
uglify = require('gulp-uglify'),
header = require('gulp-header'),
concat = require('gulp-concat'),
replace = require('gulp-replace'),
fs = require('fs'),

paths = {
componentsFile: 'components.js',
components: ['components/**/*.js', '!components/**/*.min.js'],
main: [
'components/prism-core.js',
Expand All @@ -14,7 +17,8 @@ var gulp = require('gulp'),
'components/prism-javascript.js',
'plugins/file-highlight/prism-file-highlight.js'
],
plugins: ['plugins/**/*.js', '!plugins/**/*.min.js']
plugins: ['plugins/**/*.js', '!plugins/**/*.min.js'],
showLanguagePlugin: 'plugins/show-language/prism-show-language.js'
};

gulp.task('components', function() {
Expand All @@ -33,7 +37,7 @@ gulp.task('build', function() {
.pipe(gulp.dest('./'));
});

gulp.task('plugins', function() {
gulp.task('plugins', ['show-language-plugin'], function() {
return gulp.src(paths.plugins)
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
Expand All @@ -45,4 +49,43 @@ gulp.task('watch', function() {
gulp.watch(paths.plugins, ['plugins', 'build']);
});

gulp.task('show-language-plugin', function (cb) {
fs.readFile(paths.componentsFile, {
encoding: 'utf-8'
}, function (err, data) {
if (!err) {
data = data.replace(/^var\s+components\s*=\s*|;\s*$/g, '');
try {
data = JSON.parse(data);

var languagesMap = {};
for (var p in data.languages) {
if (p !== 'meta') {
var title = data.languages[p].displayTitle || data.languages[p].title;
var ucfirst = p.substring(0, 1).toUpperCase() + p.substring(1);
if (title !== ucfirst) {
languagesMap[p] = title;
}
}
}

var jsonLanguages = JSON.stringify(languagesMap);
var stream = gulp.src(paths.showLanguagePlugin)
.pipe(replace(
/\/\*languages_placeholder\[\*\/[\s\S]*?\/\*\]\*\//,
'/*languages_placeholder[*/' + jsonLanguages + '/*]*/'
))
.pipe(gulp.dest(paths.showLanguagePlugin.substring(0, paths.showLanguagePlugin.lastIndexOf('/'))));
stream.on('error', cb);
stream.on('end', cb);

} catch (e) {
cb(e);
}
} else {
cb(err);
}
});
});

gulp.task('default', ['components', 'plugins', 'build']);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"gulp-header": "^1.0.5",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.3.1",
"gulp-replace": "^0.5.4",
"mocha": "^2.2.5"
}
}
8 changes: 3 additions & 5 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ if (!self.Prism) {
return;
}

var Languages = {
'csharp': 'C#',
'cpp': 'C++'
};
// The languages map is built automatically with gulp
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"}/*]*/;
Prism.hooks.add('before-highlight', function(env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
return;
}
var language = Languages[env.language] || env.language;
var language = Languages[env.language] || (env.language.substring(0, 1).toUpperCase() + env.language.substring(1));
pre.setAttribute('data-language', language);
});

Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39bd827

Please sign in to comment.