2015-04-11 23:34:33 +00:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* PrinterUtils (printer-utils.js)
|
|
|
|
|
* Author: rtfpessoa
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
(function() {
|
2015-08-08 00:11:35 +00:00
|
|
|
var jsDiff = require('diff');
|
|
|
|
|
var utils = require('./utils.js').Utils;
|
2015-11-25 16:37:26 +00:00
|
|
|
var Rematch = require('./rematch.js').Rematch;
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2016-05-20 22:17:03 +00:00
|
|
|
var separator = '/';
|
|
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
function PrinterUtils() {
|
2015-04-11 23:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-05 21:13:51 +00:00
|
|
|
PrinterUtils.prototype.separatePrefix = function(isCombined, line) {
|
|
|
|
|
var prefix;
|
|
|
|
|
var lineWithoutPrefix;
|
|
|
|
|
|
|
|
|
|
if (isCombined) {
|
|
|
|
|
prefix = line.substring(0, 2);
|
|
|
|
|
lineWithoutPrefix = line.substring(2);
|
|
|
|
|
} else {
|
|
|
|
|
prefix = line.substring(0, 1);
|
|
|
|
|
lineWithoutPrefix = line.substring(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'prefix': prefix,
|
|
|
|
|
'line': lineWithoutPrefix
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-19 11:47:49 +00:00
|
|
|
PrinterUtils.prototype.getHtmlId = function(file) {
|
2015-12-20 22:22:58 +00:00
|
|
|
var hashCode = function(text) {
|
|
|
|
|
var i, chr, len;
|
|
|
|
|
var hash = 0;
|
|
|
|
|
|
2015-10-19 14:43:16 +00:00
|
|
|
for (i = 0, len = text.length; i < len; i++) {
|
2015-12-20 22:22:58 +00:00
|
|
|
chr = text.charCodeAt(i);
|
|
|
|
|
hash = ((hash << 5) - hash) + chr;
|
2015-10-19 14:43:16 +00:00
|
|
|
hash |= 0; // Convert to 32bit integer
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-10-19 14:43:16 +00:00
|
|
|
return hash;
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
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) {
|
2016-05-20 22:17:03 +00:00
|
|
|
var oldFilename = unifyPath(file.oldName);
|
|
|
|
|
var newFilename = unifyPath(file.newName);
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2016-04-25 18:24:35 +00:00
|
|
|
if (oldFilename && newFilename && oldFilename !== newFilename && !isDevNullName(oldFilename) && !isDevNullName(newFilename)) {
|
2016-05-20 22:17:03 +00:00
|
|
|
var prefixPaths = [];
|
|
|
|
|
var suffixPaths = [];
|
|
|
|
|
|
|
|
|
|
var oldFilenameParts = oldFilename.split(separator);
|
|
|
|
|
var newFilenameParts = newFilename.split(separator);
|
|
|
|
|
|
|
|
|
|
var oldFilenamePartsSize = oldFilenameParts.length;
|
|
|
|
|
var newFilenamePartsSize = newFilenameParts.length;
|
|
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
|
var j = oldFilenamePartsSize - 1;
|
|
|
|
|
var k = newFilenamePartsSize - 1;
|
|
|
|
|
|
|
|
|
|
while (i < j && i < k) {
|
|
|
|
|
if (oldFilenameParts[i] === newFilenameParts[i]) {
|
|
|
|
|
prefixPaths.push(newFilenameParts[i]);
|
2016-05-20 22:25:10 +00:00
|
|
|
i += 1;
|
2016-05-20 22:17:03 +00:00
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (j > i && k > i) {
|
|
|
|
|
if (oldFilenameParts[j] === newFilenameParts[k]) {
|
|
|
|
|
suffixPaths.unshift(newFilenameParts[k]);
|
2016-05-20 22:25:10 +00:00
|
|
|
j -= 1;
|
|
|
|
|
k -= 1;
|
2016-05-20 22:17:03 +00:00
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var finalPrefix = prefixPaths.join(separator);
|
|
|
|
|
var finalSuffix = suffixPaths.join(separator);
|
|
|
|
|
|
|
|
|
|
var oldRemainingPath = oldFilenameParts.slice(i, j + 1).join(separator);
|
|
|
|
|
var newRemainingPath = newFilenameParts.slice(i, k + 1).join(separator);
|
|
|
|
|
|
|
|
|
|
if (finalPrefix.length && finalSuffix.length) {
|
|
|
|
|
return finalPrefix + separator + '{' + oldRemainingPath + ' → ' + newRemainingPath + '}' + separator + finalSuffix;
|
|
|
|
|
} else if (finalPrefix.length) {
|
|
|
|
|
return finalPrefix + separator + '{' + oldRemainingPath + ' → ' + newRemainingPath + '}';
|
|
|
|
|
} else if (finalSuffix.length) {
|
|
|
|
|
return '{' + oldRemainingPath + ' → ' + newRemainingPath + '}' + separator + finalSuffix;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 22:25:10 +00:00
|
|
|
return oldFilename + ' → ' + newFilename;
|
2016-03-20 23:33:48 +00:00
|
|
|
} else if (newFilename && !isDevNullName(newFilename)) {
|
2015-04-12 01:59:54 +00:00
|
|
|
return newFilename;
|
|
|
|
|
} else if (oldFilename) {
|
|
|
|
|
return oldFilename;
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2016-05-20 22:17:03 +00:00
|
|
|
return 'unknown/file/path';
|
2015-04-12 01:59:54 +00:00
|
|
|
};
|
|
|
|
|
|
2016-05-15 22:43:51 +00:00
|
|
|
PrinterUtils.prototype.getFileTypeIcon = function(file) {
|
|
|
|
|
var templateName = 'file-changed';
|
|
|
|
|
|
|
|
|
|
if (file.isRename) {
|
|
|
|
|
templateName = 'file-renamed';
|
|
|
|
|
} else if (file.isCopy) {
|
|
|
|
|
templateName = 'file-renamed';
|
|
|
|
|
} else if (file.isNew) {
|
|
|
|
|
templateName = 'file-added';
|
|
|
|
|
} else if (file.isDeleted) {
|
|
|
|
|
templateName = 'file-deleted';
|
|
|
|
|
} else if (file.newName !== file.oldName) {
|
|
|
|
|
// If file is not Added, not Deleted and the names changed it must be a rename :)
|
|
|
|
|
templateName = 'file-renamed';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return templateName;
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) {
|
2015-12-20 22:22:58 +00:00
|
|
|
var linePrefix1, linePrefix2, unprefixedLine1, unprefixedLine2;
|
2015-04-12 01:59:54 +00:00
|
|
|
|
|
|
|
|
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-12-20 22:22:58 +00:00
|
|
|
linePrefix1 = diffLine1.substr(0, prefixSize);
|
|
|
|
|
linePrefix2 = diffLine2.substr(0, prefixSize);
|
|
|
|
|
unprefixedLine1 = diffLine1.substr(prefixSize);
|
|
|
|
|
unprefixedLine2 = 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) {
|
2015-12-20 22:22:58 +00:00
|
|
|
diff = jsDiff.diffChars(unprefixedLine1, unprefixedLine2);
|
2015-08-08 00:11:35 +00:00
|
|
|
} else {
|
2015-12-20 22:22:58 +00:00
|
|
|
diff = jsDiff.diffWordsWithSpace(unprefixedLine1, unprefixedLine2);
|
2015-08-08 00:11:35 +00:00
|
|
|
}
|
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-11-25 16:37:26 +00:00
|
|
|
var changedWords = [];
|
2015-12-09 22:17:26 +00:00
|
|
|
if (!config.charByChar && config.matching === 'words') {
|
2015-11-25 16:37:26 +00:00
|
|
|
var treshold = 0.25;
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-12-23 19:34:39 +00:00
|
|
|
if (typeof (config.matchWordsThreshold) !== 'undefined') {
|
2015-11-25 16:37:26 +00:00
|
|
|
treshold = config.matchWordsThreshold;
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
var matcher = Rematch.rematch(function(a, b) {
|
2015-12-20 22:22:58 +00:00
|
|
|
var amod = a.value;
|
|
|
|
|
var bmod = b.value;
|
|
|
|
|
|
|
|
|
|
return Rematch.distance(amod, bmod);
|
2015-11-25 16:37:26 +00:00
|
|
|
});
|
2015-12-20 22:22:58 +00:00
|
|
|
|
|
|
|
|
var removed = diff.filter(function isRemoved(element) {
|
2015-11-25 16:37:26 +00:00
|
|
|
return element.removed;
|
|
|
|
|
});
|
2015-12-20 22:22:58 +00:00
|
|
|
|
|
|
|
|
var added = diff.filter(function isAdded(element) {
|
2015-11-25 16:37:26 +00:00
|
|
|
return element.added;
|
|
|
|
|
});
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
var chunks = matcher(added, removed);
|
2015-12-20 22:22:58 +00:00
|
|
|
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);
|
2015-11-25 16:37:26 +00:00
|
|
|
if (dist < treshold) {
|
|
|
|
|
changedWords.push(chunk[0][0]);
|
|
|
|
|
changedWords.push(chunk[1][0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
diff.forEach(function(part) {
|
2015-11-25 16:37:26 +00:00
|
|
|
var addClass = changedWords.indexOf(part) > -1 ? ' class="d2h-change"' : '';
|
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) {
|
2015-11-25 16:37:26 +00:00
|
|
|
highlightedLine += '<' + elemType + addClass + '>' + escapedValue + '</' + elemType + '>';
|
2015-08-08 00:11:35 +00:00
|
|
|
} 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: {
|
2015-12-20 22:22:58 +00:00
|
|
|
prefix: linePrefix1,
|
2015-05-31 22:07:23 +00:00
|
|
|
line: removeIns(highlightedLine)
|
|
|
|
|
},
|
|
|
|
|
second: {
|
2015-12-20 22:22:58 +00:00
|
|
|
prefix: linePrefix2,
|
2015-05-31 22:07:23 +00:00
|
|
|
line: removeDel(highlightedLine)
|
|
|
|
|
}
|
2015-12-20 23:38:16 +00:00
|
|
|
};
|
2015-04-12 01:59:54 +00:00
|
|
|
};
|
2015-04-11 23:34:33 +00:00
|
|
|
|
2016-05-20 22:17:03 +00:00
|
|
|
function unifyPath(path) {
|
|
|
|
|
if (path) {
|
|
|
|
|
return path.replace('\\', '/');
|
|
|
|
|
}
|
2016-05-20 22:25:10 +00:00
|
|
|
|
|
|
|
|
return path;
|
2016-05-20 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-20 23:33:48 +00:00
|
|
|
function isDevNullName(name) {
|
|
|
|
|
return name.indexOf('dev/null') !== -1;
|
2015-04-19 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
function removeIns(line) {
|
2015-11-25 16:37:26 +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-11-25 16:37:26 +00:00
|
|
|
return line.replace(/(<del[^>]*>((.|\n)*?)<\/del>)/g, '');
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
module.exports.PrinterUtils = new PrinterUtils();
|
|
|
|
|
})();
|