liquid.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { languages } from '../fillers/monaco-editor-core.js';
  6. var EMPTY_ELEMENTS = [
  7. 'area',
  8. 'base',
  9. 'br',
  10. 'col',
  11. 'embed',
  12. 'hr',
  13. 'img',
  14. 'input',
  15. 'keygen',
  16. 'link',
  17. 'menuitem',
  18. 'meta',
  19. 'param',
  20. 'source',
  21. 'track',
  22. 'wbr'
  23. ];
  24. export var conf = {
  25. wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
  26. brackets: [
  27. ['<!--', '-->'],
  28. ['<', '>'],
  29. ['{{', '}}'],
  30. ['{%', '%}'],
  31. ['{', '}'],
  32. ['(', ')']
  33. ],
  34. autoClosingPairs: [
  35. { open: '{', close: '}' },
  36. { open: '%', close: '%' },
  37. { open: '[', close: ']' },
  38. { open: '(', close: ')' },
  39. { open: '"', close: '"' },
  40. { open: "'", close: "'" }
  41. ],
  42. surroundingPairs: [
  43. { open: '<', close: '>' },
  44. { open: '"', close: '"' },
  45. { open: "'", close: "'" }
  46. ],
  47. onEnterRules: [
  48. {
  49. beforeText: new RegExp("<(?!(?:" + EMPTY_ELEMENTS.join('|') + "))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$", 'i'),
  50. afterText: /^<\/(\w[\w\d]*)\s*>$/i,
  51. action: {
  52. indentAction: languages.IndentAction.IndentOutdent
  53. }
  54. },
  55. {
  56. beforeText: new RegExp("<(?!(?:" + EMPTY_ELEMENTS.join('|') + "))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$", 'i'),
  57. action: { indentAction: languages.IndentAction.Indent }
  58. }
  59. ]
  60. };
  61. export var language = {
  62. defaultToken: '',
  63. tokenPostfix: '',
  64. builtinTags: [
  65. 'if',
  66. 'else',
  67. 'elseif',
  68. 'endif',
  69. 'render',
  70. 'assign',
  71. 'capture',
  72. 'endcapture',
  73. 'case',
  74. 'endcase',
  75. 'comment',
  76. 'endcomment',
  77. 'cycle',
  78. 'decrement',
  79. 'for',
  80. 'endfor',
  81. 'include',
  82. 'increment',
  83. 'layout',
  84. 'raw',
  85. 'endraw',
  86. 'render',
  87. 'tablerow',
  88. 'endtablerow',
  89. 'unless',
  90. 'endunless'
  91. ],
  92. builtinFilters: [
  93. 'abs',
  94. 'append',
  95. 'at_least',
  96. 'at_most',
  97. 'capitalize',
  98. 'ceil',
  99. 'compact',
  100. 'date',
  101. 'default',
  102. 'divided_by',
  103. 'downcase',
  104. 'escape',
  105. 'escape_once',
  106. 'first',
  107. 'floor',
  108. 'join',
  109. 'json',
  110. 'last',
  111. 'lstrip',
  112. 'map',
  113. 'minus',
  114. 'modulo',
  115. 'newline_to_br',
  116. 'plus',
  117. 'prepend',
  118. 'remove',
  119. 'remove_first',
  120. 'replace',
  121. 'replace_first',
  122. 'reverse',
  123. 'round',
  124. 'rstrip',
  125. 'size',
  126. 'slice',
  127. 'sort',
  128. 'sort_natural',
  129. 'split',
  130. 'strip',
  131. 'strip_html',
  132. 'strip_newlines',
  133. 'times',
  134. 'truncate',
  135. 'truncatewords',
  136. 'uniq',
  137. 'upcase',
  138. 'url_decode',
  139. 'url_encode',
  140. 'where'
  141. ],
  142. constants: ['true', 'false'],
  143. operators: ['==', '!=', '>', '<', '>=', '<='],
  144. symbol: /[=><!]+/,
  145. identifier: /[a-zA-Z_][\w]*/,
  146. tokenizer: {
  147. root: [
  148. [/\{\%\s*comment\s*\%\}/, 'comment.start.liquid', '@comment'],
  149. [/\{\{/, { token: '@rematch', switchTo: '@liquidState.root' }],
  150. [/\{\%/, { token: '@rematch', switchTo: '@liquidState.root' }],
  151. [/(<)([\w\-]+)(\/>)/, ['delimiter.html', 'tag.html', 'delimiter.html']],
  152. [/(<)([:\w]+)/, ['delimiter.html', { token: 'tag.html', next: '@otherTag' }]],
  153. [/(<\/)([\w\-]+)/, ['delimiter.html', { token: 'tag.html', next: '@otherTag' }]],
  154. [/</, 'delimiter.html'],
  155. [/\{/, 'delimiter.html'],
  156. [/[^<{]+/] // text
  157. ],
  158. comment: [
  159. [/\{\%\s*endcomment\s*\%\}/, 'comment.end.liquid', '@pop'],
  160. [/./, 'comment.content.liquid']
  161. ],
  162. otherTag: [
  163. [
  164. /\{\{/,
  165. {
  166. token: '@rematch',
  167. switchTo: '@liquidState.otherTag'
  168. }
  169. ],
  170. [
  171. /\{\%/,
  172. {
  173. token: '@rematch',
  174. switchTo: '@liquidState.otherTag'
  175. }
  176. ],
  177. [/\/?>/, 'delimiter.html', '@pop'],
  178. [/"([^"]*)"/, 'attribute.value'],
  179. [/'([^']*)'/, 'attribute.value'],
  180. [/[\w\-]+/, 'attribute.name'],
  181. [/=/, 'delimiter'],
  182. [/[ \t\r\n]+/] // whitespace
  183. ],
  184. liquidState: [
  185. [/\{\{/, 'delimiter.output.liquid'],
  186. [/\}\}/, { token: 'delimiter.output.liquid', switchTo: '@$S2.$S3' }],
  187. [/\{\%/, 'delimiter.tag.liquid'],
  188. [/raw\s*\%\}/, 'delimiter.tag.liquid', '@liquidRaw'],
  189. [/\%\}/, { token: 'delimiter.tag.liquid', switchTo: '@$S2.$S3' }],
  190. { include: 'liquidRoot' }
  191. ],
  192. liquidRaw: [
  193. [/^(?!\{\%\s*endraw\s*\%\}).+/],
  194. [/\{\%/, 'delimiter.tag.liquid'],
  195. [/@identifier/],
  196. [/\%\}/, { token: 'delimiter.tag.liquid', next: '@root' }],
  197. ],
  198. liquidRoot: [
  199. [/\d+(\.\d+)?/, 'number.liquid'],
  200. [/"[^"]*"/, 'string.liquid'],
  201. [/'[^']*'/, 'string.liquid'],
  202. [/\s+/],
  203. [
  204. /@symbol/,
  205. {
  206. cases: {
  207. '@operators': 'operator.liquid',
  208. '@default': ''
  209. }
  210. }
  211. ],
  212. [/\./],
  213. [
  214. /@identifier/,
  215. {
  216. cases: {
  217. '@constants': 'keyword.liquid',
  218. '@builtinFilters': 'predefined.liquid',
  219. '@builtinTags': 'predefined.liquid',
  220. '@default': 'variable.liquid'
  221. }
  222. }
  223. ],
  224. [/[^}|%]/, 'variable.liquid']
  225. ]
  226. }
  227. };