2015-05-18 17:24:41 +02:00
|
|
|
/**
|
|
|
|
* Tidy up the HTML in a given file
|
|
|
|
*/
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const log4js = require('log4js');
|
|
|
|
const settings = require('./Settings');
|
|
|
|
const spawn = require('child_process').spawn;
|
2015-05-18 17:24:41 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
exports.tidy = function (srcFile) {
|
|
|
|
const logger = log4js.getLogger('TidyHtml');
|
2015-05-18 18:43:46 +02:00
|
|
|
|
2019-01-31 14:42:41 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Don't do anything if Tidy hasn't been enabled
|
|
|
|
if (!settings.tidyHtml) {
|
|
|
|
logger.debug('tidyHtml has not been configured yet, ignoring tidy request');
|
|
|
|
return resolve(null);
|
|
|
|
}
|
2015-05-18 17:24:41 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let errMessage = '';
|
2015-05-18 17:24:41 +02:00
|
|
|
|
2019-01-31 14:42:41 +01:00
|
|
|
// Spawn a new tidy instance that cleans up the file inline
|
2020-11-23 19:24:19 +01:00
|
|
|
logger.debug(`Tidying ${srcFile}`);
|
|
|
|
const tidy = spawn(settings.tidyHtml, ['-modify', srcFile]);
|
2015-05-18 17:24:41 +02:00
|
|
|
|
2019-01-31 14:42:41 +01:00
|
|
|
// Keep track of any error messages
|
2020-11-23 19:24:19 +01:00
|
|
|
tidy.stderr.on('data', (data) => {
|
2019-01-31 14:42:41 +01:00
|
|
|
errMessage += data.toString();
|
|
|
|
});
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
tidy.on('close', (code) => {
|
2019-01-31 14:42:41 +01:00
|
|
|
// Tidy returns a 0 when no errors occur and a 1 exit code when
|
|
|
|
// the file could be tidied but a few warnings were generated
|
|
|
|
if (code === 0 || code === 1) {
|
2020-11-23 19:24:19 +01:00
|
|
|
logger.debug(`Tidied ${srcFile} successfully`);
|
2019-01-31 14:42:41 +01:00
|
|
|
resolve(null);
|
|
|
|
} else {
|
2020-11-23 19:24:19 +01:00
|
|
|
logger.error(`Failed to tidy ${srcFile}\n${errMessage}`);
|
|
|
|
reject(`Tidy died with exit code ${code}`);
|
2019-01-31 14:42:41 +01:00
|
|
|
}
|
|
|
|
});
|
2015-05-18 17:24:41 +02:00
|
|
|
});
|
2020-11-23 19:24:19 +01:00
|
|
|
};
|