UNPKG

course-renderer

Version:

Manages CA School Courses file system storage and HTML conversion

60 lines (40 loc) 1.67 kB
import Reader from '../reader' import Token from '../Token' import { isSpace } from '../utils' import { HandlerOptionInterface } from '../handlers' export function option(reader: Reader, tokens: Token[], silent: boolean = false, options: HandlerOptionInterface): boolean { if (!hasMarker(reader)) return false if (silent) return true const start = reader.currentLine const initialPos = reader.getCurrentLineStartPos() const endPos = skipMarker(reader) const endLine = reader.jumpBeforeEmptyLines() let optionLines = reader.getLines(start, endLine) optionLines = optionLines.substring(endPos - initialPos) const token = tokens[tokens.length - 1] if (options.sectionMode && options.replMode) { const replToken = token.children[token.children.length - 1] const lastToken = replToken.children[replToken.children.length - 1] lastToken.addChoice(optionLines) } else if (options.sectionMode || options.replMode) { const childToken = token.children[token.children.length - 1] childToken.addChoice(optionLines) } else { token.addChoice(optionLines) } reader.currentLine = endLine } function hasMarker(reader: Reader): boolean { let pos = reader.skipEmptyChars() let max = reader.getCurrentLineEndPos() if (reader.getCharCode(pos++) !== 0x2D /* - */) return false if (!isSpace(reader.getCharCode(pos++))) return false pos = reader.skipSpaces(pos) if ( pos === max) return false return true } function skipMarker(reader: Reader) { let pos = reader.skipEmptyChars() + 1 pos = reader.skipSpaces(pos) return pos }