You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.2 KiB
JavaScript
110 lines
3.2 KiB
JavaScript
10 years ago
|
/**
|
||
|
* Original by Aaron Harun: http://aahacreative.com/2012/07/31/php-syntax-highlighting-prism/
|
||
|
* Modified by Miles Johnson: http://milesj.me
|
||
|
*
|
||
|
* Supports the following:
|
||
|
* - Extends clike syntax
|
||
|
* - Support for PHP 5.3+ (namespaces, traits, generators, etc)
|
||
|
* - Smarter constant and function matching
|
||
|
*
|
||
|
* Adds the following new token classes:
|
||
|
* constant, delimiter, variable, function, package
|
||
|
*/
|
||
|
|
||
|
Prism.languages.php = Prism.languages.extend('clike', {
|
||
|
'keyword': /\b(and|or|xor|array|as|break|case|cfunction|class|const|continue|declare|default|die|do|else|elseif|enddeclare|endfor|endforeach|endif|endswitch|endwhile|extends|for|foreach|function|include|include_once|global|if|new|return|static|switch|use|require|require_once|var|while|abstract|interface|public|implements|private|protected|parent|throw|null|echo|print|trait|namespace|final|yield|goto|instanceof|finally|try|catch)\b/ig,
|
||
|
'constant': /\b[A-Z0-9_]{2,}\b/g,
|
||
|
'comment': {
|
||
|
pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|(^|[^:])(\/\/).*?(\r?\n|$))/g,
|
||
|
lookbehind: true
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Shell-like comments are matched after strings, because they are less
|
||
|
// common than strings containing hashes...
|
||
|
Prism.languages.insertBefore('php', 'class-name', {
|
||
|
'shell-comment': {
|
||
|
pattern: /(^|[^\\])#.*?(\r?\n|$)/g,
|
||
|
lookbehind: true,
|
||
|
alias: 'comment'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Prism.languages.insertBefore('php', 'keyword', {
|
||
|
'delimiter': /(\?>|<\?php|<\?)/ig,
|
||
|
'variable': /(\$\w+)\b/ig,
|
||
|
'package': {
|
||
|
pattern: /(\\|namespace\s+|use\s+)[\w\\]+/g,
|
||
|
lookbehind: true,
|
||
|
inside: {
|
||
|
punctuation: /\\/
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Must be defined after the function pattern
|
||
|
Prism.languages.insertBefore('php', 'operator', {
|
||
|
'property': {
|
||
|
pattern: /(->)[\w]+/g,
|
||
|
lookbehind: true
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Add HTML support of the markup language exists
|
||
|
if (Prism.languages.markup) {
|
||
|
|
||
|
// Tokenize all inline PHP blocks that are wrapped in <?php ?>
|
||
|
// This allows for easy PHP + markup highlighting
|
||
|
Prism.hooks.add('before-highlight', function(env) {
|
||
|
if (env.language !== 'php') {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
env.tokenStack = [];
|
||
|
|
||
|
env.backupCode = env.code;
|
||
|
env.code = env.code.replace(/(?:<\?php|<\?)[\w\W]*?(?:\?>)/ig, function(match) {
|
||
|
env.tokenStack.push(match);
|
||
|
|
||
|
return '{{{PHP' + env.tokenStack.length + '}}}';
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// Restore env.code for other plugins (e.g. line-numbers)
|
||
|
Prism.hooks.add('before-insert', function(env) {
|
||
|
if (env.language === 'php') {
|
||
|
env.code = env.backupCode;
|
||
|
delete env.backupCode;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Re-insert the tokens after highlighting
|
||
|
Prism.hooks.add('after-highlight', function(env) {
|
||
|
if (env.language !== 'php') {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
for (var i = 0, t; t = env.tokenStack[i]; i++) {
|
||
|
env.highlightedCode = env.highlightedCode.replace('{{{PHP' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php'));
|
||
|
}
|
||
|
|
||
|
env.element.innerHTML = env.highlightedCode;
|
||
|
});
|
||
|
|
||
|
// Wrap tokens in classes that are missing them
|
||
|
Prism.hooks.add('wrap', function(env) {
|
||
|
if (env.language === 'php' && env.type === 'markup') {
|
||
|
env.content = env.content.replace(/(\{\{\{PHP[0-9]+\}\}\})/g, "<span class=\"token php\">$1</span>");
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Add the rules before all others
|
||
|
Prism.languages.insertBefore('php', 'comment', {
|
||
|
'markup': {
|
||
|
pattern: /<[^?]\/?(.*?)>/g,
|
||
|
inside: Prism.languages.markup
|
||
|
},
|
||
|
'php': /\{\{\{PHP[0-9]+\}\}\}/g
|
||
|
});
|
||
|
}
|