Added Handlebars language
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
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