Merge pull request #39 from rtfpessoa/improve-code-style

Update contributors list, add codacy badge and clean code
This commit is contained in:
Rodrigo Fernandes 2015-12-20 23:01:05 +00:00
commit c80a4c6415
16 changed files with 579 additions and 501 deletions

View file

@ -17,3 +17,5 @@ Paulo Bu, [@pbu88](https://github.com/pbu88)
Nuno Teixeira, [@nmatpt](https://github.com/nmatpt)
Mikko Rantanen, [@Rantanen](https://github.com/Rantanen)
Wolfgang Illmeyer, [@escitalopram](https://github.com/escitalopram)

View file

@ -1,5 +1,7 @@
# Diff to Html by [rtfpessoa](https://github.com/rtfpessoa)
[![Codacy Badge](https://api.codacy.com/project/badge/grade/06412dc3f5a14f568778d0db8a1f7dc8)](https://www.codacy.com/app/Codacy/diff2html)
Diff to Html generates pretty HTML diffs from git diff output.
## Features

View file

@ -42,7 +42,6 @@
color: #399839;
border-radius: 5px 0 0 5px;
padding: 2px;
width: 25px;
}
.d2h-lines-deleted {
@ -55,12 +54,10 @@
color: #c33;
border-radius: 0 5px 5px 0;
padding: 2px;
width: 25px;
}
.d2h-file-name {
display: inline;
height: 33px;
line-height: 33px;
max-width: 80%;
white-space: nowrap;
@ -152,7 +149,6 @@
}
.line-num1 {
display: inline-block;
float: left;
width: 30px;
overflow: hidden;
@ -160,7 +156,6 @@
}
.line-num2 {
display: inline-block;
float: right;
width: 30px;
overflow: hidden;
@ -169,7 +164,6 @@
.d2h-code-linenumber {
position: absolute;
width: 2%;
min-width: 65px;
padding-left: 10px;
padding-right: 10px;
@ -232,9 +226,6 @@
.d2h-file-list-header {
font-weight: bold;
margin-bottom: 5px;
text-align: left;
display: inline;
float: left;
}
@ -259,12 +250,15 @@
.d2h-del.d2h-change, .d2h-ins.d2h-change {
background-color: #ffc;
}
ins.d2h-change, del.d2h-change {
background-color: #fad771;
}
.d2h-file-diff .d2h-del.d2h-change {
background-color: #fae1af;
}
.d2h-file-diff .d2h-ins.d2h-change {
background-color: #ded;
}
@ -274,15 +268,19 @@ ins.d2h-change, del.d2h-change {
display: none;
float: left;
}
.d2h-hide {
float: left;
}
.d2h-hide:target + .d2h-show {
display: inline;
}
.d2h-hide:target {
display: none;
}
.d2h-hide:target ~ .d2h-file-list {
display: block;
}

16
dist/diff2html.css vendored
View file

@ -42,7 +42,6 @@
color: #399839;
border-radius: 5px 0 0 5px;
padding: 2px;
width: 25px;
}
.d2h-lines-deleted {
@ -55,12 +54,10 @@
color: #c33;
border-radius: 0 5px 5px 0;
padding: 2px;
width: 25px;
}
.d2h-file-name {
display: inline;
height: 33px;
line-height: 33px;
max-width: 80%;
white-space: nowrap;
@ -152,7 +149,6 @@
}
.line-num1 {
display: inline-block;
float: left;
width: 30px;
overflow: hidden;
@ -160,7 +156,6 @@
}
.line-num2 {
display: inline-block;
float: right;
width: 30px;
overflow: hidden;
@ -169,7 +164,6 @@
.d2h-code-linenumber {
position: absolute;
width: 2%;
min-width: 65px;
padding-left: 10px;
padding-right: 10px;
@ -232,9 +226,6 @@
.d2h-file-list-header {
font-weight: bold;
margin-bottom: 5px;
text-align: left;
display: inline;
float: left;
}
@ -259,12 +250,15 @@
.d2h-del.d2h-change, .d2h-ins.d2h-change {
background-color: #ffc;
}
ins.d2h-change, del.d2h-change {
background-color: #fad771;
}
.d2h-file-diff .d2h-del.d2h-change {
background-color: #fae1af;
}
.d2h-file-diff .d2h-ins.d2h-change {
background-color: #ded;
}
@ -274,15 +268,19 @@ ins.d2h-change, del.d2h-change {
display: none;
float: left;
}
.d2h-hide {
float: left;
}
.d2h-hide:target + .d2h-show {
display: inline;
}
.d2h-hide:target {
display: none;
}
.d2h-hide:target ~ .d2h-file-list {
display: block;
}

344
dist/diff2html.js vendored
View file

@ -51,7 +51,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var diffParser = __webpack_require__(1).DiffParser;
var fileLister = __webpack_require__(3).FileListPrinter;
@ -63,9 +63,9 @@
/*
* Line diff type configuration
var config = {
"wordByWord": true, // (default)
'wordByWord': true, // (default)
// OR
"charByChar": true
'charByChar': true
};
*/
@ -87,19 +87,19 @@
diffJson = diffParser.generateDiffJson(diffInput);
}
var fileList = "";
var fileList = '';
if (configOrEmpty.showFiles === true) {
fileList = fileLister.generateFileList(diffJson, configOrEmpty);
}
var diffOutput = "";
var diffOutput = '';
if (configOrEmpty.outputFormat === 'side-by-side') {
diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, configOrEmpty);
} else {
diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, configOrEmpty);
}
return fileList + diffOutput
return fileList + diffOutput;
};
@ -112,9 +112,9 @@
*/
Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) {
var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'diff';
configOrEmpty['outputFormat'] = 'line-by-line';
return this.getPrettyHtml(diffInput, configOrEmpty)
configOrEmpty.inputFormat = 'diff';
configOrEmpty.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffInput, configOrEmpty);
};
/*
@ -122,9 +122,9 @@
*/
Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) {
var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'json';
configOrEmpty['outputFormat'] = 'line-by-line';
return this.getPrettyHtml(diffJson, configOrEmpty)
configOrEmpty.inputFormat = 'json';
configOrEmpty.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffJson, configOrEmpty);
};
/*
@ -132,9 +132,9 @@
*/
Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) {
var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'diff';
configOrEmpty['outputFormat'] = 'side-by-side';
return this.getPrettyHtml(diffInput, configOrEmpty)
configOrEmpty.inputFormat = 'diff';
configOrEmpty.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffInput, configOrEmpty);
};
/*
@ -142,18 +142,18 @@
*/
Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) {
var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'json';
configOrEmpty['outputFormat'] = 'side-by-side';
return this.getPrettyHtml(diffJson, configOrEmpty)
configOrEmpty.inputFormat = 'json';
configOrEmpty.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffJson, configOrEmpty);
};
var diffName = 'Diff2Html';
var diffObject = new Diff2Html();
module.exports[diffName] = diffObject;
// Expose diff2html in the browser
global[diffName] = diffObject;
module.exports.Diff2Html = diffObject;
})(this);
// Expose diff2html in the browser
global.Diff2Html = diffObject;
})();
/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))
@ -168,7 +168,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var utils = __webpack_require__(2).Utils;
@ -194,6 +194,7 @@
var newLine = null;
var saveBlock = function() {
/* Add previous block(if exists) before start a new file */
if (currentBlock) {
currentFile.blocks.push(currentBlock);
@ -202,6 +203,7 @@
};
var saveFile = function() {
/*
* Add previous file(if exists) before start a new one
* if it has name (to avoid binary files errors)
@ -379,14 +381,14 @@
var nameSplit = filename.split('.');
if (nameSplit.length > 1) {
return nameSplit[nameSplit.length - 1];
} else {
}
return language;
}
}
module.exports['DiffParser'] = new DiffParser();
module.exports.DiffParser = new DiffParser();
})(this);
})();
/***/ },
@ -400,7 +402,7 @@
*
*/
(function(ctx, undefined) {
(function() {
function Utils() {
}
@ -414,7 +416,7 @@
};
Utils.prototype.getRandomId = function(prefix) {
return prefix + "-" + Math.random().toString(36).slice(-3);
return prefix + '-' + Math.random().toString(36).slice(-3);
};
Utils.prototype.startsWith = function(str, start) {
@ -436,9 +438,9 @@
return value ? value : '';
};
module.exports['Utils'] = new Utils();
module.exports.Utils = new Utils();
})(this);
})();
/***/ },
@ -452,7 +454,7 @@
*
*/
(function (ctx, undefined) {
(function() {
var printerUtils = __webpack_require__(4).PrinterUtils;
var utils = __webpack_require__(2).Utils;
@ -461,8 +463,8 @@
}
FileListPrinter.prototype.generateFileList = function(diffFiles) {
var hideId = utils.getRandomId("d2h-hide"); //necessary if there are 2 elements like this in the same page
var showId = utils.getRandomId("d2h-show");
var hideId = utils.getRandomId('d2h-hide'); //necessary if there are 2 elements like this in the same page
var showId = utils.getRandomId('d2h-show');
return '<div class="d2h-file-list-wrapper">\n' +
' <div class="d2h-file-list-header">Files changed (' + diffFiles.length + ')&nbsp&nbsp</div>\n' +
' <a id="' + hideId + '" class="d2h-hide" href="#' + hideId + '">+</a>\n' +
@ -480,14 +482,14 @@
' <span>-' + file.deletedLines + '</span>\n' +
' </td>\n' +
' <td class="d2h-file-name"><a href="#' + printerUtils.getHtmlId(file) + '">&nbsp;' + printerUtils.getDiffName(file) + '</a></td>\n' +
' </tr>\n'
' </tr>\n';
}).join('\n') +
'</table></div>\n';
};
module.exports['FileListPrinter'] = new FileListPrinter();
module.exports.FileListPrinter = new FileListPrinter();
})(this);
})();
/***/ },
@ -501,7 +503,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var jsDiff = __webpack_require__(5);
var utils = __webpack_require__(2).Utils;
@ -512,38 +514,40 @@
PrinterUtils.prototype.getHtmlId = function(file) {
var hashCode = function(text) {
var hash = 0, i, chr, len;
if (text.length == 0) return hash;
var i, chr, len;
var hash = 0;
if (text.length === 0) return hash;
for (i = 0, len = text.length; i < len; i++) {
chr = text.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
return "d2h-" + hashCode(this.getDiffName(file)).toString().slice(-6);
return 'd2h-' + hashCode(this.getDiffName(file)).toString().slice(-6);
};
PrinterUtils.prototype.getDiffName = function(file) {
var oldFilename = file.oldName;
var newFilename = file.newName;
if (oldFilename && newFilename
&& oldFilename !== newFilename
&& !isDeletedName(newFilename)) {
if (oldFilename && newFilename && oldFilename !== newFilename && !isDeletedName(newFilename)) {
return oldFilename + ' -> ' + newFilename;
} else if (newFilename && !isDeletedName(newFilename)) {
return newFilename;
} else if (oldFilename) {
return oldFilename;
} else {
return 'Unknown filename';
}
return 'Unknown filename';
};
PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) {
var lineStart1, lineStart2;
var linePrefix1, linePrefix2, unprefixedLine1, unprefixedLine2;
var prefixSize = 1;
@ -551,17 +555,16 @@
prefixSize = 2;
}
lineStart1 = diffLine1.substr(0, prefixSize);
lineStart2 = diffLine2.substr(0, prefixSize);
diffLine1 = diffLine1.substr(prefixSize);
diffLine2 = diffLine2.substr(prefixSize);
linePrefix1 = diffLine1.substr(0, prefixSize);
linePrefix2 = diffLine2.substr(0, prefixSize);
unprefixedLine1 = diffLine1.substr(prefixSize);
unprefixedLine2 = diffLine2.substr(prefixSize);
var diff;
if (config.charByChar) {
diff = jsDiff.diffChars(diffLine1, diffLine2);
diff = jsDiff.diffChars(unprefixedLine1, unprefixedLine2);
} else {
diff = jsDiff.diffWordsWithSpace(diffLine1, diffLine2);
diff = jsDiff.diffWordsWithSpace(unprefixedLine1, unprefixedLine2);
}
var highlightedLine = '';
@ -569,25 +572,30 @@
var changedWords = [];
if (!config.charByChar && config.matching === 'words') {
var treshold = 0.25;
if (typeof(config.matchWordsThreshold) !== "undefined") {
if (typeof(config.matchWordsThreshold) !== 'undefined') {
treshold = config.matchWordsThreshold;
}
var matcher = Rematch.rematch(function(a, b) {
var amod = a.value,
bmod = b.value,
result = Rematch.distance(amod, bmod);
return result;
var amod = a.value;
var bmod = b.value;
return Rematch.distance(amod, bmod);
});
var removed = diff.filter(function isRemoved(element) {
return element.removed;
});
var added = diff.filter(function isAdded(element) {
return element.added;
});
var chunks = matcher(added, removed);
chunks = chunks.forEach(function(chunk){
chunks.forEach(function(chunk) {
if (chunk[0].length === 1 && chunk[1].length === 1) {
var dist = Rematch.distance(chunk[0][0].value, chunk[1][0].value)
var dist = Rematch.distance(chunk[0][0].value, chunk[1][0].value);
if (dist < treshold) {
changedWords.push(chunk[0][0]);
changedWords.push(chunk[1][0]);
@ -595,6 +603,7 @@
}
});
}
diff.forEach(function(part) {
var addClass = changedWords.indexOf(part) > -1 ? ' class="d2h-change"' : '';
var elemType = part.added ? 'ins' : part.removed ? 'del' : null;
@ -609,11 +618,11 @@
return {
first: {
prefix: lineStart1,
prefix: linePrefix1,
line: removeIns(highlightedLine)
},
second: {
prefix: lineStart2,
prefix: linePrefix2,
line: removeDel(highlightedLine)
}
}
@ -631,9 +640,9 @@
return line.replace(/(<del[^>]*>((.|\n)*?)<\/del>)/g, '');
}
module.exports['PrinterUtils'] = new PrinterUtils();
module.exports.PrinterUtils = new PrinterUtils();
})(this);
})();
/***/ },
@ -1892,7 +1901,8 @@
*
*/
(function(ctx, undefined) {
(function() {
var Rematch = {};
Rematch.arrayToString = function arrayToString(a) {
if (Object.prototype.toString.apply(a, []) === "[object Array]") {
@ -1900,7 +1910,7 @@
} else {
return a;
}
}
};
/*
Copyright (c) 2011 Andrei Mackenzie
@ -1938,26 +1948,29 @@
}
}
}
return matrix[b.length][a.length];
}
Rematch.levenshtein = levenshtein;
Rematch.distance = function distance(x, y) {
x = x.trim();
y = y.trim();
var lev = levenshtein(x,y),
score = lev / (x.length + y.length);
var lev = levenshtein(x, y);
var score = lev / (x.length + y.length);
return score;
}
};
Rematch.rematch = function rematch(distanceFunction) {
function findBestMatch(a, b, cache) {
var cachecount = 0;
for (var key in cache) {
cachecount++;
}
var bestMatchDist = Infinity;
var bestMatch;
for (var i = 0; i < a.length; ++i) {
@ -1976,21 +1989,25 @@
}
}
}
return bestMatch;
}
function group(a, b, level, cache) {
function group(a, b, level, cache) {
if (typeof(cache) === "undefined") {
cache = {};
}
var minLength = Math.min(a.length, b.length);
var bm = findBestMatch(a, b, cache);
if (!level) {
level = 0;
}
if (!bm || (a.length + b.length < 3)) {
return [[a, b]];
}
var a1 = a.slice(0, bm.indexA),
b1 = b.slice(0, bm.indexB),
aMatch = [a[bm.indexA]],
@ -2004,20 +2021,24 @@
var groupMatch = group(aMatch, bMatch, level + 1, cache);
var group2 = group(a2, b2, level + 1, cache);
var result = groupMatch;
if (bm.indexA > 0 || bm.indexB > 0) {
result = group1.concat(result);
}
if (a.length > tailA || b.length > tailB) {
result = result.concat(group2);
}
return result;
}
return group;
}
};
module.exports['Rematch'] = Rematch;
module.exports.Rematch = Rematch;
})(this);
})();
/***/ },
@ -2031,21 +2052,24 @@
*
*/
(function(ctx, undefined) {
(function() {
var lineByLinePrinter = __webpack_require__(22).LineByLinePrinter;
var LineByLinePrinter = __webpack_require__(22).LineByLinePrinter;
var sideBySidePrinter = __webpack_require__(23).SideBySidePrinter;
function HtmlPrinter() {
}
HtmlPrinter.prototype.generateLineByLineJsonHtml = lineByLinePrinter.generateLineByLineJsonHtml;
HtmlPrinter.prototype.generateLineByLineJsonHtml = function(diffFiles, config) {
var lineByLinePrinter = new LineByLinePrinter(config);
return lineByLinePrinter.generateLineByLineJsonHtml(diffFiles);
};
HtmlPrinter.prototype.generateSideBySideJsonHtml = sideBySidePrinter.generateSideBySideJsonHtml;
module.exports['HtmlPrinter'] = new HtmlPrinter();
module.exports.HtmlPrinter = new HtmlPrinter();
})(this);
})();
/***/ },
@ -2059,25 +2083,27 @@
*
*/
(function(ctx, undefined) {
(function() {
var diffParser = __webpack_require__(1).DiffParser;
var printerUtils = __webpack_require__(4).PrinterUtils;
var utils = __webpack_require__(2).Utils;
var Rematch = __webpack_require__(20).Rematch;
function LineByLinePrinter() {
function LineByLinePrinter(config) {
this.config = config;
}
LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles, config) {
LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles) {
var that = this;
return '<div class="d2h-wrapper">\n' +
diffFiles.map(function(file) {
var diffs;
if (file.blocks.length) {
diffs = generateFileHtml(file, config);
diffs = that._generateFileHtml(file);
} else {
diffs = generateEmptyDiff();
diffs = that._generateEmptyDiff();
}
return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' +
@ -2107,12 +2133,14 @@
};
var matcher = Rematch.rematch(function(a, b) {
var amod = a.content.substr(1),
bmod = b.content.substr(1);
var amod = a.content.substr(1);
var bmod = b.content.substr(1);
return Rematch.distance(amod, bmod);
});
function generateFileHtml(file, config) {
LineByLinePrinter.prototype._generateFileHtml = function(file) {
var that = this;
return file.blocks.map(function(block) {
var lines = '<tr>\n' +
@ -2124,11 +2152,14 @@
var oldLines = [];
var newLines = [];
function processChangeBlock() {
var matches;
var insertType;
var deleteType;
var doMatching = config.matching === "lines" || config.matching === "words";
var doMatching = that.config.matching === 'lines' || that.config.matching === 'words';
if (doMatching) {
matches = matcher(oldLines, newLines);
insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
@ -2138,36 +2169,36 @@
insertType = diffParser.LINE_TYPE.INSERTS;
deleteType = diffParser.LINE_TYPE.DELETES;
}
matches.forEach(function(match) {
var oldLines = match[0];
var newLines = match[1];
oldLines = match[0];
newLines = match[1];
var processedOldLines = [];
var processedNewLines = [];
var j = 0;
var oldLine, newLine,
common = Math.min(oldLines.length, newLines.length),
max = Math.max(oldLines.length, newLines.length);
for (j = 0; j < common; j++) {
var common = Math.min(oldLines.length, newLines.length);
var oldLine, newLine;
for (var j = 0; j < common; j++) {
oldLine = oldLines[j];
newLine = newLines[j];
config.isCombined = file.isCombined;
var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, config);
that.config.isCombined = file.isCombined;
var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, that.config);
processedOldLines +=
generateLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber,
that._generateLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber,
diff.first.line, diff.first.prefix);
processedNewLines +=
generateLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
that._generateLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
diff.second.line, diff.second.prefix);
}
lines += processedOldLines + processedNewLines;
lines += processLines(oldLines.slice(common), newLines.slice(common));
processedOldLines = [];
processedNewLines = [];
lines += that._processLines(oldLines.slice(common), newLines.slice(common));
});
oldLines = [];
newLines = [];
}
@ -2180,16 +2211,17 @@
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
processChangeBlock();
}
if (line.type == diffParser.LINE_TYPE.CONTEXT) {
lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type == diffParser.LINE_TYPE.DELETES) {
if (line.type === diffParser.LINE_TYPE.CONTEXT) {
lines += that._generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
lines += that._generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type === diffParser.LINE_TYPE.DELETES) {
oldLines.push(line);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) {
} else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
newLines.push(line);
} else {
console.error('unknown state in html line-by-line generator');
console.error('Unknown state in html line-by-line generator');
processChangeBlock();
}
}
@ -2198,27 +2230,27 @@
return lines;
}).join('\n');
}
};
function processLines(oldLines, newLines) {
LineByLinePrinter.prototype._processLines = function(oldLines, newLines) {
var lines = '';
for (j = 0; j < oldLines.length; j++) {
var oldLine = oldLines[j];
for (var i = 0; i < oldLines.length; i++) {
var oldLine = oldLines[i];
var oldEscapedLine = utils.escape(oldLine.content);
lines += generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine);
lines += this._generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine);
}
for (j = 0; j < newLines.length; j++) {
for (var j = 0; j < newLines.length; j++) {
var newLine = newLines[j];
var newEscapedLine = utils.escape(newLine.content);
lines += generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine);
lines += this._generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine);
}
return lines;
}
};
function generateLineHtml(type, oldNumber, newNumber, content, prefix) {
LineByLinePrinter.prototype._generateLineHtml = function(type, oldNumber, newNumber, content, prefix) {
var htmlPrefix = '';
if (prefix) {
htmlPrefix = '<span class="d2h-code-line-prefix">' + prefix + '</span>';
@ -2238,9 +2270,9 @@
' <div class="d2h-code-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' +
' </td>\n' +
'</tr>\n';
}
};
function generateEmptyDiff() {
LineByLinePrinter.prototype._generateEmptyDiff = function() {
return '<tr>\n' +
' <td class="' + diffParser.LINE_TYPE.INFO + '">' +
' <div class="d2h-code-line ' + diffParser.LINE_TYPE.INFO + '">' +
@ -2248,11 +2280,11 @@
' </div>' +
' </td>\n' +
'</tr>\n';
}
};
module.exports['LineByLinePrinter'] = new LineByLinePrinter();
module.exports.LineByLinePrinter = LineByLinePrinter;
})(this);
})();
/***/ },
@ -2266,7 +2298,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var diffParser = __webpack_require__(1).DiffParser;
var printerUtils = __webpack_require__(4).PrinterUtils;
@ -2355,11 +2387,13 @@
var oldLines = [];
var newLines = [];
function processChangeBlock() {
var matches;
var insertType;
var deleteType;
var doMatching = config.matching === "lines" || config.matching === "words";
var doMatching = config.matching === 'lines' || config.matching === 'words';
if (doMatching) {
matches = matcher(oldLines, newLines);
insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
@ -2369,17 +2403,17 @@
insertType = diffParser.LINE_TYPE.INSERTS;
deleteType = diffParser.LINE_TYPE.DELETES;
}
matches.forEach(function(match) {
var oldLines = match[0];
var newLines = match[1];
var tmpHtml;
var j = 0;
var oldLine, newLine,
common = Math.min(oldLines.length, newLines.length),
max = Math.max(oldLines.length, newLines.length);
for (j = 0; j < common; j++) {
oldLine = oldLines[j];
newLine = newLines[j];
oldLines = match[0];
newLines = match[1];
var common = Math.min(oldLines.length, newLines.length);
var max = Math.max(oldLines.length, newLines.length);
for (var j = 0; j < common; j++) {
var oldLine = oldLines[j];
var newLine = newLines[j];
config.isCombined = file.isCombined;
@ -2392,17 +2426,21 @@
generateSingleLineHtml(insertType, newLine.newNumber,
diff.second.line, diff.second.prefix);
}
if (max > common) {
var oldSlice = oldLines.slice(common),
newSlice = newLines.slice(common);
tmpHtml = processLines(oldLines.slice(common), newLines.slice(common));
var oldSlice = oldLines.slice(common);
var newSlice = newLines.slice(common);
var tmpHtml = processLines(oldSlice, newSlice);
fileHtml.left += tmpHtml.left;
fileHtml.right += tmpHtml.right;
}
});
oldLines = [];
newLines = [];
}
for (var i = 0; i < block.lines.length; i++) {
var line = block.lines[i];
var prefix = line[0];
@ -2412,15 +2450,16 @@
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
processChangeBlock();
}
if (line.type == diffParser.LINE_TYPE.CONTEXT) {
if (line.type === diffParser.LINE_TYPE.CONTEXT) {
fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine, prefix);
fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
} else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
fileHtml.left += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', '');
fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
} else if (line.type == diffParser.LINE_TYPE.DELETES) {
} else if (line.type === diffParser.LINE_TYPE.DELETES) {
oldLines.push(line);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) {
} else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
newLines.push(line);
} else {
console.error('unknown state in html side-by-side generator');
@ -2440,21 +2479,24 @@
fileHtml.right = '';
var maxLinesNumber = Math.max(oldLines.length, newLines.length);
for (j = 0; j < maxLinesNumber; j++) {
var oldLine = oldLines[j];
var newLine = newLines[j];
for (var i = 0; i < maxLinesNumber; i++) {
var oldLine = oldLines[i];
var newLine = newLines[i];
var oldContent;
var newContent;
var oldPrefix;
var newPrefix;
if (oldLine) {
oldContent = utils.escape(oldLine.content.substr(1));
oldPrefix = oldLine.content[0];
}
if (newLine) {
newContent = utils.escape(newLine.content.substr(1));
newPrefix = newLine.content[0];
}
if (oldLine && newLine) {
fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, oldContent, oldPrefix);
fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, newContent, newPrefix);
@ -2506,9 +2548,9 @@
return fileHtml;
}
module.exports['SideBySidePrinter'] = new SideBySidePrinter();
module.exports.SideBySidePrinter = new SideBySidePrinter();
})(this);
})();
/***/ }

View file

@ -1 +1 @@
.d2h-wrapper{display:block;margin:0 auto;text-align:left;width:100%}.d2h-file-wrapper{border:1px solid #ddd;border-radius:3px;margin-bottom:1em}.d2h-file-header{padding:5px 10px;border-bottom:1px solid #d8d8d8;background-color:#f7f7f7;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-stats{display:inline;font-size:12px;text-align:center;max-width:15%}.d2h-lines-added{text-align:right}.d2h-lines-added>*{background-color:#ceffce;border:1px solid #b4e2b4;color:#399839;border-radius:5px 0 0 5px;padding:2px;width:25px}.d2h-lines-deleted{text-align:left}.d2h-lines-deleted>*{background-color:#f7c8c8;border:1px solid #e9aeae;color:#c33;border-radius:0 5px 5px 0;padding:2px;width:25px}.d2h-file-name{display:inline;height:33px;line-height:33px;max-width:80%;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}.d2h-diff-table{border-collapse:collapse;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;height:18px;line-height:18px;width:100%}.d2h-files-diff{width:100%}.d2h-file-diff{overflow-x:scroll;overflow-y:hidden}.d2h-file-side-diff{display:inline-block;overflow-x:scroll;overflow-y:hidden;width:50%;margin-right:-4px}.d2h-code-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:80px;color:inherit;overflow-x:inherit;background:none}.d2h-code-side-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:50px;color:inherit;overflow-x:inherit;background:none}.d2h-code-line del,.d2h-code-side-line del{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#ffb6ba;border-radius:.2em}.d2h-code-line ins,.d2h-code-side-line ins{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#97f295;border-radius:.2em}.d2h-code-line-prefix{float:left;background:none;padding:0}.d2h-code-line-ctn{background:none;padding:0}.line-num1{display:inline-block;float:left;width:30px;overflow:hidden;text-overflow:ellipsis}.line-num2{display:inline-block;float:right;width:30px;overflow:hidden;text-overflow:ellipsis}.d2h-code-linenumber{position:absolute;width:2%;min-width:65px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,0.3);text-align:right;border:solid #eeeeee;border-width:0 1px 0 1px;cursor:pointer}.d2h-code-side-linenumber{position:absolute;width:35px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,0.3);text-align:right;border:solid #eeeeee;border-width:0 1px 0 1px;cursor:pointer;overflow:hidden;text-overflow:ellipsis}.d2h-del{background-color:#fee8e9;border-color:#e9aeae}.d2h-ins{background-color:#dfd;border-color:#b4e2b4}.d2h-info{background-color:#f8fafd;color:rgba(0,0,0,0.3);border-color:#d5e4f2}.d2h-file-list-wrapper{margin-bottom:10px;padding:0 10px}.d2h-file-list-wrapper a{text-decoration:none;color:#3572b0}.d2h-file-list-wrapper a:visited{color:#3572b0}.d2h-file-list-header{font-weight:bold;margin-bottom:5px;text-align:left;display:inline;float:left}.d2h-file-list-line{text-align:left;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-list-line .d2h-file-name{line-height:21px}.d2h-file-list{display:none}.d2h-clear{display:block;clear:both}.d2h-del.d2h-change,.d2h-ins.d2h-change{background-color:#ffc}ins.d2h-change,del.d2h-change{background-color:#fad771}.d2h-file-diff .d2h-del.d2h-change{background-color:#fae1af}.d2h-file-diff .d2h-ins.d2h-change{background-color:#ded}.d2h-show{display:none;float:left}.d2h-hide{float:left}.d2h-hide:target+.d2h-show{display:inline}.d2h-hide:target{display:none}.d2h-hide:target~.d2h-file-list{display:block}
.d2h-wrapper{display:block;margin:0 auto;text-align:left;width:100%}.d2h-file-wrapper{border:1px solid #ddd;border-radius:3px;margin-bottom:1em}.d2h-file-header{padding:5px 10px;border-bottom:1px solid #d8d8d8;background-color:#f7f7f7;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-stats{display:inline;font-size:12px;text-align:center;max-width:15%}.d2h-lines-added{text-align:right}.d2h-lines-added>*{background-color:#ceffce;border:1px solid #b4e2b4;color:#399839;border-radius:5px 0 0 5px;padding:2px}.d2h-lines-deleted{text-align:left}.d2h-lines-deleted>*{background-color:#f7c8c8;border:1px solid #e9aeae;color:#c33;border-radius:0 5px 5px 0;padding:2px}.d2h-file-name{display:inline;line-height:33px;max-width:80%;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}.d2h-diff-table{border-collapse:collapse;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;height:18px;line-height:18px;width:100%}.d2h-files-diff{width:100%}.d2h-file-diff{overflow-x:scroll;overflow-y:hidden}.d2h-file-side-diff{display:inline-block;overflow-x:scroll;overflow-y:hidden;width:50%;margin-right:-4px}.d2h-code-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:80px;color:inherit;overflow-x:inherit;background:none}.d2h-code-side-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:50px;color:inherit;overflow-x:inherit;background:none}.d2h-code-line del,.d2h-code-side-line del{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#ffb6ba;border-radius:.2em}.d2h-code-line ins,.d2h-code-side-line ins{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#97f295;border-radius:.2em}.d2h-code-line-prefix{float:left;background:none;padding:0}.d2h-code-line-ctn{background:none;padding:0}.line-num1{float:left;width:30px;overflow:hidden;text-overflow:ellipsis}.line-num2{float:right;width:30px;overflow:hidden;text-overflow:ellipsis}.d2h-code-linenumber{position:absolute;min-width:65px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,0.3);text-align:right;border:solid #eeeeee;border-width:0 1px 0 1px;cursor:pointer}.d2h-code-side-linenumber{position:absolute;width:35px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,0.3);text-align:right;border:solid #eeeeee;border-width:0 1px 0 1px;cursor:pointer;overflow:hidden;text-overflow:ellipsis}.d2h-del{background-color:#fee8e9;border-color:#e9aeae}.d2h-ins{background-color:#dfd;border-color:#b4e2b4}.d2h-info{background-color:#f8fafd;color:rgba(0,0,0,0.3);border-color:#d5e4f2}.d2h-file-list-wrapper{margin-bottom:10px;padding:0 10px}.d2h-file-list-wrapper a{text-decoration:none;color:#3572b0}.d2h-file-list-wrapper a:visited{color:#3572b0}.d2h-file-list-header{font-weight:bold;float:left}.d2h-file-list-line{text-align:left;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-list-line .d2h-file-name{line-height:21px}.d2h-file-list{display:none}.d2h-clear{display:block;clear:both}.d2h-del.d2h-change,.d2h-ins.d2h-change{background-color:#ffc}ins.d2h-change,del.d2h-change{background-color:#fad771}.d2h-file-diff .d2h-del.d2h-change{background-color:#fae1af}.d2h-file-diff .d2h-ins.d2h-change{background-color:#ded}.d2h-show{display:none;float:left}.d2h-hide{float:left}.d2h-hide:target+.d2h-show{display:inline}.d2h-hide:target{display:none}.d2h-hide:target~.d2h-file-list{display:block}

File diff suppressed because one or more lines are too long

View file

@ -5,7 +5,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var utils = require('./utils.js').Utils;
@ -31,6 +31,7 @@
var newLine = null;
var saveBlock = function() {
/* Add previous block(if exists) before start a new file */
if (currentBlock) {
currentFile.blocks.push(currentBlock);
@ -39,6 +40,7 @@
};
var saveFile = function() {
/*
* Add previous file(if exists) before start a new one
* if it has name (to avoid binary files errors)
@ -216,11 +218,11 @@
var nameSplit = filename.split('.');
if (nameSplit.length > 1) {
return nameSplit[nameSplit.length - 1];
} else {
}
return language;
}
}
module.exports['DiffParser'] = new DiffParser();
module.exports.DiffParser = new DiffParser();
})(this);
})();

View file

@ -5,7 +5,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var diffParser = require('./diff-parser.js').DiffParser;
var fileLister = require('./file-list-printer.js').FileListPrinter;
@ -17,9 +17,9 @@
/*
* Line diff type configuration
var config = {
"wordByWord": true, // (default)
'wordByWord': true, // (default)
// OR
"charByChar": true
'charByChar': true
};
*/
@ -41,19 +41,19 @@
diffJson = diffParser.generateDiffJson(diffInput);
}
var fileList = "";
var fileList = '';
if (configOrEmpty.showFiles === true) {
fileList = fileLister.generateFileList(diffJson, configOrEmpty);
}
var diffOutput = "";
var diffOutput = '';
if (configOrEmpty.outputFormat === 'side-by-side') {
diffOutput = htmlPrinter.generateSideBySideJsonHtml(diffJson, configOrEmpty);
} else {
diffOutput = htmlPrinter.generateLineByLineJsonHtml(diffJson, configOrEmpty);
}
return fileList + diffOutput
return fileList + diffOutput;
};
@ -66,9 +66,9 @@
*/
Diff2Html.prototype.getPrettyHtmlFromDiff = function(diffInput, config) {
var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'diff';
configOrEmpty['outputFormat'] = 'line-by-line';
return this.getPrettyHtml(diffInput, configOrEmpty)
configOrEmpty.inputFormat = 'diff';
configOrEmpty.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffInput, configOrEmpty);
};
/*
@ -76,9 +76,9 @@
*/
Diff2Html.prototype.getPrettyHtmlFromJson = function(diffJson, config) {
var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'json';
configOrEmpty['outputFormat'] = 'line-by-line';
return this.getPrettyHtml(diffJson, configOrEmpty)
configOrEmpty.inputFormat = 'json';
configOrEmpty.outputFormat = 'line-by-line';
return this.getPrettyHtml(diffJson, configOrEmpty);
};
/*
@ -86,9 +86,9 @@
*/
Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function(diffInput, config) {
var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'diff';
configOrEmpty['outputFormat'] = 'side-by-side';
return this.getPrettyHtml(diffInput, configOrEmpty)
configOrEmpty.inputFormat = 'diff';
configOrEmpty.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffInput, configOrEmpty);
};
/*
@ -96,15 +96,15 @@
*/
Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function(diffJson, config) {
var configOrEmpty = config || {};
configOrEmpty['inputFormat'] = 'json';
configOrEmpty['outputFormat'] = 'side-by-side';
return this.getPrettyHtml(diffJson, configOrEmpty)
configOrEmpty.inputFormat = 'json';
configOrEmpty.outputFormat = 'side-by-side';
return this.getPrettyHtml(diffJson, configOrEmpty);
};
var diffName = 'Diff2Html';
var diffObject = new Diff2Html();
module.exports[diffName] = diffObject;
// Expose diff2html in the browser
global[diffName] = diffObject;
module.exports.Diff2Html = diffObject;
})(this);
// Expose diff2html in the browser
global.Diff2Html = diffObject;
})();

View file

@ -5,7 +5,7 @@
*
*/
(function (ctx, undefined) {
(function() {
var printerUtils = require('./printer-utils.js').PrinterUtils;
var utils = require('./utils.js').Utils;
@ -14,8 +14,8 @@
}
FileListPrinter.prototype.generateFileList = function(diffFiles) {
var hideId = utils.getRandomId("d2h-hide"); //necessary if there are 2 elements like this in the same page
var showId = utils.getRandomId("d2h-show");
var hideId = utils.getRandomId('d2h-hide'); //necessary if there are 2 elements like this in the same page
var showId = utils.getRandomId('d2h-show');
return '<div class="d2h-file-list-wrapper">\n' +
' <div class="d2h-file-list-header">Files changed (' + diffFiles.length + ')&nbsp&nbsp</div>\n' +
' <a id="' + hideId + '" class="d2h-hide" href="#' + hideId + '">+</a>\n' +
@ -33,11 +33,11 @@
' <span>-' + file.deletedLines + '</span>\n' +
' </td>\n' +
' <td class="d2h-file-name"><a href="#' + printerUtils.getHtmlId(file) + '">&nbsp;' + printerUtils.getDiffName(file) + '</a></td>\n' +
' </tr>\n'
' </tr>\n';
}).join('\n') +
'</table></div>\n';
};
module.exports['FileListPrinter'] = new FileListPrinter();
module.exports.FileListPrinter = new FileListPrinter();
})(this);
})();

View file

@ -5,7 +5,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var LineByLinePrinter = require('./line-by-line-printer.js').LineByLinePrinter;
var sideBySidePrinter = require('./side-by-side-printer.js').SideBySidePrinter;
@ -20,6 +20,6 @@
HtmlPrinter.prototype.generateSideBySideJsonHtml = sideBySidePrinter.generateSideBySideJsonHtml;
module.exports['HtmlPrinter'] = new HtmlPrinter();
module.exports.HtmlPrinter = new HtmlPrinter();
})(this);
})();

View file

@ -5,7 +5,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var diffParser = require('./diff-parser.js').DiffParser;
var printerUtils = require('./printer-utils.js').PrinterUtils;
@ -23,9 +23,9 @@
var diffs;
if (file.blocks.length) {
diffs = that.generateFileHtml(file);
diffs = that._generateFileHtml(file);
} else {
diffs = that.generateEmptyDiff();
diffs = that._generateEmptyDiff();
}
return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' +
@ -55,12 +55,13 @@
};
var matcher = Rematch.rematch(function(a, b) {
var amod = a.content.substr(1),
bmod = b.content.substr(1);
var amod = a.content.substr(1);
var bmod = b.content.substr(1);
return Rematch.distance(amod, bmod);
});
LineByLinePrinter.prototype.generateFileHtml = function(file) {
LineByLinePrinter.prototype._generateFileHtml = function(file) {
var that = this;
return file.blocks.map(function(block) {
@ -73,11 +74,14 @@
var oldLines = [];
var newLines = [];
function processChangeBlock() {
var matches;
var insertType;
var deleteType;
var doMatching = that.config.matching === "lines" || that.config.matching === "words";
var doMatching = that.config.matching === 'lines' || that.config.matching === 'words';
if (doMatching) {
matches = matcher(oldLines, newLines);
insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
@ -87,16 +91,18 @@
insertType = diffParser.LINE_TYPE.INSERTS;
deleteType = diffParser.LINE_TYPE.DELETES;
}
matches.forEach(function(match) {
var oldLines = match[0];
var newLines = match[1];
oldLines = match[0];
newLines = match[1];
var processedOldLines = [];
var processedNewLines = [];
var j = 0;
var oldLine, newLine,
common = Math.min(oldLines.length, newLines.length),
max = Math.max(oldLines.length, newLines.length);
for (j = 0; j < common; j++) {
var common = Math.min(oldLines.length, newLines.length);
var oldLine, newLine;
for (var j = 0; j < common; j++) {
oldLine = oldLines[j];
newLine = newLines[j];
@ -104,19 +110,17 @@
var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, that.config);
processedOldLines +=
that.generateLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber,
that._generateLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber,
diff.first.line, diff.first.prefix);
processedNewLines +=
that.generateLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
that._generateLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
diff.second.line, diff.second.prefix);
}
lines += processedOldLines + processedNewLines;
lines += that.processLines(oldLines.slice(common), newLines.slice(common));
processedOldLines = [];
processedNewLines = [];
lines += that._processLines(oldLines.slice(common), newLines.slice(common));
});
oldLines = [];
newLines = [];
}
@ -129,16 +133,17 @@
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
processChangeBlock();
}
if (line.type == diffParser.LINE_TYPE.CONTEXT) {
lines += that.generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
lines += that.generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type == diffParser.LINE_TYPE.DELETES) {
if (line.type === diffParser.LINE_TYPE.CONTEXT) {
lines += that._generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
lines += that._generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
} else if (line.type === diffParser.LINE_TYPE.DELETES) {
oldLines.push(line);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) {
} else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
newLines.push(line);
} else {
console.error('unknown state in html line-by-line generator');
console.error('Unknown state in html line-by-line generator');
processChangeBlock();
}
}
@ -147,27 +152,27 @@
return lines;
}).join('\n');
}
};
LineByLinePrinter.prototype.processLines = function(oldLines, newLines) {
LineByLinePrinter.prototype._processLines = function(oldLines, newLines) {
var lines = '';
for (j = 0; j < oldLines.length; j++) {
var oldLine = oldLines[j];
for (var i = 0; i < oldLines.length; i++) {
var oldLine = oldLines[i];
var oldEscapedLine = utils.escape(oldLine.content);
lines += this.generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine);
lines += this._generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine);
}
for (j = 0; j < newLines.length; j++) {
for (var j = 0; j < newLines.length; j++) {
var newLine = newLines[j];
var newEscapedLine = utils.escape(newLine.content);
lines += this.generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine);
lines += this._generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine);
}
return lines;
}
};
LineByLinePrinter.prototype.generateLineHtml = function(type, oldNumber, newNumber, content, prefix) {
LineByLinePrinter.prototype._generateLineHtml = function(type, oldNumber, newNumber, content, prefix) {
var htmlPrefix = '';
if (prefix) {
htmlPrefix = '<span class="d2h-code-line-prefix">' + prefix + '</span>';
@ -187,9 +192,9 @@
' <div class="d2h-code-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' +
' </td>\n' +
'</tr>\n';
}
};
LineByLinePrinter.prototype.generateEmptyDiff = function() {
LineByLinePrinter.prototype._generateEmptyDiff = function() {
return '<tr>\n' +
' <td class="' + diffParser.LINE_TYPE.INFO + '">' +
' <div class="d2h-code-line ' + diffParser.LINE_TYPE.INFO + '">' +
@ -197,8 +202,8 @@
' </div>' +
' </td>\n' +
'</tr>\n';
}
};
module.exports['LineByLinePrinter'] = LineByLinePrinter;
module.exports.LineByLinePrinter = LineByLinePrinter;
})(this);
})();

View file

@ -5,7 +5,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var jsDiff = require('diff');
var utils = require('./utils.js').Utils;
@ -16,38 +16,40 @@
PrinterUtils.prototype.getHtmlId = function(file) {
var hashCode = function(text) {
var hash = 0, i, chr, len;
if (text.length == 0) return hash;
var i, chr, len;
var hash = 0;
if (text.length === 0) return hash;
for (i = 0, len = text.length; i < len; i++) {
chr = text.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
return "d2h-" + hashCode(this.getDiffName(file)).toString().slice(-6);
return 'd2h-' + hashCode(this.getDiffName(file)).toString().slice(-6);
};
PrinterUtils.prototype.getDiffName = function(file) {
var oldFilename = file.oldName;
var newFilename = file.newName;
if (oldFilename && newFilename
&& oldFilename !== newFilename
&& !isDeletedName(newFilename)) {
if (oldFilename && newFilename && oldFilename !== newFilename && !isDeletedName(newFilename)) {
return oldFilename + ' -> ' + newFilename;
} else if (newFilename && !isDeletedName(newFilename)) {
return newFilename;
} else if (oldFilename) {
return oldFilename;
} else {
return 'Unknown filename';
}
return 'Unknown filename';
};
PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) {
var lineStart1, lineStart2;
var linePrefix1, linePrefix2, unprefixedLine1, unprefixedLine2;
var prefixSize = 1;
@ -55,17 +57,16 @@
prefixSize = 2;
}
lineStart1 = diffLine1.substr(0, prefixSize);
lineStart2 = diffLine2.substr(0, prefixSize);
diffLine1 = diffLine1.substr(prefixSize);
diffLine2 = diffLine2.substr(prefixSize);
linePrefix1 = diffLine1.substr(0, prefixSize);
linePrefix2 = diffLine2.substr(0, prefixSize);
unprefixedLine1 = diffLine1.substr(prefixSize);
unprefixedLine2 = diffLine2.substr(prefixSize);
var diff;
if (config.charByChar) {
diff = jsDiff.diffChars(diffLine1, diffLine2);
diff = jsDiff.diffChars(unprefixedLine1, unprefixedLine2);
} else {
diff = jsDiff.diffWordsWithSpace(diffLine1, diffLine2);
diff = jsDiff.diffWordsWithSpace(unprefixedLine1, unprefixedLine2);
}
var highlightedLine = '';
@ -73,25 +74,30 @@
var changedWords = [];
if (!config.charByChar && config.matching === 'words') {
var treshold = 0.25;
if (typeof(config.matchWordsThreshold) !== "undefined") {
if (typeof(config.matchWordsThreshold) !== 'undefined') {
treshold = config.matchWordsThreshold;
}
var matcher = Rematch.rematch(function(a, b) {
var amod = a.value,
bmod = b.value,
result = Rematch.distance(amod, bmod);
return result;
var amod = a.value;
var bmod = b.value;
return Rematch.distance(amod, bmod);
});
var removed = diff.filter(function isRemoved(element) {
return element.removed;
});
var added = diff.filter(function isAdded(element) {
return element.added;
});
var chunks = matcher(added, removed);
chunks = chunks.forEach(function(chunk){
chunks.forEach(function(chunk) {
if (chunk[0].length === 1 && chunk[1].length === 1) {
var dist = Rematch.distance(chunk[0][0].value, chunk[1][0].value)
var dist = Rematch.distance(chunk[0][0].value, chunk[1][0].value);
if (dist < treshold) {
changedWords.push(chunk[0][0]);
changedWords.push(chunk[1][0]);
@ -99,6 +105,7 @@
}
});
}
diff.forEach(function(part) {
var addClass = changedWords.indexOf(part) > -1 ? ' class="d2h-change"' : '';
var elemType = part.added ? 'ins' : part.removed ? 'del' : null;
@ -113,11 +120,11 @@
return {
first: {
prefix: lineStart1,
prefix: linePrefix1,
line: removeIns(highlightedLine)
},
second: {
prefix: lineStart2,
prefix: linePrefix2,
line: removeDel(highlightedLine)
}
}
@ -135,6 +142,6 @@
return line.replace(/(<del[^>]*>((.|\n)*?)<\/del>)/g, '');
}
module.exports['PrinterUtils'] = new PrinterUtils();
module.exports.PrinterUtils = new PrinterUtils();
})(this);
})();

View file

@ -6,7 +6,8 @@
*
*/
(function(ctx, undefined) {
(function() {
var Rematch = {};
Rematch.arrayToString = function arrayToString(a) {
if (Object.prototype.toString.apply(a, []) === "[object Array]") {
@ -14,7 +15,7 @@
} else {
return a;
}
}
};
/*
Copyright (c) 2011 Andrei Mackenzie
@ -52,26 +53,29 @@
}
}
}
return matrix[b.length][a.length];
}
Rematch.levenshtein = levenshtein;
Rematch.distance = function distance(x, y) {
x = x.trim();
y = y.trim();
var lev = levenshtein(x,y),
score = lev / (x.length + y.length);
var lev = levenshtein(x, y);
var score = lev / (x.length + y.length);
return score;
}
};
Rematch.rematch = function rematch(distanceFunction) {
function findBestMatch(a, b, cache) {
var cachecount = 0;
for (var key in cache) {
cachecount++;
}
var bestMatchDist = Infinity;
var bestMatch;
for (var i = 0; i < a.length; ++i) {
@ -90,21 +94,25 @@
}
}
}
return bestMatch;
}
function group(a, b, level, cache) {
function group(a, b, level, cache) {
if (typeof(cache) === "undefined") {
cache = {};
}
var minLength = Math.min(a.length, b.length);
var bm = findBestMatch(a, b, cache);
if (!level) {
level = 0;
}
if (!bm || (a.length + b.length < 3)) {
return [[a, b]];
}
var a1 = a.slice(0, bm.indexA),
b1 = b.slice(0, bm.indexB),
aMatch = [a[bm.indexA]],
@ -118,17 +126,21 @@
var groupMatch = group(aMatch, bMatch, level + 1, cache);
var group2 = group(a2, b2, level + 1, cache);
var result = groupMatch;
if (bm.indexA > 0 || bm.indexB > 0) {
result = group1.concat(result);
}
if (a.length > tailA || b.length > tailB) {
result = result.concat(group2);
}
return result;
}
return group;
}
};
module.exports['Rematch'] = Rematch;
module.exports.Rematch = Rematch;
})(this);
})();

View file

@ -5,7 +5,7 @@
*
*/
(function(ctx, undefined) {
(function() {
var diffParser = require('./diff-parser.js').DiffParser;
var printerUtils = require('./printer-utils.js').PrinterUtils;
@ -94,11 +94,13 @@
var oldLines = [];
var newLines = [];
function processChangeBlock() {
var matches;
var insertType;
var deleteType;
var doMatching = config.matching === "lines" || config.matching === "words";
var doMatching = config.matching === 'lines' || config.matching === 'words';
if (doMatching) {
matches = matcher(oldLines, newLines);
insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
@ -108,17 +110,17 @@
insertType = diffParser.LINE_TYPE.INSERTS;
deleteType = diffParser.LINE_TYPE.DELETES;
}
matches.forEach(function(match) {
var oldLines = match[0];
var newLines = match[1];
var tmpHtml;
var j = 0;
var oldLine, newLine,
common = Math.min(oldLines.length, newLines.length),
max = Math.max(oldLines.length, newLines.length);
for (j = 0; j < common; j++) {
oldLine = oldLines[j];
newLine = newLines[j];
oldLines = match[0];
newLines = match[1];
var common = Math.min(oldLines.length, newLines.length);
var max = Math.max(oldLines.length, newLines.length);
for (var j = 0; j < common; j++) {
var oldLine = oldLines[j];
var newLine = newLines[j];
config.isCombined = file.isCombined;
@ -131,17 +133,21 @@
generateSingleLineHtml(insertType, newLine.newNumber,
diff.second.line, diff.second.prefix);
}
if (max > common) {
var oldSlice = oldLines.slice(common),
newSlice = newLines.slice(common);
tmpHtml = processLines(oldLines.slice(common), newLines.slice(common));
var oldSlice = oldLines.slice(common);
var newSlice = newLines.slice(common);
var tmpHtml = processLines(oldSlice, newSlice);
fileHtml.left += tmpHtml.left;
fileHtml.right += tmpHtml.right;
}
});
oldLines = [];
newLines = [];
}
for (var i = 0; i < block.lines.length; i++) {
var line = block.lines[i];
var prefix = line[0];
@ -151,15 +157,16 @@
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
processChangeBlock();
}
if (line.type == diffParser.LINE_TYPE.CONTEXT) {
if (line.type === diffParser.LINE_TYPE.CONTEXT) {
fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine, prefix);
fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
} else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
fileHtml.left += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', '');
fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
} else if (line.type == diffParser.LINE_TYPE.DELETES) {
} else if (line.type === diffParser.LINE_TYPE.DELETES) {
oldLines.push(line);
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) {
} else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
newLines.push(line);
} else {
console.error('unknown state in html side-by-side generator');
@ -179,21 +186,24 @@
fileHtml.right = '';
var maxLinesNumber = Math.max(oldLines.length, newLines.length);
for (j = 0; j < maxLinesNumber; j++) {
var oldLine = oldLines[j];
var newLine = newLines[j];
for (var i = 0; i < maxLinesNumber; i++) {
var oldLine = oldLines[i];
var newLine = newLines[i];
var oldContent;
var newContent;
var oldPrefix;
var newPrefix;
if (oldLine) {
oldContent = utils.escape(oldLine.content.substr(1));
oldPrefix = oldLine.content[0];
}
if (newLine) {
newContent = utils.escape(newLine.content.substr(1));
newPrefix = newLine.content[0];
}
if (oldLine && newLine) {
fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, oldContent, oldPrefix);
fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, newContent, newPrefix);
@ -245,6 +255,6 @@
return fileHtml;
}
module.exports['SideBySidePrinter'] = new SideBySidePrinter();
module.exports.SideBySidePrinter = new SideBySidePrinter();
})(this);
})();

View file

@ -5,7 +5,7 @@
*
*/
(function(ctx, undefined) {
(function() {
function Utils() {
}
@ -19,7 +19,7 @@
};
Utils.prototype.getRandomId = function(prefix) {
return prefix + "-" + Math.random().toString(36).slice(-3);
return prefix + '-' + Math.random().toString(36).slice(-3);
};
Utils.prototype.startsWith = function(str, start) {
@ -41,6 +41,6 @@
return value ? value : '';
};
module.exports['Utils'] = new Utils();
module.exports.Utils = new Utils();
})(this);
})();