OLD | NEW |
1 /** | 1 /** |
2 * Original by Aaron Harun: http://aahacreative.com/2012/07/31/php-syntax-highli
ghting-prism/ | 2 * Original by Aaron Harun: http://aahacreative.com/2012/07/31/php-syntax-highli
ghting-prism/ |
3 * Modified by Miles Johnson: http://milesj.me | 3 * Modified by Miles Johnson: http://milesj.me |
4 * | 4 * |
5 * Supports the following: | 5 * Supports the following: |
6 * - Extends clike syntax | 6 * - Extends clike syntax |
7 * - Support for PHP 5.3+ (namespaces, traits, generators, etc) | 7 * - Support for PHP 5.3+ (namespaces, traits, generators, etc) |
8 * - Smarter constant and function matching | 8 * - Smarter constant and function matching |
9 * | 9 * |
10 * Adds the following new token classes: | 10 * Adds the following new token classes: |
11 * constant, delimiter, variable, function, package | 11 * constant, delimiter, variable, function, package |
12 */ | 12 */ |
13 | 13 |
14 Prism.languages.php = Prism.languages.extend('clike', { | 14 Prism.languages.php = Prism.languages.extend('clike', { |
15 'keyword': /\b(and|or|xor|array|as|break|case|cfunction|class|const|cont
inue|declare|default|die|do|else|elseif|enddeclare|endfor|endforeach|endif|endsw
itch|endwhile|extends|for|foreach|function|include|include_once|global|if|new|re
turn|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, | 15 'keyword': /\b(and|or|xor|array|as|break|case|cfunction|class|const|cont
inue|declare|default|die|do|else|elseif|enddeclare|endfor|endforeach|endif|endsw
itch|endwhile|extends|for|foreach|function|include|include_once|global|if|new|re
turn|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/, | 16 'constant': /\b[A-Z0-9_]{2,}\b/, |
17 'comment': { | 17 'comment': { |
18 » » pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|(^|[^:])(\/\/).*?(\r?\n|$))
/, | 18 » » pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/, |
19 lookbehind: true | 19 lookbehind: true |
20 } | 20 } |
21 }); | 21 }); |
22 | 22 |
23 // Shell-like comments are matched after strings, because they are less | 23 // Shell-like comments are matched after strings, because they are less |
24 // common than strings containing hashes... | 24 // common than strings containing hashes... |
25 Prism.languages.insertBefore('php', 'class-name', { | 25 Prism.languages.insertBefore('php', 'class-name', { |
26 'shell-comment': { | 26 'shell-comment': { |
27 » » pattern: /(^|[^\\])#.*?(\r?\n|$)/, | 27 » » pattern: /(^|[^\\])#.*/, |
28 lookbehind: true, | 28 lookbehind: true, |
29 alias: 'comment' | 29 alias: 'comment' |
30 } | 30 } |
31 }); | 31 }); |
32 | 32 |
33 Prism.languages.insertBefore('php', 'keyword', { | 33 Prism.languages.insertBefore('php', 'keyword', { |
34 » 'delimiter': /(\?>|<\?php|<\?)/i, | 34 » 'delimiter': /\?>|<\?(?:php)?/i, |
35 » 'variable': /(\$\w+)\b/i, | 35 » 'variable': /\$\w+\b/i, |
36 'package': { | 36 'package': { |
37 pattern: /(\\|namespace\s+|use\s+)[\w\\]+/, | 37 pattern: /(\\|namespace\s+|use\s+)[\w\\]+/, |
38 lookbehind: true, | 38 lookbehind: true, |
39 inside: { | 39 inside: { |
40 punctuation: /\\/ | 40 punctuation: /\\/ |
41 } | 41 } |
42 } | 42 } |
43 }); | 43 }); |
44 | 44 |
45 // Must be defined after the function pattern | 45 // Must be defined after the function pattern |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } | 78 } |
79 }); | 79 }); |
80 | 80 |
81 // Re-insert the tokens after highlighting | 81 // Re-insert the tokens after highlighting |
82 Prism.hooks.add('after-highlight', function(env) { | 82 Prism.hooks.add('after-highlight', function(env) { |
83 if (env.language !== 'php') { | 83 if (env.language !== 'php') { |
84 return; | 84 return; |
85 } | 85 } |
86 | 86 |
87 for (var i = 0, t; t = env.tokenStack[i]; i++) { | 87 for (var i = 0, t; t = env.tokenStack[i]; i++) { |
88 » » » env.highlightedCode = env.highlightedCode.replace('{{{PH
P' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php')); | 88 » » » // The replace prevents $$, $&, $`, $', $n, $nn from bei
ng interpreted as special patterns |
| 89 » » » env.highlightedCode = env.highlightedCode.replace('{{{PH
P' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$
$')); |
89 } | 90 } |
90 | 91 |
91 env.element.innerHTML = env.highlightedCode; | 92 env.element.innerHTML = env.highlightedCode; |
92 }); | 93 }); |
93 | 94 |
94 // Wrap tokens in classes that are missing them | 95 // Wrap tokens in classes that are missing them |
95 Prism.hooks.add('wrap', function(env) { | 96 Prism.hooks.add('wrap', function(env) { |
96 if (env.language === 'php' && env.type === 'markup') { | 97 if (env.language === 'php' && env.type === 'markup') { |
97 env.content = env.content.replace(/(\{\{\{PHP[0-9]+\}\}\
})/g, "<span class=\"token php\">$1</span>"); | 98 env.content = env.content.replace(/(\{\{\{PHP[0-9]+\}\}\
})/g, "<span class=\"token php\">$1</span>"); |
98 } | 99 } |
99 }); | 100 }); |
100 | 101 |
101 // Add the rules before all others | 102 // Add the rules before all others |
102 Prism.languages.insertBefore('php', 'comment', { | 103 Prism.languages.insertBefore('php', 'comment', { |
103 'markup': { | 104 'markup': { |
104 pattern: /<[^?]\/?(.*?)>/, | 105 pattern: /<[^?]\/?(.*?)>/, |
105 inside: Prism.languages.markup | 106 inside: Prism.languages.markup |
106 }, | 107 }, |
107 'php': /\{\{\{PHP[0-9]+\}\}\}/ | 108 'php': /\{\{\{PHP[0-9]+\}\}\}/ |
108 }); | 109 }); |
109 } | 110 } |
OLD | NEW |