2015-04-11 23:34:33 +00:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* PrinterUtils (printer-utils.js)
|
|
|
|
|
* Author: rtfpessoa
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
(function (global, undefined) {
|
|
|
|
|
|
|
|
|
|
// dirty hack for browser compatibility
|
|
|
|
|
var jsDiff = (typeof JsDiff !== "undefined" && JsDiff) || require("../lib/diff.js");
|
|
|
|
|
|
|
|
|
|
function PrinterUtils() {
|
2015-04-11 23:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-19 23:24:19 +00:00
|
|
|
PrinterUtils.prototype.getDiffName = function (file) {
|
|
|
|
|
var oldFilename = file.oldName;
|
|
|
|
|
var newFilename = file.newName;
|
|
|
|
|
|
|
|
|
|
if (oldFilename && newFilename
|
|
|
|
|
&& oldFilename !== newFilename
|
|
|
|
|
&& !isDeletedName(newFilename)) {
|
2015-04-12 01:59:54 +00:00
|
|
|
return oldFilename + " -> " + newFilename;
|
2015-04-19 23:24:19 +00:00
|
|
|
} else if (newFilename && !isDeletedName(newFilename)) {
|
2015-04-12 01:59:54 +00:00
|
|
|
return newFilename;
|
|
|
|
|
} else if (oldFilename) {
|
|
|
|
|
return oldFilename;
|
|
|
|
|
} else {
|
|
|
|
|
return "Unknown filename";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-12 10:32:37 +00:00
|
|
|
PrinterUtils.prototype.diffHighlight = function (diffLine1, diffLine2, config) {
|
2015-04-12 01:59:54 +00:00
|
|
|
var lineStart1, lineStart2;
|
|
|
|
|
|
|
|
|
|
var prefixSize = 1;
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-19 23:24:19 +00:00
|
|
|
if (config.isCombined) prefixSize = 2;
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
lineStart1 = diffLine1.substr(0, prefixSize);
|
|
|
|
|
lineStart2 = diffLine2.substr(0, prefixSize);
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
diffLine1 = diffLine1.substr(prefixSize);
|
|
|
|
|
diffLine2 = diffLine2.substr(prefixSize);
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 10:32:37 +00:00
|
|
|
var diff;
|
|
|
|
|
if (config.charByChar) diff = jsDiff.diffChars(diffLine1, diffLine2);
|
|
|
|
|
else diff = jsDiff.diffWordsWithSpace(diffLine1, diffLine2);
|
|
|
|
|
|
|
|
|
|
//var diff = jsDiff.diffChars(diffLine1, diffLine2);
|
|
|
|
|
//var diff = jsDiff.diffWordsWithSpace(diffLine1, diffLine2);
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
var highlightedLine = "";
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
diff.forEach(function (part) {
|
|
|
|
|
var elemType = part.added ? 'ins' : part.removed ? 'del' : null;
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
if (elemType !== null) highlightedLine += "<" + elemType + ">" + part.value + "</" + elemType + ">";
|
|
|
|
|
else highlightedLine += part.value;
|
|
|
|
|
});
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
return {
|
|
|
|
|
o: lineStart1 + removeIns(highlightedLine),
|
|
|
|
|
n: lineStart2 + removeDel(highlightedLine)
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-19 23:24:19 +00:00
|
|
|
function isDeletedName(name) {
|
|
|
|
|
return name === "dev/null";
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
function removeIns(line) {
|
|
|
|
|
return line.replace(/(<ins>((.|\n)*?)<\/ins>)/g, "");
|
2015-04-11 23:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
function removeDel(line) {
|
|
|
|
|
return line.replace(/(<del>((.|\n)*?)<\/del>)/g, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined' && module.exports) {
|
|
|
|
|
module.exports.PrinterUtils = new PrinterUtils();
|
|
|
|
|
} else if (typeof global.PrinterUtils === 'undefined') {
|
|
|
|
|
global.PrinterUtils = new PrinterUtils();
|
|
|
|
|
}
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
})(this);
|