| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = exports.Chomp = void 0;
-
- var _constants = require("../constants");
-
- var _Node = _interopRequireDefault(require("./Node"));
-
- var _Range = _interopRequireDefault(require("./Range"));
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- const Chomp = {
- CLIP: 'CLIP',
- KEEP: 'KEEP',
- STRIP: 'STRIP'
- };
- exports.Chomp = Chomp;
-
- class BlockValue extends _Node.default {
- constructor(type, props) {
- super(type, props);
- this.blockIndent = null;
- this.chomping = Chomp.CLIP;
- this.header = null;
- }
-
- get includesTrailingLines() {
- return this.chomping === Chomp.KEEP;
- }
-
- get strValue() {
- if (!this.valueRange || !this.context) return null;
- let {
- start,
- end
- } = this.valueRange;
- const {
- indent,
- src
- } = this.context;
- if (this.valueRange.isEmpty()) return '';
- let lastNewLine = null;
- let ch = src[end - 1];
-
- while (ch === '\n' || ch === '\t' || ch === ' ') {
- end -= 1;
-
- if (end <= start) {
- if (this.chomping === Chomp.KEEP) break;else return '';
- }
-
- if (ch === '\n') lastNewLine = end;
- ch = src[end - 1];
- }
-
- let keepStart = end + 1;
-
- if (lastNewLine) {
- if (this.chomping === Chomp.KEEP) {
- keepStart = lastNewLine;
- end = this.valueRange.end;
- } else {
- end = lastNewLine;
- }
- }
-
- const bi = indent + this.blockIndent;
- const folded = this.type === _constants.Type.BLOCK_FOLDED;
- let atStart = true;
- let str = '';
- let sep = '';
- let prevMoreIndented = false;
-
- for (let i = start; i < end; ++i) {
- for (let j = 0; j < bi; ++j) {
- if (src[i] !== ' ') break;
- i += 1;
- }
-
- const ch = src[i];
-
- if (ch === '\n') {
- if (sep === '\n') str += '\n';else sep = '\n';
- } else {
- const lineEnd = _Node.default.endOfLine(src, i);
-
- const line = src.slice(i, lineEnd);
- i = lineEnd;
-
- if (folded && (ch === ' ' || ch === '\t') && i < keepStart) {
- if (sep === ' ') sep = '\n';else if (!prevMoreIndented && !atStart && sep === '\n') sep = '\n\n';
- str += sep + line; //+ ((lineEnd < end && src[lineEnd]) || '')
-
- sep = lineEnd < end && src[lineEnd] || '';
- prevMoreIndented = true;
- } else {
- str += sep + line;
- sep = folded && i < keepStart ? ' ' : '\n';
- prevMoreIndented = false;
- }
-
- if (atStart && line !== '') atStart = false;
- }
- }
-
- return this.chomping === Chomp.STRIP ? str : str + '\n';
- }
-
- parseBlockHeader(start) {
- const {
- src
- } = this.context;
- let offset = start + 1;
- let bi = '';
-
- while (true) {
- const ch = src[offset];
-
- switch (ch) {
- case '-':
- this.chomping = Chomp.STRIP;
- break;
-
- case '+':
- this.chomping = Chomp.KEEP;
- break;
-
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- bi += ch;
- break;
-
- default:
- this.blockIndent = Number(bi) || null;
- this.header = new _Range.default(start, offset);
- return offset;
- }
-
- offset += 1;
- }
- }
-
- parseBlockValue(start) {
- const {
- indent,
- src
- } = this.context;
- let offset = start;
- let valueEnd = start;
- let bi = this.blockIndent ? indent + this.blockIndent - 1 : indent;
- let minBlockIndent = 1;
-
- for (let ch = src[offset]; ch === '\n'; ch = src[offset]) {
- offset += 1;
- if (_Node.default.atDocumentBoundary(src, offset)) break;
-
- const end = _Node.default.endOfBlockIndent(src, bi, offset); // should not include tab?
-
-
- if (end === null) break;
-
- if (!this.blockIndent) {
- // no explicit block indent, none yet detected
- const lineIndent = end - (offset + indent);
-
- if (src[end] !== '\n') {
- // first line with non-whitespace content
- if (lineIndent < minBlockIndent) {
- offset -= 1;
- break;
- }
-
- this.blockIndent = lineIndent;
- bi = indent + this.blockIndent - 1;
- } else if (lineIndent > minBlockIndent) {
- // empty line with more whitespace
- minBlockIndent = lineIndent;
- }
- }
-
- if (src[end] === '\n') {
- offset = end;
- } else {
- offset = valueEnd = _Node.default.endOfLine(src, end);
- }
- }
-
- if (this.chomping !== Chomp.KEEP) {
- offset = src[valueEnd] ? valueEnd + 1 : valueEnd;
- }
-
- this.valueRange = new _Range.default(start + 1, offset);
- return offset;
- }
- /**
- * Parses a block value from the source
- *
- * Accepted forms are:
- * ```
- * BS
- * block
- * lines
- *
- * BS #comment
- * block
- * lines
- * ```
- * where the block style BS matches the regexp `[|>][-+1-9]*` and block lines
- * are empty or have an indent level greater than `indent`.
- *
- * @param {ParseContext} context
- * @param {number} start - Index of first character
- * @returns {number} - Index of the character after this block
- */
-
-
- parse(context, start) {
- this.context = context;
- const {
- src
- } = context;
- let offset = this.parseBlockHeader(start);
- offset = _Node.default.endOfWhiteSpace(src, offset);
- offset = this.parseComment(offset);
- offset = this.parseBlockValue(offset);
- return offset;
- }
-
- setOrigRanges(cr, offset) {
- offset = super.setOrigRanges(cr, offset);
- return this.header ? this.header.setOrigRange(cr, offset) : offset;
- }
-
- }
-
- exports.default = BlockValue;
|