prism-jade.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. (function(Prism) {
  2. Prism.languages.jade = {
  3. // Multiline stuff should appear before the rest
  4. 'multiline-comment': {
  5. pattern: /((?:^|\n)([\t ]*))\/\/.*(\n\2[\t ]+.+)*/,
  6. lookbehind: true,
  7. alias: 'comment'
  8. },
  9. // All the tag-related part is in lookbehind
  10. // so that it can be highlighted by the "tag" pattern
  11. 'multiline-script': {
  12. pattern: /((?:^|\n)([\t ]*)script\b.*\.[\t ]*)(\n(?:\2[\t ]+.+|\s*?(?=\n)))+/,
  13. lookbehind: true,
  14. inside: {
  15. rest: Prism.languages.javascript
  16. }
  17. },
  18. // See at the end of the file for known filters
  19. 'filter': {
  20. pattern: /((?:^|\n)([\t ]*)):.+(\n(?:\2[\t ]+.+|\s*?(?=\n)))+/,
  21. lookbehind: true,
  22. inside: {
  23. 'filter-name': {
  24. pattern: /^:[\w-]+/,
  25. alias: 'variable'
  26. }
  27. }
  28. },
  29. 'multiline-plain-text': {
  30. pattern: /((?:^|\n)([\t ]*)[\w\-#.]+\.[\t ]*)(\n(?:\2[\t ]+.+|\s*?(?=\n)))+/,
  31. lookbehind: true
  32. },
  33. 'markup': {
  34. pattern: /((?:^|\n)[\t ]*)<.+/,
  35. lookbehind: true,
  36. inside: {
  37. rest: Prism.languages.markup
  38. }
  39. },
  40. 'comment': {
  41. pattern: /((?:^|\n)[\t ]*)\/\/.+/,
  42. lookbehind: true
  43. },
  44. 'doctype': {
  45. pattern: /((?:^|\n)[\t ]*)doctype(?: .+)?/,
  46. lookbehind: true
  47. },
  48. // This handle all conditional and loop keywords
  49. 'flow-control': {
  50. pattern: /((?:^|\n)[\t ]*)(?:if|unless|else|case|when|default|each|while)(?: .+)?/,
  51. lookbehind: true,
  52. inside: {
  53. 'each': {
  54. pattern: /((?:^|\n)[\t ]*)each .+? in\b/,
  55. lookbehind: true,
  56. inside: {
  57. 'keyword': /\b(?:each|in)\b/,
  58. 'punctuation': /,/
  59. }
  60. },
  61. 'branch': {
  62. pattern: /((?:^|\n)[\t ]*)(?:if|unless|else|case|when|default|while)/,
  63. lookbehind: true,
  64. alias: 'keyword'
  65. },
  66. rest: Prism.languages.javascript
  67. }
  68. },
  69. 'keyword': {
  70. pattern: /((?:^|\n)[\t ]*)(?:block|extends|include|append|prepend)\b.+/,
  71. lookbehind: true
  72. },
  73. 'mixin': [
  74. // Declaration
  75. {
  76. pattern: /((?:^|\n)[\t ]*)mixin .+/,
  77. lookbehind: true,
  78. inside: {
  79. 'keyword': /^mixin/,
  80. 'function': /\w+(?=\s*\(|\s*$)/,
  81. 'punctuation': /[(),.]/
  82. }
  83. },
  84. // Usage
  85. {
  86. pattern: /((?:^|\n)[\t ]*)\+.+/,
  87. lookbehind: true,
  88. inside: {
  89. 'name': {
  90. pattern: /^\+\w+/,
  91. alias: 'function'
  92. },
  93. 'rest': Prism.languages.javascript
  94. }
  95. }
  96. ],
  97. 'script': {
  98. pattern: /((?:^|\n)[\t ]*script(?:(?:&[^(]+)?\([^)]+\))*) .+/,
  99. lookbehind: true,
  100. inside: {
  101. rest: Prism.languages.javascript
  102. }
  103. },
  104. 'plain-text': {
  105. pattern: /((?:^|\n)[\t ]*(?!-)[\w\-#.]*[\w\-](?:(?:&[^(]+)?\([^)]+\))*\/?[\t ]+).+/,
  106. lookbehind: true
  107. },
  108. 'tag': {
  109. pattern: /((?:^|\n)[\t ]*)(?!-)[\w\-#.]*[\w\-](?:(?:&[^(]+)?\([^)]+\))*\/?:?/,
  110. lookbehind: true,
  111. inside: {
  112. 'attributes': [
  113. {
  114. pattern: /&[^(]+\([^)]+\)/,
  115. inside: {
  116. rest: Prism.languages.javascript
  117. }
  118. },
  119. {
  120. pattern: /\([^)]+\)/,
  121. inside: {
  122. 'attr-value': {
  123. pattern: /(=\s*)(?:\{[^}]*\}|[^,)\n]+)/,
  124. lookbehind: true,
  125. inside: {
  126. rest: Prism.languages.javascript
  127. }
  128. },
  129. 'attr-name': /[\w-]+(?=\s*!?=|\s*[,)])/,
  130. 'punctuation': /[!=(),]/
  131. }
  132. }
  133. ],
  134. 'punctuation': /[:]/
  135. }
  136. },
  137. 'code': [
  138. {
  139. pattern: /((?:^|\n)[\t ]*(?:-|!?=)).+/,
  140. lookbehind: true,
  141. inside: {
  142. rest: Prism.languages.javascript
  143. }
  144. }
  145. ],
  146. 'punctuation': /[.\-!=|]/
  147. };
  148. var filter_pattern = '((?:^|\\n)([\\t ]*)):{{filter_name}}(\\n(?:\\2[\\t ]+.+|\\s*?(?=\\n)))+';
  149. // Non exhaustive list of available filters and associated languages
  150. var filters = [
  151. {filter:'atpl',language:'twig'},
  152. {filter:'coffee',language:'coffeescript'},
  153. 'ejs',
  154. 'handlebars',
  155. 'hogan',
  156. 'less',
  157. 'livescript',
  158. 'markdown',
  159. 'mustache',
  160. 'plates',
  161. {filter:'sass',language:'scss'},
  162. 'stylus',
  163. 'swig'
  164. ];
  165. var all_filters = {};
  166. for (var i = 0, l = filters.length; i < l; i++) {
  167. var filter = filters[i];
  168. filter = typeof filter === 'string' ? {filter: filter, language: filter} : filter;
  169. if (Prism.languages[filter.language]) {
  170. all_filters['filter-' + filter.filter] = {
  171. pattern: RegExp(filter_pattern.replace('{{filter_name}}', filter.filter)),
  172. lookbehind: true,
  173. inside: {
  174. 'filter-name': {
  175. pattern: /^:[\w-]+/,
  176. alias: 'variable'
  177. },
  178. rest: Prism.languages[filter.language]
  179. }
  180. }
  181. }
  182. }
  183. Prism.languages.insertBefore('jade', 'filter', all_filters);
  184. }(Prism));