runmode-standalone.js 5.1 KB

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