2015-04-11 23:34:33 +00:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* HtmlPrinter (html-printer.js)
|
|
|
|
|
* Author: rtfpessoa
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
(function() {
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
var diffParser = require('./diff-parser.js').DiffParser;
|
|
|
|
|
var printerUtils = require('./printer-utils.js').PrinterUtils;
|
|
|
|
|
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
|
|
|
|
2015-12-23 19:34:39 +00:00
|
|
|
var matcher = Rematch.rematch(function(a, b) {
|
|
|
|
|
var amod = a.content.substr(1);
|
|
|
|
|
var bmod = b.content.substr(1);
|
|
|
|
|
|
|
|
|
|
return Rematch.distance(amod, bmod);
|
|
|
|
|
});
|
|
|
|
|
|
2015-12-21 13:42:07 +00:00
|
|
|
function SideBySidePrinter(config) {
|
|
|
|
|
this.config = config;
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-30 21:55:33 +00:00
|
|
|
SideBySidePrinter.prototype.makeDiffHtml = function(file, diffs) {
|
|
|
|
|
return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' +
|
|
|
|
|
' <div class="d2h-file-header">\n' +
|
2016-02-21 16:46:07 +00:00
|
|
|
' <span class="d2h-file-name-wrapper">\n' +
|
|
|
|
|
' <span class="d2h-file-name">' + printerUtils.getDiffName(file) + '</span>\n' +
|
|
|
|
|
' </span>\n' +
|
2015-12-30 21:55:33 +00:00
|
|
|
' </div>\n' +
|
|
|
|
|
' <div class="d2h-files-diff">\n' +
|
|
|
|
|
' <div class="d2h-file-side-diff">\n' +
|
|
|
|
|
' <div class="d2h-code-wrapper">\n' +
|
|
|
|
|
' <table class="d2h-diff-table">\n' +
|
|
|
|
|
' <tbody class="d2h-diff-tbody">\n' +
|
|
|
|
|
' ' + diffs.left +
|
|
|
|
|
' </tbody>\n' +
|
|
|
|
|
' </table>\n' +
|
|
|
|
|
' </div>\n' +
|
|
|
|
|
' </div>\n' +
|
|
|
|
|
' <div class="d2h-file-side-diff">\n' +
|
|
|
|
|
' <div class="d2h-code-wrapper">\n' +
|
|
|
|
|
' <table class="d2h-diff-table">\n' +
|
|
|
|
|
' <tbody class="d2h-diff-tbody">\n' +
|
|
|
|
|
' ' + diffs.right +
|
|
|
|
|
' </tbody>\n' +
|
|
|
|
|
' </table>\n' +
|
|
|
|
|
' </div>\n' +
|
|
|
|
|
' </div>\n' +
|
|
|
|
|
' </div>\n' +
|
|
|
|
|
' </div>\n';
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-21 13:42:07 +00:00
|
|
|
SideBySidePrinter.prototype.generateSideBySideJsonHtml = function(diffFiles) {
|
2015-12-21 13:34:16 +00:00
|
|
|
var that = this;
|
2015-08-08 00:11:35 +00:00
|
|
|
return '<div class="d2h-wrapper">\n' +
|
|
|
|
|
diffFiles.map(function(file) {
|
2015-04-19 23:24:19 +00:00
|
|
|
|
|
|
|
|
var diffs;
|
2015-08-08 00:11:35 +00:00
|
|
|
if (file.blocks.length) {
|
2015-12-21 13:42:07 +00:00
|
|
|
diffs = that.generateSideBySideFileHtml(file);
|
2015-08-08 00:11:35 +00:00
|
|
|
} else {
|
2015-12-21 13:34:16 +00:00
|
|
|
diffs = that.generateEmptyDiff();
|
2015-08-08 00:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-30 21:55:33 +00:00
|
|
|
return that.makeDiffHtml(file, diffs);
|
2015-08-08 00:11:35 +00:00
|
|
|
}).join('\n') +
|
|
|
|
|
'</div>\n';
|
2015-04-12 01:59:54 +00:00
|
|
|
};
|
|
|
|
|
|
2015-12-30 21:55:33 +00:00
|
|
|
SideBySidePrinter.prototype.makeSideHtml = function(blockHeader) {
|
|
|
|
|
return '<tr>\n' +
|
|
|
|
|
' <td class="d2h-code-side-linenumber ' + diffParser.LINE_TYPE.INFO + '"></td>\n' +
|
|
|
|
|
' <td class="' + diffParser.LINE_TYPE.INFO + '">\n' +
|
2016-01-10 18:49:16 +00:00
|
|
|
' <div class="d2h-code-side-line ' + diffParser.LINE_TYPE.INFO + '">' + blockHeader + '</div>\n' +
|
2015-12-30 21:55:33 +00:00
|
|
|
' </td>\n' +
|
|
|
|
|
'</tr>\n';
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-21 13:42:07 +00:00
|
|
|
SideBySidePrinter.prototype.generateSideBySideFileHtml = function(file) {
|
2015-12-21 13:34:16 +00:00
|
|
|
var that = this;
|
2015-04-12 01:59:54 +00:00
|
|
|
var fileHtml = {};
|
2015-08-08 00:11:35 +00:00
|
|
|
fileHtml.left = '';
|
|
|
|
|
fileHtml.right = '';
|
|
|
|
|
|
|
|
|
|
file.blocks.forEach(function(block) {
|
|
|
|
|
|
2015-12-30 21:55:33 +00:00
|
|
|
fileHtml.left += that.makeSideHtml(utils.escape(block.header));
|
|
|
|
|
fileHtml.right += that.makeSideHtml('');
|
2015-08-08 00:11:35 +00:00
|
|
|
|
|
|
|
|
var oldLines = [];
|
|
|
|
|
var newLines = [];
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
function processChangeBlock() {
|
2015-12-09 22:17:26 +00:00
|
|
|
var matches;
|
|
|
|
|
var insertType;
|
|
|
|
|
var deleteType;
|
2016-04-14 11:01:25 +00:00
|
|
|
|
|
|
|
|
var comparisons = oldLines.length * newLines.length;
|
|
|
|
|
var maxComparisons = that.config.matchingMaxComparisons || 2500;
|
|
|
|
|
var doMatching = comparisons < maxComparisons && (that.config.matching === 'lines' ||
|
|
|
|
|
that.config.matching === 'words');
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-12-09 22:17:26 +00:00
|
|
|
if (doMatching) {
|
|
|
|
|
matches = matcher(oldLines, newLines);
|
|
|
|
|
insertType = diffParser.LINE_TYPE.INSERT_CHANGES;
|
|
|
|
|
deleteType = diffParser.LINE_TYPE.DELETE_CHANGES;
|
|
|
|
|
} else {
|
2015-12-20 22:22:58 +00:00
|
|
|
matches = [[oldLines, newLines]];
|
2015-12-09 22:17:26 +00:00
|
|
|
insertType = diffParser.LINE_TYPE.INSERTS;
|
|
|
|
|
deleteType = diffParser.LINE_TYPE.DELETES;
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
|
|
|
|
matches.forEach(function(match) {
|
|
|
|
|
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];
|
2015-11-25 16:37:26 +00:00
|
|
|
|
2015-12-21 13:42:07 +00:00
|
|
|
that.config.isCombined = file.isCombined;
|
2015-11-25 16:37:26 +00:00
|
|
|
|
2015-12-21 13:42:07 +00:00
|
|
|
var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, that.config);
|
2015-11-25 16:37:26 +00:00
|
|
|
|
|
|
|
|
fileHtml.left +=
|
2015-12-21 13:34:16 +00:00
|
|
|
that.generateSingleLineHtml(deleteType, oldLine.oldNumber,
|
2015-11-25 16:37:26 +00:00
|
|
|
diff.first.line, diff.first.prefix);
|
|
|
|
|
fileHtml.right +=
|
2015-12-21 13:34:16 +00:00
|
|
|
that.generateSingleLineHtml(insertType, newLine.newNumber,
|
2015-11-25 16:37:26 +00:00
|
|
|
diff.second.line, diff.second.prefix);
|
2015-12-20 22:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (max > common) {
|
|
|
|
|
var oldSlice = oldLines.slice(common);
|
|
|
|
|
var newSlice = newLines.slice(common);
|
|
|
|
|
|
2015-12-21 13:34:16 +00:00
|
|
|
var tmpHtml = that.processLines(oldSlice, newSlice);
|
2015-12-20 22:22:58 +00:00
|
|
|
fileHtml.left += tmpHtml.left;
|
|
|
|
|
fileHtml.right += tmpHtml.right;
|
|
|
|
|
}
|
2015-11-25 16:37:26 +00:00
|
|
|
});
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
oldLines = [];
|
|
|
|
|
newLines = [];
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
for (var i = 0; i < block.lines.length; i++) {
|
|
|
|
|
var line = block.lines[i];
|
2015-12-23 14:33:35 +00:00
|
|
|
var prefix = line.content[0];
|
2015-11-25 16:37:26 +00:00
|
|
|
var escapedLine = utils.escape(line.content.substr(1));
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
if (line.type !== diffParser.LINE_TYPE.INSERTS &&
|
|
|
|
|
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines.length > 0))) {
|
2015-11-25 16:37:26 +00:00
|
|
|
processChangeBlock();
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
|
|
|
|
if (line.type === diffParser.LINE_TYPE.CONTEXT) {
|
2015-12-21 13:34:16 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(line.type, line.oldNumber, escapedLine, prefix);
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
|
2015-12-20 22:22:58 +00:00
|
|
|
} else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
|
2015-12-21 13:34:16 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', '');
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(line.type, line.newNumber, escapedLine, prefix);
|
2015-12-20 22:22:58 +00:00
|
|
|
} else if (line.type === diffParser.LINE_TYPE.DELETES) {
|
2015-04-12 01:59:54 +00:00
|
|
|
oldLines.push(line);
|
2015-12-20 22:22:58 +00:00
|
|
|
} else if (line.type === diffParser.LINE_TYPE.INSERTS && Boolean(oldLines.length)) {
|
2015-04-12 01:59:54 +00:00
|
|
|
newLines.push(line);
|
2015-04-11 23:34:33 +00:00
|
|
|
} else {
|
2015-11-25 16:37:26 +00:00
|
|
|
console.error('unknown state in html side-by-side generator');
|
|
|
|
|
processChangeBlock();
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
2015-04-11 23:34:33 +00:00
|
|
|
}
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
processChangeBlock();
|
2015-04-12 01:59:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return fileHtml;
|
2015-12-21 13:34:16 +00:00
|
|
|
};
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-21 13:34:16 +00:00
|
|
|
SideBySidePrinter.prototype.processLines = function(oldLines, newLines) {
|
|
|
|
|
var that = this;
|
2015-04-19 23:24:19 +00:00
|
|
|
var fileHtml = {};
|
2015-08-08 00:11:35 +00:00
|
|
|
fileHtml.left = '';
|
|
|
|
|
fileHtml.right = '';
|
2015-04-19 23:24:19 +00:00
|
|
|
|
|
|
|
|
var maxLinesNumber = Math.max(oldLines.length, newLines.length);
|
2015-12-20 22:22:58 +00:00
|
|
|
for (var i = 0; i < maxLinesNumber; i++) {
|
|
|
|
|
var oldLine = oldLines[i];
|
|
|
|
|
var newLine = newLines[i];
|
2015-11-25 16:37:26 +00:00
|
|
|
var oldContent;
|
|
|
|
|
var newContent;
|
|
|
|
|
var oldPrefix;
|
|
|
|
|
var newPrefix;
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
if (oldLine) {
|
|
|
|
|
oldContent = utils.escape(oldLine.content.substr(1));
|
|
|
|
|
oldPrefix = oldLine.content[0];
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
if (newLine) {
|
|
|
|
|
newContent = utils.escape(newLine.content.substr(1));
|
|
|
|
|
newPrefix = newLine.content[0];
|
|
|
|
|
}
|
2015-12-20 22:22:58 +00:00
|
|
|
|
2015-04-19 23:24:19 +00:00
|
|
|
if (oldLine && newLine) {
|
2015-12-21 13:34:16 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(oldLine.type, oldLine.oldNumber, oldContent, oldPrefix);
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(newLine.type, newLine.newNumber, newContent, newPrefix);
|
2015-04-19 23:24:19 +00:00
|
|
|
} else if (oldLine) {
|
2015-12-21 13:34:16 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(oldLine.type, oldLine.oldNumber, oldContent, oldPrefix);
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', '');
|
2015-04-19 23:24:19 +00:00
|
|
|
} else if (newLine) {
|
2015-12-21 13:34:16 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, '', '', '');
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(newLine.type, newLine.newNumber, newContent, newPrefix);
|
2015-04-19 23:24:19 +00:00
|
|
|
} else {
|
2015-08-08 00:11:35 +00:00
|
|
|
console.error('How did it get here?');
|
2015-04-19 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileHtml;
|
2015-12-21 13:34:16 +00:00
|
|
|
};
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2015-12-30 21:55:33 +00:00
|
|
|
SideBySidePrinter.prototype.makeSingleLineHtml = function(type, number, htmlContent, htmlPrefix) {
|
|
|
|
|
return '<tr>\n' +
|
|
|
|
|
' <td class="d2h-code-side-linenumber ' + type + '">' + number + '</td>\n' +
|
|
|
|
|
' <td class="' + type + '">' +
|
|
|
|
|
' <div class="d2h-code-side-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' +
|
|
|
|
|
' </td>\n' +
|
|
|
|
|
' </tr>\n';
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-21 13:34:16 +00:00
|
|
|
SideBySidePrinter.prototype.generateSingleLineHtml = function(type, number, content, prefix) {
|
2015-08-08 00:11:35 +00:00
|
|
|
var htmlPrefix = '';
|
|
|
|
|
if (prefix) {
|
|
|
|
|
htmlPrefix = '<span class="d2h-code-line-prefix">' + prefix + '</span>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var htmlContent = '';
|
|
|
|
|
if (content) {
|
|
|
|
|
htmlContent = '<span class="d2h-code-line-ctn">' + content + '</span>';
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 21:55:33 +00:00
|
|
|
return this.makeSingleLineHtml(type, number, htmlContent, htmlPrefix);
|
2015-12-21 13:34:16 +00:00
|
|
|
};
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-21 13:34:16 +00:00
|
|
|
SideBySidePrinter.prototype.generateEmptyDiff = function() {
|
2015-04-19 23:24:19 +00:00
|
|
|
var fileHtml = {};
|
2015-08-08 00:11:35 +00:00
|
|
|
fileHtml.right = '';
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
fileHtml.left = '<tr>\n' +
|
|
|
|
|
' <td class="' + diffParser.LINE_TYPE.INFO + '">' +
|
|
|
|
|
' <div class="d2h-code-side-line ' + diffParser.LINE_TYPE.INFO + '">' +
|
|
|
|
|
'File without changes' +
|
|
|
|
|
' </div>' +
|
|
|
|
|
' </td>\n' +
|
|
|
|
|
'</tr>\n';
|
2015-04-19 23:24:19 +00:00
|
|
|
|
|
|
|
|
return fileHtml;
|
2015-12-21 13:34:16 +00:00
|
|
|
};
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2015-12-21 13:42:07 +00:00
|
|
|
module.exports.SideBySidePrinter = SideBySidePrinter;
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
})();
|