diff2html/src/diff2html.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-04-12 01:59:54 +00:00
/*
*
* Diff to HTML (diff2html.js)
* Author: rtfpessoa
*
*/
(function() {
const diffParser = require("./diff-parser.js").DiffParser;
const htmlPrinter = require("./html-printer.js").HtmlPrinter;
const utils = require("./utils.js").Utils;
2015-04-12 01:59:54 +00:00
function Diff2Html() {}
2015-04-12 01:59:54 +00:00
const defaultConfig = {
inputFormat: "diff",
outputFormat: "line-by-line",
showFiles: false,
diffStyle: "word",
matching: "none",
2017-10-16 21:00:03 +00:00
matchWordsThreshold: 0.25,
matchingMaxComparisons: 2500,
maxLineSizeInBlockForComparison: 200,
maxLineLengthHighlight: 10000,
templates: {},
rawTemplates: {},
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) {
const cfg = utils.safeConfig(config, defaultConfig);
2017-10-16 21:00:03 +00:00
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) {
const cfg = utils.safeConfig(config, defaultConfig);
let diffJson = diffInput;
if (!cfg.inputFormat || cfg.inputFormat === "diff") {
2017-10-16 21:00:03 +00:00
diffJson = diffParser.generateDiffJson(diffInput, cfg);
}
let fileList = "";
2017-10-16 21:00:03 +00:00
if (cfg.showFiles === true) {
fileList = htmlPrinter.generateFileListSummary(diffJson, cfg);
}
let diffOutput = "";
if (cfg.outputFormat === "side-by-side") {
2017-10-16 21:00:03 +00:00
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) {
const cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = "diff";
cfg.outputFormat = "line-by-line";
2017-10-16 21:00:03 +00:00
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) {
const cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = "json";
cfg.outputFormat = "line-by-line";
2017-10-16 21:00:03 +00:00
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) {
const cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = "diff";
cfg.outputFormat = "side-by-side";
2017-10-16 21:00:03 +00:00
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) {
const cfg = utils.safeConfig(config, defaultConfig);
cfg.inputFormat = "json";
cfg.outputFormat = "side-by-side";
2017-10-16 21:00:03 +00:00
return this.getPrettyHtml(diffJson, cfg);
2015-04-12 01:59:54 +00:00
};
const diffObject = new Diff2Html();
module.exports.Diff2Html = diffObject;
// Expose diff2html in the browser
global.Diff2Html = diffObject;
})();