text_highlight_rules.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 lang = require("../lib/lang");
  33. var TextHighlightRules = function() {
  34. // regexp must not have capturing parentheses
  35. // regexps are ordered -> the first match is used
  36. this.$rules = {
  37. "start" : [{
  38. token : "empty_line",
  39. regex : '^$'
  40. }, {
  41. defaultToken : "text"
  42. }]
  43. };
  44. };
  45. (function() {
  46. this.addRules = function(rules, prefix) {
  47. if (!prefix) {
  48. for (var key in rules)
  49. this.$rules[key] = rules[key];
  50. return;
  51. }
  52. for (var key in rules) {
  53. var state = rules[key];
  54. for (var i = 0; i < state.length; i++) {
  55. var rule = state[i];
  56. if (rule.next) {
  57. if (typeof rule.next != "string") {
  58. if (rule.nextState && rule.nextState.indexOf(prefix) !== 0)
  59. rule.nextState = prefix + rule.nextState;
  60. } else {
  61. if (rule.next.indexOf(prefix) !== 0)
  62. rule.next = prefix + rule.next;
  63. }
  64. }
  65. }
  66. this.$rules[prefix + key] = state;
  67. }
  68. };
  69. this.getRules = function() {
  70. return this.$rules;
  71. };
  72. this.embedRules = function (HighlightRules, prefix, escapeRules, states, append) {
  73. var embedRules = new HighlightRules().getRules();
  74. if (states) {
  75. for (var i = 0; i < states.length; i++)
  76. states[i] = prefix + states[i];
  77. } else {
  78. states = [];
  79. for (var key in embedRules)
  80. states.push(prefix + key);
  81. }
  82. this.addRules(embedRules, prefix);
  83. if (escapeRules) {
  84. var addRules = Array.prototype[append ? "push" : "unshift"];
  85. for (var i = 0; i < states.length; i++)
  86. addRules.apply(this.$rules[states[i]], lang.deepCopy(escapeRules));
  87. }
  88. if (!this.$embeds)
  89. this.$embeds = [];
  90. this.$embeds.push(prefix);
  91. };
  92. this.getEmbeds = function() {
  93. return this.$embeds;
  94. };
  95. var pushState = function(currentState, stack) {
  96. if (currentState != "start")
  97. stack.unshift(this.nextState, currentState);
  98. return this.nextState;
  99. };
  100. var popState = function(currentState, stack) {
  101. if (stack[0] !== currentState)
  102. return "start";
  103. stack.shift();
  104. return stack.shift();
  105. };
  106. this.normalizeRules = function() {
  107. var id = 0;
  108. var rules = this.$rules;
  109. function processState(key) {
  110. var state = rules[key];
  111. state.processed = true;
  112. for (var i = 0; i < state.length; i++) {
  113. var rule = state[i];
  114. if (!rule.regex && rule.start) {
  115. rule.regex = rule.start;
  116. if (!rule.next)
  117. rule.next = [];
  118. rule.next.push({
  119. defaultToken: rule.token
  120. }, {
  121. token: rule.token + ".end",
  122. regex: rule.end || rule.start,
  123. next: "pop"
  124. });
  125. rule.token = rule.token + ".start";
  126. rule.push = true;
  127. }
  128. var next = rule.next || rule.push;
  129. if (next && Array.isArray(next)) {
  130. var stateName = rule.stateName;
  131. if (!stateName) {
  132. stateName = rule.token;
  133. if (typeof stateName != "string")
  134. stateName = stateName[0] || "";
  135. if (rules[stateName])
  136. stateName += id++;
  137. }
  138. rules[stateName] = next;
  139. rule.next = stateName;
  140. processState(stateName);
  141. } else if (next == "pop") {
  142. rule.next = popState;
  143. }
  144. if (rule.push) {
  145. rule.nextState = rule.next || rule.push;
  146. rule.next = pushState;
  147. delete rule.push;
  148. }
  149. if (rule.rules) {
  150. for (var r in rule.rules) {
  151. if (rules[r]) {
  152. if (rules[r].push)
  153. rules[r].push.apply(rules[r], rule.rules[r]);
  154. } else {
  155. rules[r] = rule.rules[r];
  156. }
  157. }
  158. }
  159. if (rule.include || typeof rule == "string") {
  160. var includeName = rule.include || rule;
  161. var toInsert = rules[includeName];
  162. } else if (Array.isArray(rule))
  163. toInsert = rule;
  164. if (toInsert) {
  165. var args = [i, 1].concat(toInsert);
  166. if (rule.noEscape)
  167. args = args.filter(function(x) {return !x.next;});
  168. state.splice.apply(state, args);
  169. // skip included rules since they are already processed
  170. //i += args.length - 3;
  171. i--;
  172. toInsert = null
  173. }
  174. if (rule.keywordMap) {
  175. rule.token = this.createKeywordMapper(
  176. rule.keywordMap, rule.defaultToken || "text", rule.caseInsensitive
  177. );
  178. delete rule.defaultToken;
  179. }
  180. }
  181. };
  182. Object.keys(rules).forEach(processState, this);
  183. };
  184. this.createKeywordMapper = function(map, defaultToken, ignoreCase, splitChar) {
  185. var keywords = Object.create(null);
  186. Object.keys(map).forEach(function(className) {
  187. var a = map[className];
  188. if (ignoreCase)
  189. a = a.toLowerCase();
  190. var list = a.split(splitChar || "|");
  191. for (var i = list.length; i--; )
  192. keywords[list[i]] = className;
  193. });
  194. // in old versions of opera keywords["__proto__"] sets prototype
  195. // even on objects with __proto__=null
  196. if (Object.getPrototypeOf(keywords)) {
  197. keywords.__proto__ = null;
  198. }
  199. this.$keywordList = Object.keys(keywords);
  200. map = null;
  201. return ignoreCase
  202. ? function(value) {return keywords[value.toLowerCase()] || defaultToken }
  203. : function(value) {return keywords[value] || defaultToken };
  204. }
  205. this.getKeywords = function() {
  206. return this.$keywords;
  207. };
  208. }).call(TextHighlightRules.prototype);
  209. exports.TextHighlightRules = TextHighlightRules;
  210. });