2015-05-18 17:24:41 +02:00
|
|
|
/**
|
|
|
|
* Tidy up the HTML in a given file
|
|
|
|
*/
|
|
|
|
|
2015-05-18 18:43:46 +02:00
|
|
|
var log4js = require('log4js');
|
|
|
|
var settings = require('./Settings');
|
2015-05-18 17:24:41 +02:00
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
exports.tidy = function(srcFile, callback) {
|
2015-05-18 18:43:46 +02:00
|
|
|
var logger = log4js.getLogger('TidyHtml');
|
|
|
|
|
2015-05-18 17:24:41 +02:00
|
|
|
// Don't do anything if Tidy hasn't been enabled
|
|
|
|
if (!settings.tidyHtml) {
|
2015-05-18 18:43:46 +02:00
|
|
|
logger.debug('tidyHtml has not been configured yet, ignoring tidy request');
|
2015-05-18 17:24:41 +02:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
var errMessage = '';
|
|
|
|
|
|
|
|
// Spawn a new tidy instance that cleans up the file inline
|
2015-05-18 18:43:46 +02:00
|
|
|
logger.debug('Tidying ' + srcFile);
|
2015-05-18 17:24:41 +02:00
|
|
|
var tidy = spawn(settings.tidyHtml, ['-modify', srcFile]);
|
|
|
|
|
|
|
|
// Keep track of any error messages
|
|
|
|
tidy.stderr.on('data', function (data) {
|
|
|
|
errMessage += data.toString();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait until Tidy is done
|
|
|
|
tidy.on('close', function(code) {
|
|
|
|
// 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) {
|
2015-05-18 18:43:46 +02:00
|
|
|
logger.debug('Tidied ' + srcFile + ' successfully');
|
2015-05-18 17:24:41 +02:00
|
|
|
return callback(null);
|
|
|
|
} else {
|
2015-05-18 18:43:46 +02:00
|
|
|
logger.error('Failed to tidy ' + srcFile + '\n' + errMessage);
|
2015-05-18 17:24:41 +02:00
|
|
|
return callback('Tidy died with exit code ' + code);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|