docutils-ts
Version:
Port of the Python Docutils library to TypeScript
67 lines (66 loc) • 2.27 kB
JavaScript
import StateMachineWS from "../../stateMachineWS.js";
import Inliner from "./inliner.js";
import { getLanguage } from "./languages.js";
/**
* reStructuredText's master StateMachine.
*
* The entry point to reStructuredText parsing is the `run()` method.
*/
class RSTStateMachine extends StateMachineWS {
run(inputLines, inputOffset, runContext, inputSource, initialState, document, matchTitles = true, inliner) {
if (document === undefined) {
throw new Error('need document');
}
this.document = document;
// @ts-ignore
this.inputLines = inputLines;
this.inputOffset = inputOffset;
try {
if (document.settings === undefined) {
throw new Error('unexpected');
}
const languageCode = document.settings.languageCode;
if (languageCode !== undefined) {
this.rstLanguage = getLanguage(languageCode);
}
}
catch (error) {
console.log(error);
throw error;
}
this.matchTitles = matchTitles;
/* istanbul ignore next */
if (inliner === undefined) {
this.logger.silly('creating new inliner');
this.inliner = new Inliner(document, this.logger);
}
else {
this.inliner = inliner;
}
if (this.inliner !== undefined) {
this.inliner.initCustomizations(document.settings);
}
this.memo = {
document,
reporter: document.reporter,
language: this.rstLanguage,
titleStyles: [],
sectionLevel: 0,
sectionBubbleUpKludge: false,
inliner: this.inliner,
};
this.document = document;
this.attachObserver(document.noteSource.bind(document));
this.reporter = this.memo.reporter;
this.node = document;
const results = super.run(inputLines, inputOffset);
/* istanbul ignore if */
if (results.length !== 0) {
throw new Error('should be empty array return from statemachine.run');
}
this.node = undefined;
this.memo = undefined;
return [];
}
}
export default RSTStateMachine;