UNPKG

course-renderer

Version:

Manages CA School Courses file system storage and HTML conversion

130 lines (129 loc) 3.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); class Reader { constructor(src) { this.source = src.replace(/\r\n/g, "\n"); this.length = this.source.length; this.currentLine = 0; // Initial line position for reading this.parseSource(); } parseSource() { this.startLinePos = []; this.endLinePos = []; let start, pos, len; for (start = pos = 0, len = this.length; pos < len; pos++) { let ch = this.getCharCode(pos); if (ch === 0x0A || pos === len - 1) { if (ch !== 0x0A) pos++; this.startLinePos.push(start); this.endLinePos.push(pos); start = pos + 1; } } this.maxLine = this.startLinePos.length; } getCharCode(pos = this.getCurrentLineStartPos()) { return this.source.charCodeAt(pos); } getCurrentLineStartPos() { return this.getLineStartPos(); } getLineStartPos(line = this.currentLine) { return this.startLinePos[line]; } getCurrentLineEndPos() { return this.getLineEndPos(); } getLineEndPos(line = this.currentLine) { return this.endLinePos[line]; } getLine(line) { return this.source.slice(this.getLineStartPos(line), this.getLineEndPos(line)); } getLines(start, end) { let queue = []; if (start === end) { return this.getLine(start); } while (start <= end) { queue.push(this.getLine(start)); start++; } return queue.join('\n'); } // Moves the position to the next line. Needs to REDO nextLine() { this.currentLine++; } isEnd() { return this.currentLine >= this.maxLine; } isEmpty(line) { let pos; let max = this.getLineEndPos(line); let isEmpty = true; for (pos = this.getLineStartPos(line); pos <= max; pos++) { if (!utils_1.isSpace(this.getCharCode(pos)) && !utils_1.isLineFeed(this.getCharCode(pos))) { isEmpty = false; break; } } return isEmpty; } nextNonEmptyLines() { this.nextLine(); this.skipEmptyLines(); } jumpBeforeEmptyLines(line = this.currentLine) { while (line < this.maxLine) { if (!this.isEmpty(line)) break; line++; } return line; } skipEmptyChars(startPos = this.getCurrentLineStartPos()) { let pos; for (pos = startPos; pos < this.length; pos++) { if (!utils_1.isWhiteSpace(this.getCharCode(pos))) break; } return pos; } // Skip empty lines from the current pos skipEmptyLines() { let pos; let currentLine; let exit = false; for (currentLine = this.currentLine; currentLine < this.maxLine; currentLine++) { for (pos = this.startLinePos[currentLine]; pos <= this.endLinePos[currentLine]; pos++) { let ch = this.getCharCode(pos); if (!utils_1.isLineFeed(ch) && !utils_1.isSpace(this.getCharCode(pos))) { this.currentLine = currentLine; exit = true; break; } } if (exit) break; } } skipSpaces(pos) { for (let max = this.length; pos < max; pos++) { let ch = this.getCharCode(pos); if (!utils_1.isSpace(ch)) break; } return pos; } skipChars(pos = this.getCurrentLineStartPos(), code) { for (const max = this.length; pos < max; pos++) { if (this.getCharCode(pos) !== code) break; } return pos; } } exports.default = Reader;