@podlite/schema
Version:
AST tools for Podlite markup language
72 lines • 2.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toAny = void 0;
const makeQuery_1 = require("./helpers/makeQuery");
const makeInterator_1 = __importDefault(require("./helpers/makeInterator"));
const writer_1 = __importDefault(require("./writer"));
const _1 = require("./");
const toAny = (options = {}, plugins = []) => {
let fns = plugins;
// setup pod6 processor
const processor = options.processor || _1.parse;
const _writer = options.writer;
chain.use = use;
chain.run = run;
chain.getPlugins = () => fns;
return chain;
function chain(root) { }
function use(key, fn) {
if (key instanceof Array) {
return (0, exports.toAny)(options, [...fns, ...fn]);
}
if (key instanceof Object) {
for (var prop in key) {
if (key.hasOwnProperty(prop)) {
use(prop, key[prop]);
}
}
return chain;
}
fns.push((0, makeQuery_1.makeRule)((0, makeQuery_1.makePlug)(key), fn));
return chain;
}
function run(src, opt_writer) {
let res = '';
const _tmp_writer = opt_writer ||
_writer ||
new writer_1.default(s => {
res = res + s;
});
const writer = 'function' === typeof _tmp_writer
? new _tmp_writer(s => {
res = res + s;
})
: _tmp_writer;
// src may be preparsed tree
const tree = 'string' === typeof src ? processor(src) : src;
// make new instance of HTML with initialized plugins
// reverse init
let newFns = fns.slice();
newFns.reverse();
const interator = (0, makeInterator_1.default)(newFns.map(rule => (0, makeQuery_1.makeRule)(rule.rule, rule.fn(writer, processor, tree))).reverse());
const context = {};
writer.startWrite(tree);
const result = interator(tree, context);
writer.endWrite();
return {
errors: writer.errors,
toString: () => res,
valueOf: () => res,
indexingTerms: writer.INDEXTERMS,
annotations: writer.FOOTNOTES,
defenitions: writer.DEFINITIONS,
interator: result,
};
}
};
exports.toAny = toAny;
exports.default = exports.toAny;
//# sourceMappingURL=exportAny.js.map