2015-04-11 23:34:33 +00:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* LineByLinePrinter (line-by-line-printer.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 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
|
|
|
|
|
|
|
|
function LineByLinePrinter() {
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles, config) {
|
|
|
|
|
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) {
|
|
|
|
|
diffs = generateFileHtml(file, config);
|
|
|
|
|
} else {
|
|
|
|
|
diffs = generateEmptyDiff();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-19 11:47:49 +00:00
|
|
|
return '<div id="' + printerUtils.getHtmlId(file) + '" class="d2h-file-wrapper" data-lang="' + file.language + '">\n' +
|
2015-08-08 00:11:35 +00:00
|
|
|
' <div class="d2h-file-header">\n' +
|
|
|
|
|
' <div class="d2h-file-stats">\n' +
|
2015-10-31 21:55:15 +00:00
|
|
|
' <span class="d2h-lines-added">' +
|
|
|
|
|
' <span>+' + file.addedLines + '</span>\n' +
|
|
|
|
|
' </span>\n' +
|
|
|
|
|
' <span class="d2h-lines-deleted">' +
|
|
|
|
|
' <span>-' + file.deletedLines + '</span>\n' +
|
|
|
|
|
' </span>\n' +
|
2015-08-08 00:11:35 +00:00
|
|
|
' </div>\n' +
|
|
|
|
|
' <div class="d2h-file-name">' + printerUtils.getDiffName(file) + '</div>\n' +
|
|
|
|
|
' </div>\n' +
|
|
|
|
|
' <div class="d2h-file-diff">\n' +
|
|
|
|
|
' <div class="d2h-code-wrapper">\n' +
|
|
|
|
|
' <table class="d2h-diff-table">\n' +
|
|
|
|
|
' <tbody class="d2h-diff-tbody">\n' +
|
|
|
|
|
' ' + diffs +
|
|
|
|
|
' </tbody>\n' +
|
|
|
|
|
' </table>\n' +
|
|
|
|
|
' </div>\n' +
|
|
|
|
|
' </div>\n' +
|
|
|
|
|
' </div>\n';
|
|
|
|
|
}).join('\n') +
|
|
|
|
|
'</div>\n';
|
2015-04-12 01:59:54 +00:00
|
|
|
};
|
|
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
var matcher=Rematch.rematch(function(a,b) {
|
|
|
|
|
var amod = a.content.substr(1),
|
|
|
|
|
bmod = b.content.substr(1);
|
|
|
|
|
return Rematch.distance(amod, bmod);
|
|
|
|
|
});
|
|
|
|
|
|
2015-04-12 10:32:37 +00:00
|
|
|
function generateFileHtml(file, config) {
|
2015-08-08 00:11:35 +00:00
|
|
|
return file.blocks.map(function(block) {
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
var lines = '<tr>\n' +
|
|
|
|
|
' <td class="d2h-code-linenumber ' + diffParser.LINE_TYPE.INFO + '"></td>\n' +
|
|
|
|
|
' <td class="' + diffParser.LINE_TYPE.INFO + '">' +
|
|
|
|
|
' <div class="d2h-code-line ' + diffParser.LINE_TYPE.INFO + '">' + utils.escape(block.header) + '</div>' +
|
|
|
|
|
' </td>\n' +
|
|
|
|
|
'</tr>\n';
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
var oldLines = [];
|
|
|
|
|
var newLines = [];
|
2015-11-25 16:37:26 +00:00
|
|
|
function processChangeBlock() {
|
|
|
|
|
var matches = matcher(oldLines, newLines);
|
|
|
|
|
matches.forEach(function(match){
|
|
|
|
|
var oldLines = match[0];
|
|
|
|
|
var newLines = match[1];
|
|
|
|
|
var processedOldLines = [];
|
|
|
|
|
var processedNewLines = [];
|
2015-04-12 01:59:54 +00:00
|
|
|
var j = 0;
|
2015-11-25 16:37:26 +00:00
|
|
|
var oldLine, newLine,
|
|
|
|
|
common = Math.min(oldLines.length, newLines.length),
|
|
|
|
|
max = Math.max(oldLines.length, newLines.length);
|
|
|
|
|
for (j = 0; j < common; j++) {
|
2015-04-12 01:59:54 +00:00
|
|
|
oldLine = oldLines[j];
|
|
|
|
|
newLine = newLines[j];
|
|
|
|
|
|
2015-04-19 23:24:19 +00:00
|
|
|
config.isCombined = file.isCombined;
|
2015-06-21 21:49:05 +00:00
|
|
|
var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, config);
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
processedOldLines +=
|
2015-12-06 21:45:58 +00:00
|
|
|
generateLineHtml(diffParser.LINE_TYPE.DELETE_CHANGES, oldLine.oldNumber, oldLine.newNumber,
|
2015-08-08 00:11:35 +00:00
|
|
|
diff.first.line, diff.first.prefix);
|
|
|
|
|
processedNewLines +=
|
2015-12-06 21:45:58 +00:00
|
|
|
generateLineHtml(diffParser.LINE_TYPE.INSERT_CHANGES, newLine.oldNumber, newLine.newNumber,
|
2015-08-08 00:11:35 +00:00
|
|
|
diff.second.line, diff.second.prefix);
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines += processedOldLines + processedNewLines;
|
2015-11-25 16:37:26 +00:00
|
|
|
lines += processLines(oldLines.slice(common), newLines.slice(common));
|
|
|
|
|
|
|
|
|
|
processedOldLines = [];
|
|
|
|
|
processedNewLines = [];
|
|
|
|
|
});
|
|
|
|
|
oldLines = [];
|
|
|
|
|
newLines = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < block.lines.length; i++) {
|
|
|
|
|
var line = block.lines[i];
|
|
|
|
|
var escapedLine = utils.escape(line.content);
|
|
|
|
|
|
|
|
|
|
if ( line.type !== diffParser.LINE_TYPE.INSERTS &&
|
|
|
|
|
(newLines.length > 0 || (line.type !== diffParser.LINE_TYPE.DELETES && oldLines > 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) {
|
|
|
|
|
oldLines.push(line);
|
|
|
|
|
} else if (line.type == diffParser.LINE_TYPE.INSERTS && !!oldLines.length) {
|
|
|
|
|
newLines.push(line);
|
|
|
|
|
} else {
|
|
|
|
|
console.error('unknown state in html line-by-line generator');
|
|
|
|
|
processChangeBlock();
|
2015-04-11 23:34:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
processChangeBlock();
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2015-04-12 01:59:54 +00:00
|
|
|
return lines;
|
2015-08-08 00:11:35 +00:00
|
|
|
}).join('\n');
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-19 23:24:19 +00:00
|
|
|
function processLines(oldLines, newLines) {
|
2015-08-08 00:11:35 +00:00
|
|
|
var lines = '';
|
2015-04-19 23:24:19 +00:00
|
|
|
|
|
|
|
|
for (j = 0; j < oldLines.length; j++) {
|
|
|
|
|
var oldLine = oldLines[j];
|
|
|
|
|
var oldEscapedLine = utils.escape(oldLine.content);
|
|
|
|
|
lines += generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-31 22:07:23 +00:00
|
|
|
function generateLineHtml(type, oldNumber, newNumber, 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>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '<tr>\n' +
|
|
|
|
|
' <td class="d2h-code-linenumber ' + type + '">' +
|
|
|
|
|
' <div class="line-num1">' + utils.valueOrEmpty(oldNumber) + '</div>' +
|
|
|
|
|
' <div class="line-num2">' + utils.valueOrEmpty(newNumber) + '</div>' +
|
|
|
|
|
' </td>\n' +
|
|
|
|
|
' <td class="' + type + '">' +
|
|
|
|
|
' <div class="d2h-code-line ' + type + '">' + htmlPrefix + htmlContent + '</div>' +
|
|
|
|
|
' </td>\n' +
|
|
|
|
|
'</tr>\n';
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-19 23:24:19 +00:00
|
|
|
function generateEmptyDiff() {
|
2015-08-08 00:11:35 +00:00
|
|
|
return '<tr>\n' +
|
|
|
|
|
' <td class="' + diffParser.LINE_TYPE.INFO + '">' +
|
|
|
|
|
' <div class="d2h-code-line ' + diffParser.LINE_TYPE.INFO + '">' +
|
|
|
|
|
'File without changes' +
|
|
|
|
|
' </div>' +
|
|
|
|
|
' </td>\n' +
|
|
|
|
|
'</tr>\n';
|
2015-04-19 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-08 00:11:35 +00:00
|
|
|
module.exports['LineByLinePrinter'] = new LineByLinePrinter();
|
2015-04-12 01:59:54 +00:00
|
|
|
|
|
|
|
|
})(this);
|