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