Skip to content

Commit

Permalink
Merge pull request #409 from Golmote/gh-pages
Browse files Browse the repository at this point in the history
Added Handlebars language
  • Loading branch information
LeaVerou committed Dec 9, 2014
2 parents 859a592 + c504438 commit 46b7513
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
12 changes: 8 additions & 4 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,21 @@ var components = {
"title": "Git",
"owner": "lgiraudel"
},
"scheme" : {
"title": "Scheme",
"owner" : "bacchus123"
},
"scheme" : {
"title": "Scheme",
"owner" : "bacchus123"
},
"nasm": {
"title": "nasm",
"owner": "rbmj"
},
"perl": {
"title": "Perl",
"owner": "Golmote"
},
"handlebars": {
"title": "Handlebars",
"owner": "Golmote"
}
},
"plugins": {
Expand Down
92 changes: 92 additions & 0 deletions components/prism-handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
Prism.languages.handlebars = {
'expression': {
pattern: /\{\{\{[\w\W]+?\}\}\}|\{\{[\w\W]+?\}\}/g,
inside: {
'comment': {
pattern: /(\{\{)![\w\W]*(?=\}\})/g,
lookbehind: true
},
'delimiter': {
pattern: /^\{\{\{?|\}\}\}?$/ig,
alias: 'punctuation'
},
'string': /(["'])(\\?.)+?\1/g,
'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/g,
'boolean': /\b(true|false)\b/g,
'block': {
pattern: /^(\s*~?\s*)[#\/]\w+/ig,
lookbehind: true,
alias: 'keyword'
},
'brackets': {
pattern: /\[[^\]]+\]/,
inside: {
punctuation: /\[|\]/g,
variable: /[\w\W]+/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
});
}

1 change: 1 addition & 0 deletions components/prism-handlebars.min.js

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

0 comments on commit 46b7513

Please sign in to comment.