docutils-ts
Version:
Port of the Python Docutils library to TypeScript
46 lines (45 loc) • 1.47 kB
JavaScript
import Component from "./component.js";
import { getLanguage } from "./languages/index.js";
import { InvalidStateError } from "./exceptions.js";
const __version__ = '';
/**
* Base class for all writers.
*/
export default class Writer extends Component {
constructor() {
super(...arguments);
this.parts = {};
}
// TODO : SHOULD 'write' BE ASYNC ?
async write(document, destination) {
this.document = document;
if (document !== undefined) {
this.language = getLanguage(document.settings.languageCode, document.reporter);
}
this.destination = destination;
this.translate();
let fn;
if (this.destination) {
if (typeof this.destination === 'function') {
fn = this.destination;
}
else if (typeof this.destination.write === 'function') {
fn = this.destination.write.bind(this.destination);
}
}
if (fn !== undefined && this.output !== undefined) {
return fn(this.output); // Should this call be async?
}
else {
return this.output;
}
}
assembleParts() {
if (this.document === undefined) {
throw new InvalidStateError();
}
this.parts.whole = this.output; // TODO : fixme!
this.parts.encoding = this.document.settings.outputEncoding;
this.parts.version = __version__;
}
}