prism-haml.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* TODO
  2. Handle multiline code after tag
  3. %foo= some |
  4. multiline |
  5. code |
  6. */
  7. (function (Prism) {
  8. Prism.languages.haml = {
  9. // Multiline stuff should appear before the rest
  10. 'multiline-comment': {
  11. pattern: /((?:^|\r?\n|\r)([\t ]*))(?:\/|-#).*(?:(?:\r?\n|\r)\2[\t ].+)*/,
  12. lookbehind: true,
  13. alias: 'comment'
  14. },
  15. 'multiline-code': [
  16. {
  17. pattern: /((?:^|\r?\n|\r)([\t ]*)(?:[~-]|[&!]?=)).*,[\t ]*(?:(?:\r?\n|\r)\2[\t ].*,[\t ]*)*(?:(?:\r?\n|\r)\2[\t ].+)/,
  18. lookbehind: true,
  19. inside: Prism.languages.ruby
  20. },
  21. {
  22. pattern: /((?:^|\r?\n|\r)([\t ]*)(?:[~-]|[&!]?=)).*\|[\t ]*(?:(?:\r?\n|\r)\2[\t ].*\|[\t ]*)*/,
  23. lookbehind: true,
  24. inside: Prism.languages.ruby
  25. }
  26. ],
  27. // See at the end of the file for known filters
  28. 'filter': {
  29. pattern: /((?:^|\r?\n|\r)([\t ]*)):[\w-]+(?:(?:\r?\n|\r)(?:\2[\t ].+|\s*?(?=\r?\n|\r)))+/,
  30. lookbehind: true,
  31. inside: {
  32. 'filter-name': {
  33. pattern: /^:[\w-]+/,
  34. alias: 'symbol'
  35. }
  36. }
  37. },
  38. 'markup': {
  39. pattern: /((?:^|\r?\n|\r)[\t ]*)<.+/,
  40. lookbehind: true,
  41. inside: Prism.languages.markup
  42. },
  43. 'doctype': {
  44. pattern: /((?:^|\r?\n|\r)[\t ]*)!!!(?: .+)?/,
  45. lookbehind: true
  46. },
  47. 'tag': {
  48. // Allows for one nested group of braces
  49. pattern: /((?:^|\r?\n|\r)[\t ]*)[%.#][\w\-#.]*[\w\-](?:\([^)]+\)|\{(?:\{[^}]+\}|[^{}])+\}|\[[^\]]+\])*[\/<>]*/,
  50. lookbehind: true,
  51. inside: {
  52. 'attributes': [
  53. {
  54. // Lookbehind tries to prevent interpolations from breaking it all
  55. // Allows for one nested group of braces
  56. pattern: /(^|[^#])\{(?:\{[^}]+\}|[^{}])+\}/,
  57. lookbehind: true,
  58. inside: Prism.languages.ruby
  59. },
  60. {
  61. pattern: /\([^)]+\)/,
  62. inside: {
  63. 'attr-value': {
  64. pattern: /(=\s*)(?:"(?:\\.|[^\\"\r\n])*"|[^)\s]+)/,
  65. lookbehind: true
  66. },
  67. 'attr-name': /[\w:-]+(?=\s*!?=|\s*[,)])/,
  68. 'punctuation': /[=(),]/
  69. }
  70. },
  71. {
  72. pattern: /\[[^\]]+\]/,
  73. inside: Prism.languages.ruby
  74. }
  75. ],
  76. 'punctuation': /[<>]/
  77. }
  78. },
  79. 'code': {
  80. pattern: /((?:^|\r?\n|\r)[\t ]*(?:[~-]|[&!]?=)).+/,
  81. lookbehind: true,
  82. inside: Prism.languages.ruby
  83. },
  84. // Interpolations in plain text
  85. 'interpolation': {
  86. pattern: /#\{[^}]+\}/,
  87. inside: {
  88. 'delimiter': {
  89. pattern: /^#\{|\}$/,
  90. alias: 'punctuation'
  91. },
  92. 'ruby': {
  93. pattern: /[\s\S]+/,
  94. inside: Prism.languages.ruby
  95. }
  96. }
  97. },
  98. 'punctuation': {
  99. pattern: /((?:^|\r?\n|\r)[\t ]*)[~=\-&!]+/,
  100. lookbehind: true
  101. }
  102. };
  103. var filter_pattern = '((?:^|\\r?\\n|\\r)([\\t ]*)):{{filter_name}}(?:(?:\\r?\\n|\\r)(?:\\2[\\t ].+|\\s*?(?=\\r?\\n|\\r)))+';
  104. // Non exhaustive list of available filters and associated languages
  105. var filters = [
  106. 'css',
  107. { filter: 'coffee', language: 'coffeescript' },
  108. 'erb',
  109. 'javascript',
  110. 'less',
  111. 'markdown',
  112. 'ruby',
  113. 'scss',
  114. 'textile'
  115. ];
  116. var all_filters = {};
  117. for (var i = 0, l = filters.length; i < l; i++) {
  118. var filter = filters[i];
  119. filter = typeof filter === 'string' ? { filter: filter, language: filter } : filter;
  120. if (Prism.languages[filter.language]) {
  121. all_filters['filter-' + filter.filter] = {
  122. pattern: RegExp(filter_pattern.replace('{{filter_name}}', function () { return filter.filter; })),
  123. lookbehind: true,
  124. inside: {
  125. 'filter-name': {
  126. pattern: /^:[\w-]+/,
  127. alias: 'symbol'
  128. },
  129. 'text': {
  130. pattern: /[\s\S]+/,
  131. alias: [filter.language, 'language-' + filter.language],
  132. inside: Prism.languages[filter.language]
  133. }
  134. }
  135. };
  136. }
  137. }
  138. Prism.languages.insertBefore('haml', 'filter', all_filters);
  139. }(Prism));