runmode-standalone.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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. };
  66. CodeMirror.StringStream = StringStream;
  67. CodeMirror.startState = function (mode, a1, a2) {
  68. return mode.startState ? mode.startState(a1, a2) : true;
  69. };
  70. var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
  71. CodeMirror.defineMode = function (name, mode) {
  72. if (arguments.length > 2)
  73. mode.dependencies = Array.prototype.slice.call(arguments, 2);
  74. modes[name] = mode;
  75. };
  76. CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
  77. CodeMirror.resolveMode = function(spec) {
  78. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
  79. spec = mimeModes[spec];
  80. } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
  81. spec = mimeModes[spec.name];
  82. }
  83. if (typeof spec == "string") return {name: spec};
  84. else return spec || {name: "null"};
  85. };
  86. CodeMirror.getMode = function (options, spec) {
  87. spec = CodeMirror.resolveMode(spec);
  88. var mfactory = modes[spec.name];
  89. if (!mfactory) throw new Error("Unknown mode: " + spec);
  90. return mfactory(options, spec);
  91. };
  92. CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;
  93. CodeMirror.defineMode("null", function() {
  94. return {token: function(stream) {stream.skipToEnd();}};
  95. });
  96. CodeMirror.defineMIME("text/plain", "null");
  97. CodeMirror.runMode = function (string, modespec, callback, options) {
  98. var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
  99. if (callback.nodeType == 1) {
  100. var tabSize = (options && options.tabSize) || 4;
  101. var node = callback, col = 0;
  102. node.innerHTML = "";
  103. callback = function (text, style) {
  104. if (text == "\n") {
  105. node.appendChild(document.createElement("br"));
  106. col = 0;
  107. return;
  108. }
  109. var content = "";
  110. // replace tabs
  111. for (var pos = 0; ;) {
  112. var idx = text.indexOf("\t", pos);
  113. if (idx == -1) {
  114. content += text.slice(pos);
  115. col += text.length - pos;
  116. break;
  117. } else {
  118. col += idx - pos;
  119. content += text.slice(pos, idx);
  120. var size = tabSize - col % tabSize;
  121. col += size;
  122. for (var i = 0; i < size; ++i) content += " ";
  123. pos = idx + 1;
  124. }
  125. }
  126. if (style) {
  127. var sp = node.appendChild(document.createElement("span"));
  128. sp.className = "cm-" + style.replace(/ +/g, " cm-");
  129. sp.appendChild(document.createTextNode(content));
  130. } else {
  131. node.appendChild(document.createTextNode(content));
  132. }
  133. };
  134. }
  135. var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
  136. for (var i = 0, e = lines.length; i < e; ++i) {
  137. if (i) callback("\n");
  138. var stream = new CodeMirror.StringStream(lines[i]);
  139. if (!stream.string && mode.blankLine) mode.blankLine(state);
  140. while (!stream.eol()) {
  141. var style = mode.token(stream, state);
  142. callback(stream.current(), style, i, stream.start, state);
  143. stream.start = stream.pos;
  144. }
  145. }
  146. };
  147. })();