docutils-ts
Version:
Port of the Python Docutils library to TypeScript
40 lines (39 loc) • 1.06 kB
JavaScript
import Input from './io/input.js';
import Output from './io/output.js';
/**
* Direct string input.
*/
export class StringInput extends Input {
constructor(args) {
super(args);
this.sourcePath = '<string>';
}
read() {
return Promise.resolve(this.source);
}
readlines() {
if (typeof this.source === 'string') {
return Promise.resolve(this.source.split('\n'));
}
else if (Array.isArray(this.source)) {
return Promise.resolve(this.source);
}
else {
return Promise.reject(new Error('StringInput: source is not a string or array'));
}
}
}
export class StringOutput extends Output {
constructor(args) {
super(args);
this.defaultDestinationPath = '<string>';
}
async write(data) {
// self.destination = self.encode(data) // fixme encoding
if (Array.isArray(data)) {
data = JSON.stringify(data);
}
this.destination = data;
return this.destination;
}
}