Commit c272a04a763ce81e1f8e7e323d491f2186de0074

Golmote 2014-12-08T23:04:02

Added Handlebars language

diff --git a/components/prism-handlebars.js b/components/prism-handlebars.js
new file mode 100644
index 0000000..a54bcf5
--- /dev/null
+++ b/components/prism-handlebars.js
@@ -0,0 +1,90 @@
+Prism.languages.handlebars = {
+	'expression': {
+		pattern: /\{\{\{[\w\W]+?\}\}\}|\{\{[\w\W]+?\}\}/g,
+		inside: {
+			'comment': {
+				pattern: /(\{\{)![\w\W]*(?=\}\})/g,
+				lookbehind: true
+			},
+			'delimiter': {
+				pattern: /\{\{\{?|\}\}\}?/ig,
+				alias: 'punctuation'
+			},
+			'block': {
+				pattern: /^(\s*~?\s*)[#\/]\w+/ig,
+				lookbehind: true,
+				alias: 'keyword'
+			},
+			'brackets': {
+				pattern: /\[[^\]]+\]/,
+				inside: {
+					punctuation: /\[|\]/g,
+					variable: /[\w\W]+/g
+				}
+			},
+			'string': /(["'])(\\?.)+?\1/g,
+			'punctuation': /[!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~]/g,
+			'variable': /[^!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~]+/g
+		}
+	}
+};
+
+if (Prism.languages.markup) {
+
+	// Tokenize all inline Handlebars expressions that are wrapped in {{ }} or {{{ }}}
+	// This allows for easy Handlebars + markup highlighting
+	Prism.hooks.add('before-highlight', function(env) {
+		console.log(env.language);
+		if (env.language !== 'handlebars') {
+			return;
+		}
+
+		env.tokenStack = [];
+
+		env.backupCode = env.code;
+		env.code = env.code.replace(/\{\{\{[\w\W]+?\}\}\}|\{\{[\w\W]+?\}\}/ig, function(match) {
+			console.log(match);
+			env.tokenStack.push(match);
+
+			return '___HANDLEBARS' + env.tokenStack.length + '___';
+		});
+	});
+
+	// Restore env.code for other plugins (e.g. line-numbers)
+	Prism.hooks.add('before-insert', function(env) {
+		if (env.language === 'handlebars') {
+			env.code = env.backupCode;
+			delete env.backupCode;
+		}
+	});
+
+	// Re-insert the tokens after highlighting
+	Prism.hooks.add('after-highlight', function(env) {
+		if (env.language !== 'handlebars') {
+			return;
+		}
+
+		for (var i = 0, t; t = env.tokenStack[i]; i++) {
+			env.highlightedCode = env.highlightedCode.replace('___HANDLEBARS' + (i + 1) + '___', Prism.highlight(t, env.grammar, 'handlebars'));
+		}
+
+		env.element.innerHTML = env.highlightedCode;
+	});
+
+	// Wrap tokens in classes that are missing them
+	Prism.hooks.add('wrap', function(env) {
+		if (env.language === 'handlebars' && env.type === 'markup') {
+			env.content = env.content.replace(/(___HANDLEBARS[0-9]+___)/g, "<span class=\"token handlebars\">$1</span>");
+		}
+	});
+
+	// Add the rules before all others
+	Prism.languages.insertBefore('handlebars', 'expression', {
+		'markup': {
+			pattern: /<[^?]\/?(.*?)>/g,
+			inside: Prism.languages.markup
+		},
+		'handlebars': /___HANDLEBARS[0-9]+___/g
+	});
+}
+
diff --git a/components/prism-handlebars.min.js b/components/prism-handlebars.min.js
new file mode 100644
index 0000000..206d085
--- /dev/null
+++ b/components/prism-handlebars.min.js
@@ -0,0 +1 @@
+Prism.languages.handlebars={expression:{pattern:/\{\{\{[\w\W]+?\}\}\}|\{\{[\w\W]+?\}\}/g,inside:{comment:{pattern:/(\{\{)![\w\W]*(?=\}\})/g,lookbehind:!0},delimiter:{pattern:/\{\{\{?|\}\}\}?/gi,alias:"punctuation"},block:{pattern:/^(\s*~?\s*)[#\/]\w+/gi,lookbehind:!0,alias:"keyword"},brackets:{pattern:/\[[^\]]+\]/,inside:{punctuation:/\[|\]/g,variable:/[\w\W]+/g}},string:/(["'])(\\?.)+?\1/g,punctuation:/[!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~]/g,variable:/[^!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~]+/g}}},Prism.languages.markup&&(Prism.hooks.add("before-highlight",function(a){console.log(a.language),"handlebars"===a.language&&(a.tokenStack=[],a.backupCode=a.code,a.code=a.code.replace(/\{\{\{[\w\W]+?\}\}\}|\{\{[\w\W]+?\}\}/gi,function(b){return console.log(b),a.tokenStack.push(b),"___HANDLEBARS"+a.tokenStack.length+"___"}))}),Prism.hooks.add("before-insert",function(a){"handlebars"===a.language&&(a.code=a.backupCode,delete a.backupCode)}),Prism.hooks.add("after-highlight",function(a){if("handlebars"===a.language){for(var c,b=0;c=a.tokenStack[b];b++)a.highlightedCode=a.highlightedCode.replace("___HANDLEBARS"+(b+1)+"___",Prism.highlight(c,a.grammar,"handlebars"));a.element.innerHTML=a.highlightedCode}}),Prism.hooks.add("wrap",function(a){"handlebars"===a.language&&"markup"===a.type&&(a.content=a.content.replace(/(___HANDLEBARS[0-9]+___)/g,'<span class="token handlebars">$1</span>'))}),Prism.languages.insertBefore("handlebars","expression",{markup:{pattern:/<[^?]\/?(.*?)>/g,inside:Prism.languages.markup},handlebars:/___HANDLEBARS[0-9]+___/g}));
\ No newline at end of file