2015-04-11 23:34:33 +00:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* LineByLinePrinter (line-by-line-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;
|
2016-01-17 13:49:11 +00:00
|
|
|
var nunjucks = require('nunjucks');
|
|
|
|
|
var nunjucksEnv = nunjucks.configure(__dirname + '/templates/line-by-line/')
|
|
|
|
|
.addGlobal('printerUtils', printerUtils)
|
|
|
|
|
.addGlobal('utils', utils)
|
|
|
|
|
.addGlobal('diffParser', diffParser);
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-20 14:56:53 +00:00
|
|
|
function LineByLinePrinter(config) {
|
|
|
|
|
this.config = config;
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-27 10:08:50 +00:00
|
|
|
LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) {
|
2016-01-17 13:49:11 +00:00
|
|
|
return nunjucksEnv.render('file-diff.html', {'file': file, 'diffs': diffs});
|
2015-12-27 10:08:50 +00:00
|
|
|
};
|
|
|
|
|
|
2016-01-28 15:03:20 +00:00
|
|
|
LineByLinePrinter.prototype.makeLineByLineHtmlWrapper = function(content) {
|
|
|
|
|
return nunjucksEnv.render('wrapper.html', {'content': content});
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-20 14:56:53 +00:00
|
|
|
LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles) {
|
2015-12-20 22:42:37 +00:00
|
|
|
var that = this;
|
2015-12-27 10:08:50 +00:00
|
|
|
var htmlDiffs = 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-20 22:22:58 +00:00
|
|
|
diffs = that._generateFileHtml(file);
|
2015-08-08 00:11:35 +00:00
|
|
|
} else {
|
2015-12-20 22:22:58 +00:00
|
|
|
diffs = that._generateEmptyDiff();
|
2015-08-08 00:11:35 +00:00
|
|
|
}
|
2015-12-27 10:08:50 +00:00
|
|
|
return that.makeFileDiffHtml(file, diffs);
|
|
|
|
|
});
|
2015-08-08 00:11:35 +00:00
|
|
|
|
2016-01-28 15:03:20 +00:00
|
|
|
return this.makeLineByLineHtmlWrapper(htmlDiffs.join('\n'));
|
2015-04-12 01:59:54 +00:00
|
|
|
};
|
|
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
var matcher = Rematch.rematch(function(a, b) {
|
|
|
|
|
var amod = a.content.substr(1);
|
|
|
|
|
var bmod = b.content.substr(1);
|
|
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
return Rematch.distance(amod, bmod);
|
|
|
|
|
});
|
|
|
|
|
|
2015-12-27 10:08:50 +00:00
|
|
|
LineByLinePrinter.prototype.makeColumnLineNumberHtml = function(block) {
|
2016-01-17 13:49:11 +00:00
|
|
|
return nunjucksEnv.render('column-line-number.html', {block: block});
|
2015-12-27 10:08:50 +00:00
|
|
|
};
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-27 10:08:50 +00:00
|
|
|
LineByLinePrinter.prototype._generateFileHtml = function(file) {
|
|
|
|
|
var that = this;
|
|
|
|
|
return file.blocks.map(function(block) {
|
|
|
|
|
|
|
|
|
|
var lines = that.makeColumnLineNumberHtml(block);
|
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;
|
2015-12-20 22:22:58 +00:00
|
|
|
|
|
|
|
|
var doMatching = that.config.matching === 'lines' || that.config.matching === 'words';
|
|
|
|
|
|
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];
|
|
|
|
|
|
2015-11-25 16:37:26 +00:00
|
|
|
var processedOldLines = [];
|
|
|
|
|
var processedNewLines = [];
|
2015-12-20 22:22:58 +00:00
|
|
|
|
|
|
|
|
var common = Math.min(oldLines.length, newLines.length);
|
|
|
|
|
|
|
|
|
|
var oldLine, newLine;
|
|
|
|
|
for (var j = 0; j < common; j++) {
|
|
|
|
|
oldLine = oldLines[j];
|
|
|
|
|
newLine = newLines[j];
|
|
|
|
|
|
|
|
|
|
that.config.isCombined = file.isCombined;
|
|
|
|
|
var diff = printerUtils.diffHighlight(oldLine.content, newLine.content, that.config);
|
|
|
|
|
|
|
|
|
|
processedOldLines +=
|
2016-01-17 13:49:11 +00:00
|
|
|
that.makeLineHtml(deleteType, oldLine.oldNumber, oldLine.newNumber,
|
2015-12-20 22:22:58 +00:00
|
|
|
diff.first.line, diff.first.prefix);
|
|
|
|
|
processedNewLines +=
|
2016-01-17 13:49:11 +00:00
|
|
|
that.makeLineHtml(insertType, newLine.oldNumber, newLine.newNumber,
|
2015-12-20 22:22:58 +00:00
|
|
|
diff.second.line, diff.second.prefix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines += processedOldLines + processedNewLines;
|
|
|
|
|
lines += that._processLines(oldLines.slice(common), newLines.slice(common));
|
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 = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < block.lines.length; i++) {
|
|
|
|
|
var line = block.lines[i];
|
|
|
|
|
var escapedLine = utils.escape(line.content);
|
|
|
|
|
|
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) {
|
2016-01-17 13:49:11 +00:00
|
|
|
lines += that.makeLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
|
2015-12-20 22:22:58 +00:00
|
|
|
} else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
|
2016-01-17 13:49:11 +00:00
|
|
|
lines += that.makeLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
|
2015-12-20 22:22:58 +00:00
|
|
|
} else if (line.type === diffParser.LINE_TYPE.DELETES) {
|
2015-11-25 16:37:26 +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-11-25 16:37:26 +00:00
|
|
|
newLines.push(line);
|
|
|
|
|
} else {
|
2015-12-20 22:22:58 +00:00
|
|
|
console.error('Unknown state in html line-by-line generator');
|
2015-11-25 16:37:26 +00:00
|
|
|
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-12-20 22:22:58 +00:00
|
|
|
};
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
LineByLinePrinter.prototype._processLines = function(oldLines, newLines) {
|
2015-08-08 00:11:35 +00:00
|
|
|
var lines = '';
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
for (var i = 0; i < oldLines.length; i++) {
|
|
|
|
|
var oldLine = oldLines[i];
|
2015-04-19 23:24:19 +00:00
|
|
|
var oldEscapedLine = utils.escape(oldLine.content);
|
2016-01-17 13:49:11 +00:00
|
|
|
lines += this.makeLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine);
|
2015-04-19 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
for (var j = 0; j < newLines.length; j++) {
|
2015-04-19 23:24:19 +00:00
|
|
|
var newLine = newLines[j];
|
|
|
|
|
var newEscapedLine = utils.escape(newLine.content);
|
2016-01-17 13:49:11 +00:00
|
|
|
lines += this.makeLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine);
|
2015-04-19 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines;
|
2015-12-20 22:22:58 +00:00
|
|
|
};
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2016-01-17 13:49:11 +00:00
|
|
|
LineByLinePrinter.prototype.makeLineHtml = function(type, oldNumber, newNumber, content, prefix) {
|
|
|
|
|
return nunjucksEnv.render('line.html',
|
|
|
|
|
{type: type,
|
|
|
|
|
oldNumber: oldNumber,
|
|
|
|
|
newNumber: newNumber,
|
|
|
|
|
prefix: prefix,
|
|
|
|
|
content: content});
|
2015-12-20 22:22:58 +00:00
|
|
|
};
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
LineByLinePrinter.prototype._generateEmptyDiff = function() {
|
2016-01-17 13:49:11 +00:00
|
|
|
return nunjucksEnv.render('empty-diff.html', {});
|
2015-12-20 22:22:58 +00:00
|
|
|
};
|
2015-04-19 23:24:19 +00:00
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
module.exports.LineByLinePrinter = LineByLinePrinter;
|
2015-04-12 01:59:54 +00:00
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
})();
|