docutils-ts
Version:
Port of the Python Docutils library to TypeScript
86 lines (84 loc) • 2.51 kB
JavaScript
import BaseWriter from '../writer.js';
import { GenericNodeVisitor } from '../nodes.js';
import { InvalidStateError } from "../exceptions.js";
const __version__ = '';
/**
* Translator class for Pojo writer
*/
class POJOTranslator extends GenericNodeVisitor {
/**
* Create a POJOTranslator
* @param {nodes.document} document - the document to translate
*/
constructor(document) {
super(document);
this.ancestors = [];
this.generator = `<!-- generated by Docutils ${__version__} -->\n`;
this.document = document;
if (!this.document.reporter) {
throw new Error('document has no reporter');
}
this.warn = this.document.reporter.warning.bind(this.document.reporter);
this.error = this.document.reporter.error.bind(this.document.reporter);
this.settings = document.settings;
this.level = 0;
this.inSimple = 0;
this.fixedText = 0;
this.output = {};
}
default_visit(node) {
if (node.attlist) {
const me = [node.tagname, node.attlist(), []];
this.ancestors.push(me);
this.level += 1;
}
}
default_departure(node) {
const me = this.ancestors.pop();
if (this.level === 1) {
this.root = me;
// console.log(JSON.stringify(me));
}
else {
const parent = this.ancestors[this.ancestors.length - 1];
parent[2].push(me);
}
this.level -= 1;
}
visit_Text(node) {
this.ancestors[this.ancestors.length - 1][2].push(node.astext());
// const text = escapeXml(node.astext())
// this.output.push(text);
}
depart_Text(node) {
}
}
/**
* Writer class for POJOWriter
*/
class POJOWriter extends BaseWriter {
constructor() {
super(...arguments);
this.translatorClass = POJOTranslator;
}
/**
* Translate the document to plain old javascript object
*/
translate() {
const TranslatorClass = this.translatorClass;
if (this.document === undefined) {
throw new InvalidStateError('No document');
}
const visitor = new TranslatorClass(this.document);
this.visitor = visitor;
this.document.walkabout(visitor);
this.output = visitor.root;
}
}
/*
POJOWriter.settingsSpec = [
'"Docutils-js POJO" Writer Options',
null,
[]];
*/
export default POJOWriter;