You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

2 years ago
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2022 Ivan Polyakov
const { spawn } = require('child_process');
const { validate } = require('schema-utils');
const schema = {
type: 'object',
properties: {
expr: {
type: 'string',
default: '(use-modules (sxml simple))(sxml->xml SXML_LOADER_CONTENT)',
},
2 years ago
doctype: {
type: 'string',
default: '<!DOCTYPE html>',
},
},
};
module.exports = function(content, map, meta) {
const options = this.getOptions();
validate(schema, options, {
name: 'SXML Loader',
baseDataPath: 'options',
});
let doctype = schema.properties.doctype.default;
if (options.doctype)
doctype = options.doctype;
let expr = schema.properties.expr.default;
if (options.expr)
expr = options.expr;
expr = expr.replace('SXML_LOADER_CONTENT', content);
chdir(this.rootContext);
2 years ago
const cb = this.async();
runScheme('guile', ['-c', expr]).then(data => {
2 years ago
cb(null, `${doctype}\n${data}`, map, meta);
}).catch(code => {
console.error(`Guile exited with code ${code}`);
});
}
function runScheme(interpreter, flags) {
return new Promise((resolve, reject) => {
const scheme = spawn(interpreter, flags);
scheme.stdout.on('data', (data) => {
resolve(data);
});
scheme.stderr.on('data', (data) => {
console.error(data.toString());
});
scheme.on('exit', (code) => {
if (code)
reject(code);
});
});
2 years ago
}