Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/runmode/runmode-standalone.js
1293 views
/* Just enough of CodeMirror to run runMode under node.js */12window.CodeMirror = {};34(function() {5"use strict";67function splitLines(string){ return string.split(/\r?\n|\r/); };89function StringStream(string) {10this.pos = this.start = 0;11this.string = string;12this.lineStart = 0;13}14StringStream.prototype = {15eol: function() {return this.pos >= this.string.length;},16sol: function() {return this.pos == 0;},17peek: function() {return this.string.charAt(this.pos) || null;},18next: function() {19if (this.pos < this.string.length)20return this.string.charAt(this.pos++);21},22eat: function(match) {23var ch = this.string.charAt(this.pos);24if (typeof match == "string") var ok = ch == match;25else var ok = ch && (match.test ? match.test(ch) : match(ch));26if (ok) {++this.pos; return ch;}27},28eatWhile: function(match) {29var start = this.pos;30while (this.eat(match)){}31return this.pos > start;32},33eatSpace: function() {34var start = this.pos;35while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;36return this.pos > start;37},38skipToEnd: function() {this.pos = this.string.length;},39skipTo: function(ch) {40var found = this.string.indexOf(ch, this.pos);41if (found > -1) {this.pos = found; return true;}42},43backUp: function(n) {this.pos -= n;},44column: function() {return this.start - this.lineStart;},45indentation: function() {return 0;},46match: function(pattern, consume, caseInsensitive) {47if (typeof pattern == "string") {48var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};49var substr = this.string.substr(this.pos, pattern.length);50if (cased(substr) == cased(pattern)) {51if (consume !== false) this.pos += pattern.length;52return true;53}54} else {55var match = this.string.slice(this.pos).match(pattern);56if (match && match.index > 0) return null;57if (match && consume !== false) this.pos += match[0].length;58return match;59}60},61current: function(){return this.string.slice(this.start, this.pos);},62hideFirstChars: function(n, inner) {63this.lineStart += n;64try { return inner(); }65finally { this.lineStart -= n; }66}67};68CodeMirror.StringStream = StringStream;6970CodeMirror.startState = function (mode, a1, a2) {71return mode.startState ? mode.startState(a1, a2) : true;72};7374var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};75CodeMirror.defineMode = function (name, mode) { modes[name] = mode; };76CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };77CodeMirror.resolveMode = function(spec) {78if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {79spec = mimeModes[spec];80} else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {81spec = mimeModes[spec.name];82}83if (typeof spec == "string") return {name: spec};84else return spec || {name: "null"};85};86CodeMirror.getMode = function (options, spec) {87spec = CodeMirror.resolveMode(spec);88var mfactory = modes[spec.name];89if (!mfactory) throw new Error("Unknown mode: " + spec);90return mfactory(options, spec);91};92CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;93CodeMirror.defineMode("null", function() {94return {token: function(stream) {stream.skipToEnd();}};95});96CodeMirror.defineMIME("text/plain", "null");9798CodeMirror.runMode = function (string, modespec, callback, options) {99var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);100101if (callback.nodeType == 1) {102var tabSize = (options && options.tabSize) || 4;103var node = callback, col = 0;104node.innerHTML = "";105callback = function (text, style) {106if (text == "\n") {107node.appendChild(document.createElement("br"));108col = 0;109return;110}111var content = "";112// replace tabs113for (var pos = 0; ;) {114var idx = text.indexOf("\t", pos);115if (idx == -1) {116content += text.slice(pos);117col += text.length - pos;118break;119} else {120col += idx - pos;121content += text.slice(pos, idx);122var size = tabSize - col % tabSize;123col += size;124for (var i = 0; i < size; ++i) content += " ";125pos = idx + 1;126}127}128129if (style) {130var sp = node.appendChild(document.createElement("span"));131sp.className = "cm-" + style.replace(/ +/g, " cm-");132sp.appendChild(document.createTextNode(content));133} else {134node.appendChild(document.createTextNode(content));135}136};137}138139var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);140for (var i = 0, e = lines.length; i < e; ++i) {141if (i) callback("\n");142var stream = new CodeMirror.StringStream(lines[i]);143while (!stream.eol()) {144var style = mode.token(stream, state);145callback(stream.current(), style, i, stream.start, state);146stream.start = stream.pos;147}148}149};150})();151152153