Commit 39bd8278ec700dd651bf99e60f62bea0ffb9b378

Golmote 2015-08-18T19:00:24

Add gulp task to build languages map in Show language plugin (Fix #671)

diff --git a/components.js b/components.js
index 169d432..b9ce823 100644
--- a/components.js
+++ b/components.js
@@ -54,6 +54,9 @@ var components = {
 			"option": "default",
 			"require": "clike"
 		},
+
+
+
 		"actionscript": {
 			"title": "ActionScript",
 			"require": "javascript",
diff --git a/gulpfile.js b/gulpfile.js
index 25de6e5..51af93c 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -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',
@@ -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() {
@@ -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' }))
@@ -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']);
diff --git a/package.json b/package.json
index 2f8af95..b00ea8f 100644
--- a/package.json
+++ b/package.json
@@ -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"
   }
 }
diff --git a/plugins/show-language/prism-show-language.js b/plugins/show-language/prism-show-language.js
index 7b345d3..ac43bd6 100644
--- a/plugins/show-language/prism-show-language.js
+++ b/plugins/show-language/prism-show-language.js
@@ -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);
 });
 
diff --git a/plugins/show-language/prism-show-language.min.js b/plugins/show-language/prism-show-language.min.js
index 3f9280d..849fc6d 100644
--- a/plugins/show-language/prism-show-language.min.js
+++ b/plugins/show-language/prism-show-language.min.js
@@ -1 +1 @@
-!function(){if(self.Prism){var e={csharp:"C#",cpp:"C++"};Prism.hooks.add("before-highlight",function(a){var t=a.element.parentNode;if(t&&/pre/i.test(t.nodeName)){var i=e[a.language]||a.language;t.setAttribute("data-language",i)}})}}();
\ No newline at end of file
+!function(){if(self.Prism){var e={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(s){var t=s.element.parentNode;if(t&&/pre/i.test(t.nodeName)){var a=e[s.language]||s.language.substring(0,1).toUpperCase()+s.language.substring(1);t.setAttribute("data-language",a)}})}}();
\ No newline at end of file