runmode-standalone.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Just enough of CodeMirror to run runMode under node.js */
  2. window.CodeMirror = {};
  3. function splitLines(string){ return string.split(/\r?\n|\r/); };
  4. function StringStream(string) {
  5. this.pos = this.start = 0;
  6. this.string = string;
  7. }
  8. StringStream.prototype = {
  9. eol: function() {return this.pos >= this.string.length;},
  10. sol: function() {return this.pos == 0;},
  11. peek: function() {return this.string.charAt(this.pos) || null;},
  12. next: function() {
  13. if (this.pos < this.string.length)
  14. return this.string.charAt(this.pos++);
  15. },
  16. eat: function(match) {
  17. var ch = this.string.charAt(this.pos);
  18. if (typeof match == "string") var ok = ch == match;
  19. else var ok = ch && (match.test ? match.test(ch) : match(ch));
  20. if (ok) {++this.pos; return ch;}
  21. },
  22. eatWhile: function(match) {
  23. var start = this.pos;
  24. while (this.eat(match)){}
  25. return this.pos > start;
  26. },
  27. eatSpace: function() {
  28. var start = this.pos;
  29. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
  30. return this.pos > start;
  31. },
  32. skipToEnd: function() {this.pos = this.string.length;},
  33. skipTo: function(ch) {
  34. var found = this.string.indexOf(ch, this.pos);
  35. if (found > -1) {this.pos = found; return true;}
  36. },
  37. backUp: function(n) {this.pos -= n;},
  38. column: function() {return this.start;},
  39. indentation: function() {return 0;},
  40. match: function(pattern, consume, caseInsensitive) {
  41. if (typeof pattern == "string") {
  42. var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
  43. if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
  44. if (consume !== false) this.pos += pattern.length;
  45. return true;
  46. }
  47. } else {
  48. var match = this.string.slice(this.pos).match(pattern);
  49. if (match && consume !== false) this.pos += match[0].length;
  50. return match;
  51. }
  52. },
  53. current: function(){return this.string.slice(this.start, this.pos);}
  54. };
  55. CodeMirror.StringStream = StringStream;
  56. CodeMirror.startState = function (mode, a1, a2) {
  57. return mode.startState ? mode.startState(a1, a2) : true;
  58. };
  59. var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
  60. CodeMirror.defineMode = function (name, mode) { modes[name] = mode; };
  61. CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
  62. CodeMirror.getMode = function (options, spec) {
  63. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
  64. spec = mimeModes[spec];
  65. if (typeof spec == "string")
  66. var mname = spec, config = {};
  67. else if (spec != null)
  68. var mname = spec.name, config = spec;
  69. var mfactory = modes[mname];
  70. if (!mfactory) throw new Error("Unknown mode: " + spec);
  71. return mfactory(options, config || {});
  72. };
  73. CodeMirror.runMode = function (string, modespec, callback, options) {
  74. var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
  75. if (callback.nodeType == 1) {
  76. var tabSize = (options && options.tabSize) || 4;
  77. var node = callback, col = 0;
  78. node.innerHTML = "";
  79. callback = function (text, style) {
  80. if (text == "\n") {
  81. node.appendChild(document.createElement("br"));
  82. col = 0;
  83. return;
  84. }
  85. var content = "";
  86. // replace tabs
  87. for (var pos = 0; ;) {
  88. var idx = text.indexOf("\t", pos);
  89. if (idx == -1) {
  90. content += text.slice(pos);
  91. col += text.length - pos;
  92. break;
  93. } else {
  94. col += idx - pos;
  95. content += text.slice(pos, idx);
  96. var size = tabSize - col % tabSize;
  97. col += size;
  98. for (var i = 0; i < size; ++i) content += " ";
  99. pos = idx + 1;
  100. }
  101. }
  102. if (style) {
  103. var sp = node.appendChild(document.createElement("span"));
  104. sp.className = "cm-" + style.replace(/ +/g, " cm-");
  105. sp.appendChild(document.createTextNode(content));
  106. } else {
  107. node.appendChild(document.createTextNode(content));
  108. }
  109. };
  110. }
  111. var lines = splitLines(string), state = CodeMirror.startState(mode);
  112. for (var i = 0, e = lines.length; i < e; ++i) {
  113. if (i) callback("\n");
  114. var stream = new CodeMirror.StringStream(lines[i]);
  115. while (!stream.eol()) {
  116. var style = mode.token(stream, state);
  117. callback(stream.current(), style, i, stream.start);
  118. stream.start = stream.pos;
  119. }
  120. }
  121. };