|
|
|
// 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)',
|
|
|
|
},
|
|
|
|
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);
|
|
|
|
|
|
|
|
const cb = this.async();
|
|
|
|
runScheme('guile', ['-c', expr]).then(data => {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|