openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

runmode.node.js (7059B)


      1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
      2 // Distributed under an MIT license: http://codemirror.net/LICENSE
      3 
      4 /* Just enough of CodeMirror to run runMode under node.js */
      5 
      6 function splitLines(string){return string.split(/\r\n?|\n/);};
      7 
      8 // Counts the column offset in a string, taking tabs into account.
      9 // Used mostly to find indentation.
     10 var countColumn = exports.countColumn = function(string, end, tabSize, startIndex, startValue) {
     11   if (end == null) {
     12     end = string.search(/[^\s\u00a0]/);
     13     if (end == -1) end = string.length;
     14   }
     15   for (var i = startIndex || 0, n = startValue || 0;;) {
     16     var nextTab = string.indexOf("\t", i);
     17     if (nextTab < 0 || nextTab >= end)
     18       return n + (end - i);
     19     n += nextTab - i;
     20     n += tabSize - (n % tabSize);
     21     i = nextTab + 1;
     22   }
     23 };
     24 
     25 function StringStream(string, tabSize, context) {
     26   this.pos = this.start = 0;
     27   this.string = string;
     28   this.tabSize = tabSize || 8;
     29   this.lastColumnPos = this.lastColumnValue = 0;
     30   this.lineStart = 0;
     31   this.context = context
     32 };
     33 
     34 StringStream.prototype = {
     35   eol: function() {return this.pos >= this.string.length;},
     36   sol: function() {return this.pos == this.lineStart;},
     37   peek: function() {return this.string.charAt(this.pos) || undefined;},
     38   next: function() {
     39     if (this.pos < this.string.length)
     40       return this.string.charAt(this.pos++);
     41   },
     42   eat: function(match) {
     43     var ch = this.string.charAt(this.pos);
     44     if (typeof match == "string") var ok = ch == match;
     45     else var ok = ch && (match.test ? match.test(ch) : match(ch));
     46     if (ok) {++this.pos; return ch;}
     47   },
     48   eatWhile: function(match) {
     49     var start = this.pos;
     50     while (this.eat(match)){}
     51     return this.pos > start;
     52   },
     53   eatSpace: function() {
     54     var start = this.pos;
     55     while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
     56     return this.pos > start;
     57   },
     58   skipToEnd: function() {this.pos = this.string.length;},
     59   skipTo: function(ch) {
     60     var found = this.string.indexOf(ch, this.pos);
     61     if (found > -1) {this.pos = found; return true;}
     62   },
     63   backUp: function(n) {this.pos -= n;},
     64   column: function() {
     65     if (this.lastColumnPos < this.start) {
     66       this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue);
     67       this.lastColumnPos = this.start;
     68     }
     69     return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0);
     70   },
     71   indentation: function() {
     72     return countColumn(this.string, null, this.tabSize) -
     73       (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0);
     74   },
     75   match: function(pattern, consume, caseInsensitive) {
     76     if (typeof pattern == "string") {
     77       var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
     78       var substr = this.string.substr(this.pos, pattern.length);
     79       if (cased(substr) == cased(pattern)) {
     80         if (consume !== false) this.pos += pattern.length;
     81         return true;
     82       }
     83     } else {
     84       var match = this.string.slice(this.pos).match(pattern);
     85       if (match && match.index > 0) return null;
     86       if (match && consume !== false) this.pos += match[0].length;
     87       return match;
     88     }
     89   },
     90   current: function(){return this.string.slice(this.start, this.pos);},
     91   hideFirstChars: function(n, inner) {
     92     this.lineStart += n;
     93     try { return inner(); }
     94     finally { this.lineStart -= n; }
     95   },
     96   lookAhead: function(n) {
     97     var line = this.context.line + n
     98     return line >= this.context.lines.length ? null : this.context.lines[line]
     99   }
    100 };
    101 exports.StringStream = StringStream;
    102 
    103 exports.startState = function(mode, a1, a2) {
    104   return mode.startState ? mode.startState(a1, a2) : true;
    105 };
    106 
    107 var modes = exports.modes = {}, mimeModes = exports.mimeModes = {};
    108 exports.defineMode = function(name, mode) {
    109   if (arguments.length > 2)
    110     mode.dependencies = Array.prototype.slice.call(arguments, 2);
    111   modes[name] = mode;
    112 };
    113 exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };
    114 
    115 exports.defineMode("null", function() {
    116   return {token: function(stream) {stream.skipToEnd();}};
    117 });
    118 exports.defineMIME("text/plain", "null");
    119 
    120 exports.resolveMode = function(spec) {
    121   if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
    122     spec = mimeModes[spec];
    123   } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
    124     spec = mimeModes[spec.name];
    125   }
    126   if (typeof spec == "string") return {name: spec};
    127   else return spec || {name: "null"};
    128 };
    129 
    130 function copyObj(obj, target, overwrite) {
    131   if (!target) target = {};
    132   for (var prop in obj)
    133     if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop)))
    134       target[prop] = obj[prop];
    135   return target;
    136 }
    137 
    138 // This can be used to attach properties to mode objects from
    139 // outside the actual mode definition.
    140 var modeExtensions = exports.modeExtensions = {};
    141 exports.extendMode = function(mode, properties) {
    142   var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {});
    143   copyObj(properties, exts);
    144 };
    145 
    146 exports.getMode = function(options, spec) {
    147   var spec = exports.resolveMode(spec);
    148   var mfactory = modes[spec.name];
    149   if (!mfactory) return exports.getMode(options, "text/plain");
    150   var modeObj = mfactory(options, spec);
    151   if (modeExtensions.hasOwnProperty(spec.name)) {
    152     var exts = modeExtensions[spec.name];
    153     for (var prop in exts) {
    154       if (!exts.hasOwnProperty(prop)) continue;
    155       if (modeObj.hasOwnProperty(prop)) modeObj["_" + prop] = modeObj[prop];
    156       modeObj[prop] = exts[prop];
    157     }
    158   }
    159   modeObj.name = spec.name;
    160   if (spec.helperType) modeObj.helperType = spec.helperType;
    161   if (spec.modeProps) for (var prop in spec.modeProps)
    162     modeObj[prop] = spec.modeProps[prop];
    163 
    164   return modeObj;
    165 };
    166 
    167 exports.innerMode = function(mode, state) {
    168   var info;
    169   while (mode.innerMode) {
    170     info = mode.innerMode(state);
    171     if (!info || info.mode == mode) break;
    172     state = info.state;
    173     mode = info.mode;
    174   }
    175   return info || {mode: mode, state: state};
    176 }
    177 
    178 exports.registerHelper = exports.registerGlobalHelper = Math.min;
    179 
    180 exports.runMode = function(string, modespec, callback, options) {
    181   var mode = exports.getMode({indentUnit: 2}, modespec);
    182   var lines = splitLines(string), state = (options && options.state) || exports.startState(mode);
    183   var context = {lines: lines, line: 0}
    184   for (var i = 0, e = lines.length; i < e; ++i, ++context.line) {
    185     if (i) callback("\n");
    186     var stream = new exports.StringStream(lines[i], 4, context);
    187     if (!stream.string && mode.blankLine) mode.blankLine(state);
    188     while (!stream.eol()) {
    189       var style = mode.token(stream, state);
    190       callback(stream.current(), style, i, stream.start, state);
    191       stream.start = stream.pos;
    192     }
    193   }
    194 };
    195 
    196 require.cache[require.resolve("../../lib/codemirror")] = require.cache[require.resolve("./runmode.node")];
    197 require.cache[require.resolve("../../addon/runmode/runmode")] = require.cache[require.resolve("./runmode.node")];