本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.2KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. window.CodeMirror = {};
  4. (function() {
  5. "use strict";
  6. function splitLines(string){ return string.split(/\r?\n|\r/); };
  7. function StringStream(string) {
  8. this.pos = this.start = 0;
  9. this.string = string;
  10. this.lineStart = 0;
  11. }
  12. StringStream.prototype = {
  13. eol: function() {return this.pos >= this.string.length;},
  14. sol: function() {return this.pos == 0;},
  15. peek: function() {return this.string.charAt(this.pos) || null;},
  16. next: function() {
  17. if (this.pos < this.string.length)
  18. return this.string.charAt(this.pos++);
  19. },
  20. eat: function(match) {
  21. var ch = this.string.charAt(this.pos);
  22. if (typeof match == "string") var ok = ch == match;
  23. else var ok = ch && (match.test ? match.test(ch) : match(ch));
  24. if (ok) {++this.pos; return ch;}
  25. },
  26. eatWhile: function(match) {
  27. var start = this.pos;
  28. while (this.eat(match)){}
  29. return this.pos > start;
  30. },
  31. eatSpace: function() {
  32. var start = this.pos;
  33. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
  34. return this.pos > start;
  35. },
  36. skipToEnd: function() {this.pos = this.string.length;},
  37. skipTo: function(ch) {
  38. var found = this.string.indexOf(ch, this.pos);
  39. if (found > -1) {this.pos = found; return true;}
  40. },
  41. backUp: function(n) {this.pos -= n;},
  42. column: function() {return this.start - this.lineStart;},
  43. indentation: function() {return 0;},
  44. match: function(pattern, consume, caseInsensitive) {
  45. if (typeof pattern == "string") {
  46. var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
  47. var substr = this.string.substr(this.pos, pattern.length);
  48. if (cased(substr) == cased(pattern)) {
  49. if (consume !== false) this.pos += pattern.length;
  50. return true;
  51. }
  52. } else {
  53. var match = this.string.slice(this.pos).match(pattern);
  54. if (match && match.index > 0) return null;
  55. if (match && consume !== false) this.pos += match[0].length;
  56. return match;
  57. }
  58. },
  59. current: function(){return this.string.slice(this.start, this.pos);},
  60. hideFirstChars: function(n, inner) {
  61. this.lineStart += n;
  62. try { return inner(); }
  63. finally { this.lineStart -= n; }
  64. },
  65. lookAhead: function() { return null }
  66. };
  67. CodeMirror.StringStream = StringStream;
  68. CodeMirror.startState = function (mode, a1, a2) {
  69. return mode.startState ? mode.startState(a1, a2) : true;
  70. };
  71. var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
  72. CodeMirror.defineMode = function (name, mode) {
  73. if (arguments.length > 2)
  74. mode.dependencies = Array.prototype.slice.call(arguments, 2);
  75. modes[name] = mode;
  76. };
  77. CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
  78. CodeMirror.resolveMode = function(spec) {
  79. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
  80. spec = mimeModes[spec];
  81. } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
  82. spec = mimeModes[spec.name];
  83. }
  84. if (typeof spec == "string") return {name: spec};
  85. else return spec || {name: "null"};
  86. };
  87. CodeMirror.getMode = function (options, spec) {
  88. spec = CodeMirror.resolveMode(spec);
  89. var mfactory = modes[spec.name];
  90. if (!mfactory) throw new Error("Unknown mode: " + spec);
  91. return mfactory(options, spec);
  92. };
  93. CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;
  94. CodeMirror.defineMode("null", function() {
  95. return {token: function(stream) {stream.skipToEnd();}};
  96. });
  97. CodeMirror.defineMIME("text/plain", "null");
  98. CodeMirror.runMode = function (string, modespec, callback, options) {
  99. var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
  100. if (callback.nodeType == 1) {
  101. var tabSize = (options && options.tabSize) || 4;
  102. var node = callback, col = 0;
  103. node.innerHTML = "";
  104. callback = function (text, style) {
  105. if (text == "\n") {
  106. node.appendChild(document.createElement("br"));
  107. col = 0;
  108. return;
  109. }
  110. var content = "";
  111. // replace tabs
  112. for (var pos = 0; ;) {
  113. var idx = text.indexOf("\t", pos);
  114. if (idx == -1) {
  115. content += text.slice(pos);
  116. col += text.length - pos;
  117. break;
  118. } else {
  119. col += idx - pos;
  120. content += text.slice(pos, idx);
  121. var size = tabSize - col % tabSize;
  122. col += size;
  123. for (var i = 0; i < size; ++i) content += " ";
  124. pos = idx + 1;
  125. }
  126. }
  127. if (style) {
  128. var sp = node.appendChild(document.createElement("span"));
  129. sp.className = "cm-" + style.replace(/ +/g, " cm-");
  130. sp.appendChild(document.createTextNode(content));
  131. } else {
  132. node.appendChild(document.createTextNode(content));
  133. }
  134. };
  135. }
  136. var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
  137. for (var i = 0, e = lines.length; i < e; ++i) {
  138. if (i) callback("\n");
  139. var stream = new CodeMirror.StringStream(lines[i]);
  140. if (!stream.string && mode.blankLine) mode.blankLine(state);
  141. while (!stream.eol()) {
  142. var style = mode.token(stream, state);
  143. callback(stream.current(), style, i, stream.start, state);
  144. stream.start = stream.pos;
  145. }
  146. }
  147. };
  148. })();
上海开阖软件有限公司 沪ICP备12045867号-1