UNPKG

docutils-ts

Version:

Port of the Python Docutils library to TypeScript

72 lines (71 loc) 2.79 kB
import Body from './body.js'; import { EOFError } from '../../../exceptions.js'; import * as nodes from '../../../nodes.js'; import * as RegExps from '../regExps.js'; /** * Parser for the contents of a substitution_definition element. */ class SubstitutionDef extends Body { constructor() { super(...arguments); this.initialTransitions = ['embedded_directive', 'text']; this.patterns = { embedded_directive: new RegExp(`(${RegExps.simplename})::( +|$)`), text: new RegExp(''), }; } ; /** Return a list of nodes. */ literal_block(match, context, nextState) { const [indented, indent, offset, blankFinish] = this.rstStateMachine.getIndented({}); while (indented && indented.length && !indented[indented.length - 1].trim()) { indented.trimEnd(); } if (!indented || !indented.length) { return this.quoted_literal_block(); } const data = indented.join('\n'); const literalBlock = new nodes.literal_block(data, data); const [source, line] = this.rstStateMachine.getSourceAndLine(offset + 1); if (source !== undefined) { literalBlock.source = source; } if (line !== undefined) { literalBlock.line = line; } const nodelist = [literalBlock]; if (!blankFinish) { nodelist.push(this.unindentWarning('Literal block')); } return nodelist; } quoted_literal_block() { const absLineOffset = this.rstStateMachine.absLineOffset(); const offset = this.rstStateMachine.lineOffset; const parentNode = new nodes.Element(); const newAbsOffset = this.nestedParse(this.rstStateMachine.inputLines.slice(offset), absLineOffset, parentNode, false); // stateMachineKwargs: { // stateFactory: this.rstStateMachine.stateFactory!.withStateClasses(['QuotedLiteralBlock']), // initialState: 'QuotedLiteralBlock', // }, this.gotoLine(newAbsOffset); return parentNode.getChildren(); } embedded_directive(match, context, nextState) { const [nodelist, blankFinish] = this.directive(match.result, { alt: this.parent.attributes.names[0] }); this.parent.add(nodelist); if (!this.rstStateMachine.atEof()) { this.blankFinish = blankFinish; } throw new EOFError(); } text(match, context, nextState) { if (!this.rstStateMachine.atEof()) { this.blankFinish = this.rstStateMachine.isNextLineBlank(); } throw new EOFError(); } } SubstitutionDef.stateName = 'SubstitutionDef'; //SubstitutionDef.constructor.stateName = 'SubstitutionDef'; export default SubstitutionDef;