UNPKG

3.58 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(function (Prism) {
14 Prism.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: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,
19 lookbehind: true
20 }
21 });
22
23 Prism.languages.insertBefore('php', 'string', {
24 'shell-comment': {
25 pattern: /(^|[^\\])#.*/,
26 lookbehind: true,
27 alias: 'comment'
28 }
29 });
30
31 Prism.languages.insertBefore('php', 'keyword', {
32 'delimiter': {
33 pattern: /\?>|<\?(?:php|=)?/i,
34 alias: 'important'
35 },
36 'variable': /\$+(?:\w+\b|(?={))/i,
37 'package': {
38 pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
39 lookbehind: true,
40 inside: {
41 punctuation: /\\/
42 }
43 }
44 });
45
46 // Must be defined after the function pattern
47 Prism.languages.insertBefore('php', 'operator', {
48 'property': {
49 pattern: /(->)[\w]+/,
50 lookbehind: true
51 }
52 });
53
54 Prism.languages.insertBefore('php', 'string', {
55 'nowdoc-string': {
56 pattern: /<<<'([^']+)'(?:\r\n?|\n)(?:.*(?:\r\n?|\n))*?\1;/,
57 greedy: true,
58 alias: 'string',
59 inside: {
60 'delimiter': {
61 pattern: /^<<<'[^']+'|[a-z_]\w*;$/i,
62 alias: 'symbol',
63 inside: {
64 'punctuation': /^<<<'?|[';]$/
65 }
66 }
67 }
68 },
69 'heredoc-string': {
70 pattern: /<<<(?:"([^"]+)"(?:\r\n?|\n)(?:.*(?:\r\n?|\n))*?\1;|([a-z_]\w*)(?:\r\n?|\n)(?:.*(?:\r\n?|\n))*?\2;)/i,
71 greedy: true,
72 alias: 'string',
73 inside: {
74 'delimiter': {
75 pattern: /^<<<(?:"[^"]+"|[a-z_]\w*)|[a-z_]\w*;$/i,
76 alias: 'symbol',
77 inside: {
78 'punctuation': /^<<<"?|[";]$/
79 }
80 },
81 'interpolation': null // See below
82 }
83 },
84 'single-quoted-string': {
85 pattern: /'(?:\\[\s\S]|[^\\'])*'/,
86 greedy: true,
87 alias: 'string'
88 },
89 'double-quoted-string': {
90 pattern: /"(?:\\[\s\S]|[^\\"])*"/,
91 greedy: true,
92 alias: 'string',
93 inside: {
94 'interpolation': null // See below
95 }
96 }
97 });
98 // The different types of PHP strings "replace" the C-like standard string
99 delete Prism.languages.php['string'];
100
101 var string_interpolation = {
102 pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[.+?]|->\w+)*)/,
103 lookbehind: true,
104 inside: {
105 rest: Prism.languages.php
106 }
107 };
108 Prism.languages.php['heredoc-string'].inside['interpolation'] = string_interpolation;
109 Prism.languages.php['double-quoted-string'].inside['interpolation'] = string_interpolation;
110
111 Prism.hooks.add('before-tokenize', function(env) {
112 if (!/(?:<\?php|<\?)/ig.test(env.code)) {
113 return;
114 }
115
116 var phpPattern = /(?:<\?php|<\?)[\s\S]*?(?:\?>|$)/ig;
117 Prism.languages['markup-templating'].buildPlaceholders(env, 'php', phpPattern);
118 });
119
120 Prism.hooks.add('after-tokenize', function(env) {
121 Prism.languages['markup-templating'].tokenizePlaceholders(env, 'php');
122 });
123
124}(Prism));
\No newline at end of file