UNPKG

3.31 kBJavaScriptView Raw
1/**
2 * Original by Aaron Harun: http://aahacreative.com/2012/07/31/php-syntax-highlighting-prism/
3 * Modified by Miles Johnson: http://milesj.me
4 *
5 * Supports the following:
6 * - Extends clike syntax
7 * - Support for PHP 5.3+ (namespaces, traits, generators, etc)
8 * - Smarter constant and function matching
9 *
10 * Adds the following new token classes:
11 * constant, delimiter, variable, function, package
12 */
13
14Prism.languages.php = Prism.languages.extend('clike', {
15 '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/i,
16 'constant': /\b[A-Z0-9_]{2,}\b/,
17 'comment': {
18 pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|(^|[^:])(\/\/).*?(\r?\n|$))/,
19 lookbehind: true
20 }
21});
22
23// Shell-like comments are matched after strings, because they are less
24// common than strings containing hashes...
25Prism.languages.insertBefore('php', 'class-name', {
26 'shell-comment': {
27 pattern: /(^|[^\\])#.*?(\r?\n|$)/,
28 lookbehind: true,
29 alias: 'comment'
30 }
31});
32
33Prism.languages.insertBefore('php', 'keyword', {
34 'delimiter': /(\?>|<\?php|<\?)/i,
35 'variable': /(\$\w+)\b/i,
36 'package': {
37 pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
38 lookbehind: true,
39 inside: {
40 punctuation: /\\/
41 }
42 }
43});
44
45// Must be defined after the function pattern
46Prism.languages.insertBefore('php', 'operator', {
47 'property': {
48 pattern: /(->)[\w]+/,
49 lookbehind: true
50 }
51});
52
53// Add HTML support of the markup language exists
54if (Prism.languages.markup) {
55
56 // Tokenize all inline PHP blocks that are wrapped in <?php ?>
57 // This allows for easy PHP + markup highlighting
58 Prism.hooks.add('before-highlight', function(env) {
59 if (env.language !== 'php') {
60 return;
61 }
62
63 env.tokenStack = [];
64
65 env.backupCode = env.code;
66 env.code = env.code.replace(/(?:<\?php|<\?)[\w\W]*?(?:\?>)/ig, function(match) {
67 env.tokenStack.push(match);
68
69 return '{{{PHP' + env.tokenStack.length + '}}}';
70 });
71 });
72
73 // Restore env.code for other plugins (e.g. line-numbers)
74 Prism.hooks.add('before-insert', function(env) {
75 if (env.language === 'php') {
76 env.code = env.backupCode;
77 delete env.backupCode;
78 }
79 });
80
81 // Re-insert the tokens after highlighting
82 Prism.hooks.add('after-highlight', function(env) {
83 if (env.language !== 'php') {
84 return;
85 }
86
87 for (var i = 0, t; t = env.tokenStack[i]; i++) {
88 env.highlightedCode = env.highlightedCode.replace('{{{PHP' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php'));
89 }
90
91 env.element.innerHTML = env.highlightedCode;
92 });
93
94 // Wrap tokens in classes that are missing them
95 Prism.hooks.add('wrap', function(env) {
96 if (env.language === 'php' && env.type === 'markup') {
97 env.content = env.content.replace(/(\{\{\{PHP[0-9]+\}\}\})/g, "<span class=\"token php\">$1</span>");
98 }
99 });
100
101 // Add the rules before all others
102 Prism.languages.insertBefore('php', 'comment', {
103 'markup': {
104 pattern: /<[^?]\/?(.*?)>/,
105 inside: Prism.languages.markup
106 },
107 'php': /\{\{\{PHP[0-9]+\}\}\}/
108 });
109}