diff2html/src/diff2html.js

110 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-04-12 01:59:54 +00:00
/*
*
* Diff to HTML (diff2html.js)
* Author: rtfpessoa
*
*/
(function() {
var diffParser = require('./diff-parser.js').DiffParser;
var htmlPrinter = require('./html-printer.js').HtmlPrinter;
2017-10-16 21:00:03 +00:00
var utils = require('./utils.js').Utils;
2015-04-12 01:59:54 +00:00
function Diff2Html() {
}
2017-10-16 21:00:03 +00:00
var defaultConfig = {
wordByWord: true,
outputFormat: 'line-by-line',
matching: 'none',
matchWordsThreshold: 0.25,
matchingMaxComparisons: 2500,
maxLineLengthHighlight: 10000,
2019-02-01 00:18:24 +00:00
renderNothingWhenEmpty: false
2017-10-16 21:00:03 +00:00
};
2015-04-12 01:59:54 +00:00
/*
* Generates json object from string diff input
2015-04-12 01:59:54 +00:00
*/
Diff2Html.prototype.getJsonFromDiff = function(diffInput, config) {
2017-10-16 21:00:03 +00:00
var cfg = utils.safeConfig(config, defaultConfig);
return diffParser.generateDiffJson(diffInput, cfg);
};
/*
* Generates the html diff. The config parameter configures the output/input formats and other options
*/
Diff2Html.prototype.getPrettyHtml = function(diffInput, config) {
2017-10-16 21:00:03 +00:00
var cfg = utils.safeConfig(config, defaultConfig);
var diffJson = diffInput;
2017-10-16 21:00:03 +00:00
if (!cfg.inputFormat || cfg.inputFormat === 'diff') {
diffJson = diffParser.generateDiffJson(diffInput, cfg);
}
var fileList = '';
2017-10-16 21:00:03 +00:00
if (cfg.showFiles === true) {
fileList = htmlPrinter.generateFileListSummary(diffJson, cfg);
}
var diffOutput = '';
2017-10-16 21:00:03 +00:00
if (cfg.outputFormat === 'side-by-side') {
diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, cfg);
} else {
2017-10-16 21:00:03 +00:00
diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, cfg);
}
return fileList + diffOutput;
2015-04-12 01:59:54 +00:00
};
/*
* Deprecated methods - The following methods exist only to maintain compatibility with previous versions
2015-04-12 01:59:54 +00:00
*/
/*
* Generates pretty html from string diff input
*/
Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) {
2017-10-16 21:00:03 +00:00
var cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = 'diff';
cfg.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffInput, cfg);
2015-04-12 01:59:54 +00:00
};
/*
* Generates pretty html from a json object
*/
Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) {
2017-10-16 21:00:03 +00:00
var cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = 'json';
cfg.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffJson, cfg);
2015-04-12 01:59:54 +00:00
};
/*
* Generates pretty side by side html from string diff input
*/
Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) {
2017-10-16 21:00:03 +00:00
var cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = 'diff';
cfg.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffInput, cfg);
2015-04-12 01:59:54 +00:00
};
/*
* Generates pretty side by side html from a json object
*/
Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) {
2017-10-16 21:00:03 +00:00
var cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = 'json';
cfg.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffJson, cfg);
2015-04-12 01:59:54 +00:00
};
var diffObject = new Diff2Html();
module.exports.Diff2Html = diffObject;
// Expose diff2html in the browser
global.Diff2Html = diffObject;
})();