2015-04-11 23:34:33 +00:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* HtmlPrinter (html-printer.js)
|
|
|
|
|
* Author: rtfpessoa
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-20 22:22:58 +00:00
|
|
|
(function() {
|
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
|
|
|
|
2016-10-09 15:41:54 +00:00
|
|
|
var hoganUtils;
|
|
|
|
|
|
2016-05-20 23:33:22 +00:00
|
|
|
var genericTemplatesPath = 'generic';
|
|
|
|
|
var baseTemplatesPath = 'side-by-side';
|
|
|
|
|
var iconsBaseTemplatesPath = 'icon';
|
|
|
|
|
var tagsBaseTemplatesPath = 'tag';
|
|
|
|
|
|
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;
|
2016-10-09 15:41:54 +00:00
|
|
|
|
|
|
|
|
var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
|
|
|
|
|
hoganUtils = new HoganJsUtils(config);
|
2015-04-12 01:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-30 21:55:33 +00:00
|
|
|
SideBySidePrinter.prototype.makeDiffHtml = function(file, diffs) {
|
2016-05-20 23:33:22 +00:00
|
|
|
var fileDiffTemplate = hoganUtils.template(baseTemplatesPath, 'file-diff');
|
|
|
|
|
var filePathTemplate = hoganUtils.template(genericTemplatesPath, 'file-path');
|
|
|
|
|
var fileIconTemplate = hoganUtils.template(iconsBaseTemplatesPath, 'file');
|
|
|
|
|
var fileTagTemplate = hoganUtils.template(tagsBaseTemplatesPath, printerUtils.getFileTypeIcon(file));
|
|
|
|
|
|
|
|
|
|
return fileDiffTemplate.render({
|
|
|
|
|
file: file,
|
|
|
|
|
fileHtmlId: printerUtils.getHtmlId(file),
|
|
|
|
|
diffs: diffs,
|
|
|
|
|
filePath: filePathTemplate.render({
|
|
|
|
|
fileDiffName: printerUtils.getDiffName(file)
|
|
|
|
|
}, {
|
|
|
|
|
fileIcon: fileIconTemplate,
|
|
|
|
|
fileTag: fileTagTemplate
|
|
|
|
|
})
|
|
|
|
|
});
|
2015-12-30 21:55:33 +00:00
|
|
|
};
|
|
|
|
|
|
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-04-19 23:24:19 +00:00
|
|
|
|
2016-05-20 23:33:22 +00:00
|
|
|
var content = diffFiles.map(function(file) {
|
|
|
|
|
var diffs;
|
|
|
|
|
if (file.blocks.length) {
|
|
|
|
|
diffs = that.generateSideBySideFileHtml(file);
|
|
|
|
|
} else {
|
|
|
|
|
diffs = that.generateEmptyDiff();
|
|
|
|
|
}
|
2015-08-08 00:11:35 +00:00
|
|
|
|
2016-05-20 23:33:22 +00:00
|
|
|
return that.makeDiffHtml(file, diffs);
|
|
|
|
|
}).join('\n');
|
|
|
|
|
|
|
|
|
|
return hoganUtils.render(genericTemplatesPath, 'wrapper', {'content': content});
|
2015-04-12 01:59:54 +00:00
|
|
|
};
|
|
|
|
|
|
2015-12-30 21:55:33 +00:00
|
|
|
SideBySidePrinter.prototype.makeSideHtml = function(blockHeader) {
|
2016-05-20 23:33:22 +00:00
|
|
|
return hoganUtils.render(genericTemplatesPath, 'column-line-number', {
|
|
|
|
|
diffParser: diffParser,
|
2017-03-17 23:57:09 +00:00
|
|
|
blockHeader: utils.escape(blockHeader),
|
2016-05-20 23:33:22 +00:00
|
|
|
lineClass: 'd2h-code-side-linenumber',
|
|
|
|
|
contentClass: 'd2h-code-side-line'
|
|
|
|
|
});
|
2015-12-30 21:55:33 +00:00
|
|
|
};
|
|
|
|
|
|
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) {
|
2016-05-20 23:33:22 +00:00
|
|
|
fileHtml.left += that.makeSideHtml(block.header);
|
2015-12-30 21:55:33 +00:00
|
|
|
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;
|
2019-06-14 15:18:09 +00:00
|
|
|
|
|
|
|
|
var maxLineSizeInBlock = Math.max.apply(null, (oldLines.concat(newLines)).map(function(elem) {
|
|
|
|
|
return elem.length;
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
var doMatching = comparisons < that.config.matchingMaxComparisons &&
|
|
|
|
|
maxLineSizeInBlock < that.config.maxLineSizeInBlockForComparison &&
|
|
|
|
|
(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 +=
|
2016-09-05 21:13:51 +00:00
|
|
|
that.generateSingleLineHtml(file.isCombined, deleteType, oldLine.oldNumber,
|
2015-11-25 16:37:26 +00:00
|
|
|
diff.first.line, diff.first.prefix);
|
|
|
|
|
fileHtml.right +=
|
2016-09-05 21:13:51 +00:00
|
|
|
that.generateSingleLineHtml(file.isCombined, 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);
|
|
|
|
|
|
2016-09-05 21:13:51 +00:00
|
|
|
var tmpHtml = that.processLines(file.isCombined, 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) {
|
2016-09-05 21:13:51 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(file.isCombined, line.type, line.oldNumber, escapedLine, prefix);
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(file.isCombined, line.type, line.newNumber, escapedLine, prefix);
|
2015-12-20 22:22:58 +00:00
|
|
|
} else if (line.type === diffParser.LINE_TYPE.INSERTS && !oldLines.length) {
|
2016-09-05 21:13:51 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(file.isCombined, diffParser.LINE_TYPE.CONTEXT, '', '', '');
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(file.isCombined, 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
|
|
|
|
2016-09-05 21:13:51 +00:00
|
|
|
SideBySidePrinter.prototype.processLines = function(isCombined, oldLines, newLines) {
|
2015-12-21 13:34:16 +00:00
|
|
|
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) {
|
2016-09-05 21:13:51 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(isCombined, oldLine.type, oldLine.oldNumber, oldContent, oldPrefix);
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(isCombined, newLine.type, newLine.newNumber, newContent, newPrefix);
|
2015-04-19 23:24:19 +00:00
|
|
|
} else if (oldLine) {
|
2016-09-05 21:13:51 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(isCombined, oldLine.type, oldLine.oldNumber, oldContent, oldPrefix);
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(isCombined, diffParser.LINE_TYPE.CONTEXT, '', '', '');
|
2015-04-19 23:24:19 +00:00
|
|
|
} else if (newLine) {
|
2016-09-05 21:13:51 +00:00
|
|
|
fileHtml.left += that.generateSingleLineHtml(isCombined, diffParser.LINE_TYPE.CONTEXT, '', '', '');
|
|
|
|
|
fileHtml.right += that.generateSingleLineHtml(isCombined, 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
|
|
|
|
2016-09-05 21:13:51 +00:00
|
|
|
SideBySidePrinter.prototype.generateSingleLineHtml = function(isCombined, type, number, content, possiblePrefix) {
|
|
|
|
|
var lineWithoutPrefix = content;
|
|
|
|
|
var prefix = possiblePrefix;
|
2018-11-14 21:18:39 +00:00
|
|
|
var lineClass = 'd2h-code-side-linenumber';
|
|
|
|
|
var contentClass = 'd2h-code-side-line';
|
|
|
|
|
|
|
|
|
|
if (!number && !content) {
|
|
|
|
|
lineClass += ' d2h-code-side-emptyplaceholder';
|
|
|
|
|
contentClass += ' d2h-code-side-emptyplaceholder';
|
|
|
|
|
type += ' d2h-emptyplaceholder';
|
2019-12-22 17:35:29 +00:00
|
|
|
prefix = ' ';
|
|
|
|
|
lineWithoutPrefix = ' ';
|
|
|
|
|
} else if (!prefix) {
|
2016-09-05 21:13:51 +00:00
|
|
|
var lineWithPrefix = printerUtils.separatePrefix(isCombined, content);
|
|
|
|
|
prefix = lineWithPrefix.prefix;
|
|
|
|
|
lineWithoutPrefix = lineWithPrefix.line;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-06 17:39:50 +00:00
|
|
|
if (prefix === ' ') {
|
|
|
|
|
prefix = ' ';
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 23:33:22 +00:00
|
|
|
return hoganUtils.render(genericTemplatesPath, 'line',
|
|
|
|
|
{
|
|
|
|
|
type: type,
|
2018-11-14 21:18:39 +00:00
|
|
|
lineClass: lineClass,
|
|
|
|
|
contentClass: contentClass,
|
2016-09-05 21:13:51 +00:00
|
|
|
prefix: prefix,
|
|
|
|
|
content: lineWithoutPrefix,
|
2016-05-20 23:33:22 +00:00
|
|
|
lineNumber: number
|
|
|
|
|
});
|
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
|
|
|
|
2016-05-20 23:33:22 +00:00
|
|
|
fileHtml.left = hoganUtils.render(genericTemplatesPath, 'empty-diff', {
|
2016-05-21 00:34:03 +00:00
|
|
|
contentClass: 'd2h-code-side-line',
|
2016-05-20 23:33:22 +00:00
|
|
|
diffParser: diffParser
|
|
|
|
|
});
|
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-12-20 22:22:58 +00:00
|
|
|
})();
|