liquid_highlight_rules.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  34. var HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
  35. var LiquidHighlightRules = function() {
  36. HtmlHighlightRules.call(this);
  37. // see: https://developer.mozilla.org/en/Liquid/Reference/Global_Objects
  38. var functions = (
  39. // Standard Filters
  40. "date|capitalize|downcase|upcase|first|last|join|sort|map|size|escape|" +
  41. "escape_once|strip_html|strip_newlines|newline_to_br|replace|replace_first|" +
  42. "truncate|truncatewords|prepend|append|minus|plus|times|divided_by|split"
  43. );
  44. var keywords = (
  45. // Standard Tags
  46. "capture|endcapture|case|endcase|when|comment|endcomment|" +
  47. "cycle|for|endfor|in|reversed|if|endif|else|elsif|include|endinclude|unless|endunless|" +
  48. // Commonly used tags
  49. "style|text|image|widget|plugin|marker|endmarker|tablerow|endtablerow"
  50. );
  51. var builtinVariables = 'forloop|tablerowloop';
  52. // "forloop\\.(length|index|index0|rindex|rindex0|first|last)|limit|offset|range" +
  53. // "tablerowloop\\.(length|index|index0|rindex|rindex0|first|last|col|col0|"+
  54. // "col_first|col_last)"
  55. var definitions = ("assign");
  56. var keywordMapper = this.createKeywordMapper({
  57. "variable.language": builtinVariables,
  58. "keyword": keywords,
  59. "support.function": functions,
  60. "keyword.definition": definitions
  61. }, "identifier");
  62. // add liquid start tags to the HTML start tags
  63. for (var rule in this.$rules) {
  64. this.$rules[rule].unshift({
  65. token : "variable",
  66. regex : "{%",
  67. push : "liquid-start"
  68. }, {
  69. token : "variable",
  70. regex : "{{",
  71. push : "liquid-start"
  72. });
  73. }
  74. this.addRules({
  75. "liquid-start" : [{
  76. token: "variable",
  77. regex: "}}",
  78. next: "pop"
  79. }, {
  80. token: "variable",
  81. regex: "%}",
  82. next: "pop"
  83. }, {
  84. token : "string", // single line
  85. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  86. }, {
  87. token : "string", // single line
  88. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  89. }, {
  90. token : "constant.numeric", // hex
  91. regex : "0[xX][0-9a-fA-F]+\\b"
  92. }, {
  93. token : "constant.numeric", // float
  94. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  95. }, {
  96. token : "constant.language.boolean",
  97. regex : "(?:true|false)\\b"
  98. }, {
  99. token : keywordMapper,
  100. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  101. }, {
  102. token : "keyword.operator",
  103. regex : "\/|\\*|\\-|\\+|=|!=|\\?\\:"
  104. }, {
  105. token : "paren.lparen",
  106. regex : /[\[\({]/
  107. }, {
  108. token : "paren.rparen",
  109. regex : /[\])}]/
  110. }, {
  111. token : "text",
  112. regex : "\\s+"
  113. }]
  114. });
  115. this.normalizeRules();
  116. };
  117. oop.inherits(LiquidHighlightRules, TextHighlightRules);
  118. exports.LiquidHighlightRules = LiquidHighlightRules;
  119. });