prism-coffeescript.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (function (Prism) {
  2. // Ignore comments starting with { to privilege string interpolation highlighting
  3. var comment = /#(?!\{).+/;
  4. var interpolation = {
  5. pattern: /#\{[^}]+\}/,
  6. alias: 'variable'
  7. };
  8. Prism.languages.coffeescript = Prism.languages.extend('javascript', {
  9. 'comment': comment,
  10. 'string': [
  11. // Strings are multiline
  12. {
  13. pattern: /'(?:\\[\s\S]|[^\\'])*'/,
  14. greedy: true
  15. },
  16. {
  17. // Strings are multiline
  18. pattern: /"(?:\\[\s\S]|[^\\"])*"/,
  19. greedy: true,
  20. inside: {
  21. 'interpolation': interpolation
  22. }
  23. }
  24. ],
  25. 'keyword': /\b(?:and|break|by|catch|class|continue|debugger|delete|do|each|else|extend|extends|false|finally|for|if|in|instanceof|is|isnt|let|loop|namespace|new|no|not|null|of|off|on|or|own|return|super|switch|then|this|throw|true|try|typeof|undefined|unless|until|when|while|window|with|yes|yield)\b/,
  26. 'class-member': {
  27. pattern: /@(?!\d)\w+/,
  28. alias: 'variable'
  29. }
  30. });
  31. Prism.languages.insertBefore('coffeescript', 'comment', {
  32. 'multiline-comment': {
  33. pattern: /###[\s\S]+?###/,
  34. alias: 'comment'
  35. },
  36. // Block regexp can contain comments and interpolation
  37. 'block-regex': {
  38. pattern: /\/{3}[\s\S]*?\/{3}/,
  39. alias: 'regex',
  40. inside: {
  41. 'comment': comment,
  42. 'interpolation': interpolation
  43. }
  44. }
  45. });
  46. Prism.languages.insertBefore('coffeescript', 'string', {
  47. 'inline-javascript': {
  48. pattern: /`(?:\\[\s\S]|[^\\`])*`/,
  49. inside: {
  50. 'delimiter': {
  51. pattern: /^`|`$/,
  52. alias: 'punctuation'
  53. },
  54. 'script': {
  55. pattern: /[\s\S]+/,
  56. alias: 'language-javascript',
  57. inside: Prism.languages.javascript
  58. }
  59. }
  60. },
  61. // Block strings
  62. 'multiline-string': [
  63. {
  64. pattern: /'''[\s\S]*?'''/,
  65. greedy: true,
  66. alias: 'string'
  67. },
  68. {
  69. pattern: /"""[\s\S]*?"""/,
  70. greedy: true,
  71. alias: 'string',
  72. inside: {
  73. interpolation: interpolation
  74. }
  75. }
  76. ]
  77. });
  78. Prism.languages.insertBefore('coffeescript', 'keyword', {
  79. // Object property
  80. 'property': /(?!\d)\w+(?=\s*:(?!:))/
  81. });
  82. delete Prism.languages.coffeescript['template-string'];
  83. Prism.languages.coffee = Prism.languages.coffeescript;
  84. }(Prism));