runmode-standalone.js 4.4 KB

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