prism-php.js 3.2 KB

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