state_block.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Parser state class
  2. 'use strict';
  3. var Token = require('../token');
  4. var isSpace = require('../common/utils').isSpace;
  5. function StateBlock(src, md, env, tokens) {
  6. var ch, s, start, pos, len, indent, offset, indent_found;
  7. this.src = src;
  8. // link to parser instance
  9. this.md = md;
  10. this.env = env;
  11. //
  12. // Internal state vartiables
  13. //
  14. this.tokens = tokens;
  15. this.bMarks = []; // line begin offsets for fast jumps
  16. this.eMarks = []; // line end offsets for fast jumps
  17. this.tShift = []; // offsets of the first non-space characters (tabs not expanded)
  18. this.sCount = []; // indents for each line (tabs expanded)
  19. // An amount of virtual spaces (tabs expanded) between beginning
  20. // of each line (bMarks) and real beginning of that line.
  21. //
  22. // It exists only as a hack because blockquotes override bMarks
  23. // losing information in the process.
  24. //
  25. // It's used only when expanding tabs, you can think about it as
  26. // an initial tab length, e.g. bsCount=21 applied to string `\t123`
  27. // means first tab should be expanded to 4-21%4 === 3 spaces.
  28. //
  29. this.bsCount = [];
  30. // block parser variables
  31. this.blkIndent = 0; // required block content indent
  32. // (for example, if we are in list)
  33. this.line = 0; // line index in src
  34. this.lineMax = 0; // lines count
  35. this.tight = false; // loose/tight mode for lists
  36. this.ddIndent = -1; // indent of the current dd block (-1 if there isn't any)
  37. // can be 'blockquote', 'list', 'root', 'paragraph' or 'reference'
  38. // used in lists to determine if they interrupt a paragraph
  39. this.parentType = 'root';
  40. this.level = 0;
  41. // renderer
  42. this.result = '';
  43. // Create caches
  44. // Generate markers.
  45. s = this.src;
  46. indent_found = false;
  47. for (start = pos = indent = offset = 0, len = s.length; pos < len; pos++) {
  48. ch = s.charCodeAt(pos);
  49. if (!indent_found) {
  50. if (isSpace(ch)) {
  51. indent++;
  52. if (ch === 0x09) {
  53. offset += 4 - offset % 4;
  54. } else {
  55. offset++;
  56. }
  57. continue;
  58. } else {
  59. indent_found = true;
  60. }
  61. }
  62. if (ch === 0x0A || pos === len - 1) {
  63. if (ch !== 0x0A) { pos++; }
  64. this.bMarks.push(start);
  65. this.eMarks.push(pos);
  66. this.tShift.push(indent);
  67. this.sCount.push(offset);
  68. this.bsCount.push(0);
  69. indent_found = false;
  70. indent = 0;
  71. offset = 0;
  72. start = pos + 1;
  73. }
  74. }
  75. // Push fake entry to simplify cache bounds checks
  76. this.bMarks.push(s.length);
  77. this.eMarks.push(s.length);
  78. this.tShift.push(0);
  79. this.sCount.push(0);
  80. this.bsCount.push(0);
  81. this.lineMax = this.bMarks.length - 1; // don't count last fake line
  82. }
  83. // Push new token to "stream".
  84. //
  85. StateBlock.prototype.push = function (type, tag, nesting) {
  86. var token = new Token(type, tag, nesting);
  87. token.block = true;
  88. if (nesting < 0) { this.level--; }
  89. token.level = this.level;
  90. if (nesting > 0) { this.level++; }
  91. this.tokens.push(token);
  92. return token;
  93. };
  94. StateBlock.prototype.isEmpty = function isEmpty(line) {
  95. return this.bMarks[line] + this.tShift[line] >= this.eMarks[line];
  96. };
  97. StateBlock.prototype.skipEmptyLines = function skipEmptyLines(from) {
  98. for (var max = this.lineMax; from < max; from++) {
  99. if (this.bMarks[from] + this.tShift[from] < this.eMarks[from]) {
  100. break;
  101. }
  102. }
  103. return from;
  104. };
  105. // Skip spaces from given position.
  106. StateBlock.prototype.skipSpaces = function skipSpaces(pos) {
  107. var ch;
  108. for (var max = this.src.length; pos < max; pos++) {
  109. ch = this.src.charCodeAt(pos);
  110. if (!isSpace(ch)) { break; }
  111. }
  112. return pos;
  113. };
  114. // Skip spaces from given position in reverse.
  115. StateBlock.prototype.skipSpacesBack = function skipSpacesBack(pos, min) {
  116. if (pos <= min) { return pos; }
  117. while (pos > min) {
  118. if (!isSpace(this.src.charCodeAt(--pos))) { return pos + 1; }
  119. }
  120. return pos;
  121. };
  122. // Skip char codes from given position
  123. StateBlock.prototype.skipChars = function skipChars(pos, code) {
  124. for (var max = this.src.length; pos < max; pos++) {
  125. if (this.src.charCodeAt(pos) !== code) { break; }
  126. }
  127. return pos;
  128. };
  129. // Skip char codes reverse from given position - 1
  130. StateBlock.prototype.skipCharsBack = function skipCharsBack(pos, code, min) {
  131. if (pos <= min) { return pos; }
  132. while (pos > min) {
  133. if (code !== this.src.charCodeAt(--pos)) { return pos + 1; }
  134. }
  135. return pos;
  136. };
  137. // cut lines range from source.
  138. StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF) {
  139. var i, lineIndent, ch, first, last, queue, lineStart,
  140. line = begin;
  141. if (begin >= end) {
  142. return '';
  143. }
  144. queue = new Array(end - begin);
  145. for (i = 0; line < end; line++, i++) {
  146. lineIndent = 0;
  147. lineStart = first = this.bMarks[line];
  148. if (line + 1 < end || keepLastLF) {
  149. // No need for bounds check because we have fake entry on tail.
  150. last = this.eMarks[line] + 1;
  151. } else {
  152. last = this.eMarks[line];
  153. }
  154. while (first < last && lineIndent < indent) {
  155. ch = this.src.charCodeAt(first);
  156. if (isSpace(ch)) {
  157. if (ch === 0x09) {
  158. lineIndent += 4 - (lineIndent + this.bsCount[line]) % 4;
  159. } else {
  160. lineIndent++;
  161. }
  162. } else if (first - lineStart < this.tShift[line]) {
  163. // patched tShift masked characters to look like spaces (blockquotes, list markers)
  164. lineIndent++;
  165. } else {
  166. break;
  167. }
  168. first++;
  169. }
  170. if (lineIndent > indent) {
  171. // partially expanding tabs in code blocks, e.g '\t\tfoobar'
  172. // with indent=2 becomes ' \tfoobar'
  173. queue[i] = new Array(lineIndent - indent + 1).join(' ') + this.src.slice(first, last);
  174. } else {
  175. queue[i] = this.src.slice(first, last);
  176. }
  177. }
  178. return queue.join('');
  179. };
  180. // re-export Token class to use in block rules
  181. StateBlock.prototype.Token = Token;
  182. module.exports = StateBlock;