prism-haml.js 3.4 KB

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