2015-04-11 23:34:33 +00:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* PrinterUtils (printer-utils.js)
|
|
|
|
|
* Author: rtfpessoa
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
(function(ctx, undefined) {
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
var jsDiff = require('diff');
|
|
|
|
|
var utils = require('./utils.js').Utils;
|
2015-04-12 01:59:54 +00:00
|
|
|
|
|
|
|
|
function PrinterUtils() {
|
2015-04-11 23:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-19 11:47:49 +00:00
|
|
|
PrinterUtils.prototype.getHtmlId = function(file) {
|
2015-10-19 14:43:16 +00:00
|
|
|
var hashCode = function(text) {
|
|
|
|
|
var hash = 0, i, chr, len;
|
|
|
|
|
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);
|
2015-10-19 11:47:49 +00:00
|
|
|
};
|
|
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
PrinterUtils.prototype.getDiffName = function(file) {
|
2015-04-19 23:24:19 +00:00
|
|
|
var oldFilename = file.oldName;
|
|
|
|
|
var newFilename = file.newName;
|
|
|
|
|
|
|
|
|
|
if (oldFilename && newFilename
|
|
|
|
|
&& oldFilename !== newFilename
|
|
|
|
|
&& !isDeletedName(newFilename)) {
|
2015-08-08 00:11:35 +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 {
|
2015-08-08 00:11:35 +00:00
|
|
|
return 'Unknown filename';
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-08 00:11:35 +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-08-08 00:11:35 +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;
|
2015-08-08 00:11:35 +00:00
|
|
|
if (config.charByChar) {
|
|
|
|
|
diff = jsDiff.diffChars(diffLine1, diffLine2);
|
|
|
|
|
} else {
|
|
|
|
|
diff = jsDiff.diffWordsWithSpace(diffLine1, diffLine2);
|
|
|
|
|
}
|
2015-04-12 10:32:37 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
var highlightedLine = '';
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
diff.forEach(function(part) {
|
2015-04-12 01:59:54 +00:00
|
|
|
var elemType = part.added ? 'ins' : part.removed ? 'del' : null;
|
2015-06-21 21:49:05 +00:00
|
|
|
var escapedValue = utils.escape(part.value);
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
if (elemType !== null) {
|
|
|
|
|
highlightedLine += '<' + elemType + '>' + escapedValue + '</' + elemType + '>';
|
|
|
|
|
} else {
|
|
|
|
|
highlightedLine += escapedValue;
|
|
|
|
|
}
|
2015-04-12 01:59:54 +00:00
|
|
|
});
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
return {
|
2015-05-31 22:07:23 +00:00
|
|
|
first: {
|
|
|
|
|
prefix: lineStart1,
|
|
|
|
|
line: removeIns(highlightedLine)
|
|
|
|
|
},
|
|
|
|
|
second: {
|
|
|
|
|
prefix: lineStart2,
|
|
|
|
|
line: removeDel(highlightedLine)
|
|
|
|
|
}
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
};
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-19 23:24:19 +00:00
|
|
|
function isDeletedName(name) {
|
2015-08-08 00:11:35 +00:00
|
|
|
return name === 'dev/null';
|
2015-04-19 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
function removeIns(line) {
|
2015-08-08 00:11:35 +00:00
|
|
|
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) {
|
2015-08-08 00:11:35 +00:00
|
|
|
return line.replace(/(<del>((.|\n)*?)<\/del>)/g, '');
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
module.exports['PrinterUtils'] = new PrinterUtils();
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
})(this);
|