diff2html
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "diff2html", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
@@ -5,0 +5,0 @@ "homepage": "https://www.github.com/rtfpessoa/diff2html-nodejs", |
@@ -67,8 +67,8 @@ /* | ||
if (values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line)) { | ||
currentFile.isTripleDiff = false; | ||
currentFile.isCombined = false; | ||
} else if (values = /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line)) { | ||
currentFile.isTripleDiff = true; | ||
currentFile.isCombined = true; | ||
} else { | ||
values = [0, 0]; | ||
currentFile.isTripleDiff = false; | ||
currentFile.isCombined = false; | ||
} | ||
@@ -124,7 +124,29 @@ | ||
// Also, remove some useless lines | ||
if (!line || utils.startsWith(line, "*") || | ||
utils.startsWith(line, "new") || utils.startsWith(line, "index")) { | ||
if (!line || utils.startsWith(line, "*")) { | ||
//|| utils.startsWith(line, "new") || utils.startsWith(line, "index") | ||
return; | ||
} | ||
/* Diff */ | ||
var oldMode = /old mode (\d{6})/; | ||
var newMode = /new mode (\d{6})/; | ||
var deletedFileMode = /deleted file mode (\d{6})/; | ||
var newFileMode = /new file mode (\d{6})/; | ||
var copyFrom = /copy from (.+)/; | ||
var copyTo = /copy to (.+)/; | ||
var renameFrom = /rename from (.+)/; | ||
var renameTo = /rename to (.+)/; | ||
var similarityIndex = /similarity index (\d+)%/; | ||
var dissimilarityIndex = /dissimilarity index (\d+)%/; | ||
var index = /index ([0-9a-z]+)..([0-9a-z]+) (\d{6})?/; | ||
/* Combined Diff */ | ||
var combinedIndex = /index ([0-9a-z]+),([0-9a-z]+)..([0-9a-z]+)/; | ||
var combinedMode = /mode (\d{6}),(\d{6})..(\d{6})/; | ||
var combinedNewFile = /new file mode (\d{6})/; | ||
var combinedDeletedFile = /deleted file mode (\d{6}),(\d{6})/; | ||
var values = []; | ||
@@ -135,9 +157,46 @@ if (utils.startsWith(line, "diff")) { | ||
currentFile.oldName = values[1]; | ||
currentFile.language = getExtension(currentFile.oldName, currentFile.language); | ||
} else if (currentFile && !currentFile.newName && (values = /^\+\+\+ [b]?\/(\S+).*$/.exec(line))) { | ||
currentFile.newName = values[1]; | ||
var fileSplit = currentFile.newName.split("."); | ||
currentFile.language = fileSplit[fileSplit.length - 1]; | ||
currentFile.language = getExtension(currentFile.newName, currentFile.language); | ||
} else if (currentFile && utils.startsWith(line, "@@")) { | ||
startBlock(line); | ||
} else if ((values = oldMode.exec(line))) { | ||
currentFile.oldMode = values[1]; | ||
} else if ((values = newMode.exec(line))) { | ||
currentFile.newMode = values[1]; | ||
} else if ((values = deletedFileMode.exec(line))) { | ||
currentFile.deletedFileMode = values[1]; | ||
} else if ((values = newFileMode.exec(line))) { | ||
currentFile.newFileMode = values[1]; | ||
} else if ((values = copyFrom.exec(line))) { | ||
currentFile.oldName = values[1]; | ||
currentFile.isCopy = true; | ||
} else if ((values = copyTo.exec(line))) { | ||
currentFile.newName = values[1]; | ||
currentFile.isCopy = true; | ||
} else if ((values = renameFrom.exec(line))) { | ||
currentFile.oldName = values[1]; | ||
currentFile.isRename = true; | ||
} else if ((values = renameTo.exec(line))) { | ||
currentFile.newName = values[1]; | ||
currentFile.isRename = true; | ||
} else if ((values = similarityIndex.exec(line))) { | ||
currentFile.unchangedPercentage = values[1]; | ||
} else if ((values = dissimilarityIndex.exec(line))) { | ||
currentFile.changedPercentage = values[1]; | ||
} else if ((values = index.exec(line))) { | ||
currentFile.checksumBefore = values[1]; | ||
currentFile.checksumAfter = values[2]; | ||
values[2] && (currentFile.mode = values[3]); | ||
} else if ((values = combinedIndex.exec(line))) { | ||
currentFile.checksumBefore = [values[2], values[3]]; | ||
currentFile.checksumAfter = values[1]; | ||
} else if ((values = combinedMode.exec(line))) { | ||
currentFile.oldMode = [values[2], values[3]]; | ||
currentFile.newMode = values[1]; | ||
} else if ((values = combinedNewFile.exec(line))) { | ||
currentFile.newFileMode = values[1]; | ||
} else if ((values = combinedDeletedFile.exec(line))) { | ||
currentFile.deletedFileMode = values[1]; | ||
} else if (currentBlock) { | ||
@@ -154,2 +213,8 @@ createLine(line); | ||
function getExtension(filename, language) { | ||
var nameSplit = filename.split("."); | ||
if (nameSplit.length > 1) return nameSplit[nameSplit.length - 1]; | ||
else return language; | ||
} | ||
if (typeof module !== 'undefined' && module.exports) { | ||
@@ -156,0 +221,0 @@ module.exports.DiffParser = new DiffParser(); |
@@ -33,3 +33,2 @@ /* | ||
var diffJson = diffParser.generateDiffJson(diffInput); | ||
var configOrEmpty = config || {}; | ||
@@ -36,0 +35,0 @@ return htmlPrinter.generateLineByLineJsonHtml(diffJson, configOrEmpty); |
@@ -20,2 +20,7 @@ /* | ||
diffFiles.map(function (file) { | ||
var diffs; | ||
if (file.blocks.length) diffs = generateFileHtml(file, config); | ||
else diffs = generateEmptyDiff(); | ||
return "<div class=\"d2h-file-wrapper\" data-lang=\"" + file.language + "\">\n" + | ||
@@ -27,3 +32,3 @@ " <div class=\"d2h-file-header\">\n" + | ||
" </div>\n" + | ||
" <div class=\"d2h-file-name\">" + printerUtils.getDiffName(file.oldName, file.newName) + "</div>\n" + | ||
" <div class=\"d2h-file-name\">" + printerUtils.getDiffName(file) + "</div>\n" + | ||
" </div>\n" + | ||
@@ -34,3 +39,3 @@ " <div class=\"d2h-file-diff\">\n" + | ||
" <tbody class=\"d2h-diff-tbody\">\n" + | ||
" " + generateFileHtml(file, config) + | ||
" " + diffs + | ||
" </tbody>\n" + | ||
@@ -82,3 +87,3 @@ " </table>\n" + | ||
config.isTripleDiff = file.isTripleDiff; | ||
config.isCombined = file.isCombined; | ||
var diff = printerUtils.diffHighlight(oldEscapedLine, newEscapedLine, config); | ||
@@ -92,13 +97,3 @@ | ||
} else { | ||
for (j = 0; j < oldLines.length; j++) { | ||
oldLine = oldLines[j]; | ||
oldEscapedLine = utils.escape(oldLine.content); | ||
lines += generateLineHtml(oldLine.type, oldLine.oldNumber, oldLine.newNumber, oldEscapedLine); | ||
} | ||
for (j = 0; j < newLines.length; j++) { | ||
newLine = newLines[j]; | ||
newEscapedLine = utils.escape(newLine.content); | ||
lines += generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, newEscapedLine); | ||
} | ||
lines += processLines(oldLines, newLines); | ||
} | ||
@@ -114,2 +109,4 @@ | ||
lines += processLines(oldLines, newLines); | ||
return lines; | ||
@@ -119,2 +116,20 @@ }).join("\n"); | ||
function processLines(oldLines, newLines) { | ||
var lines = ""; | ||
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; | ||
} | ||
function generateLineHtml(type, oldNumber, newNumber, content) { | ||
@@ -132,2 +147,12 @@ return "<tr>\n" + | ||
function generateEmptyDiff() { | ||
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"; | ||
} | ||
if (typeof module !== 'undefined' && module.exports) { | ||
@@ -134,0 +159,0 @@ module.exports.LineByLinePrinter = new LineByLinePrinter(); |
@@ -16,6 +16,11 @@ /* | ||
PrinterUtils.prototype.getDiffName = function (oldFilename, newFilename) { | ||
if (oldFilename && newFilename && oldFilename !== newFilename) { | ||
PrinterUtils.prototype.getDiffName = function (file) { | ||
var oldFilename = file.oldName; | ||
var newFilename = file.newName; | ||
if (oldFilename && newFilename | ||
&& oldFilename !== newFilename | ||
&& !isDeletedName(newFilename)) { | ||
return oldFilename + " -> " + newFilename; | ||
} else if (newFilename) { | ||
} else if (newFilename && !isDeletedName(newFilename)) { | ||
return newFilename; | ||
@@ -34,3 +39,3 @@ } else if (oldFilename) { | ||
if (config.isTripleDiff) prefixSize = 2; | ||
if (config.isCombined) prefixSize = 2; | ||
@@ -65,2 +70,6 @@ lineStart1 = diffLine1.substr(0, prefixSize); | ||
function isDeletedName(name) { | ||
return name === "dev/null"; | ||
} | ||
function removeIns(line) { | ||
@@ -67,0 +76,0 @@ return line.replace(/(<ins>((.|\n)*?)<\/ins>)/g, ""); |
@@ -20,4 +20,7 @@ /* | ||
diffFiles.map(function (file) { | ||
var diffs = generateSideBySideFileHtml(file, config); | ||
var diffs; | ||
if (file.blocks.length) diffs = generateSideBySideFileHtml(file, config); | ||
else diffs = generateEmptyDiff(); | ||
return "<div class=\"d2h-file-wrapper\" data-lang=\"" + file.language + "\">\n" + | ||
@@ -29,3 +32,3 @@ " <div class=\"d2h-file-header\">\n" + | ||
" </div>\n" + | ||
" <div class=\"d2h-file-name\">" + printerUtils.getDiffName(file.oldName, file.newName) + "</div>\n" + | ||
" <div class=\"d2h-file-name\">" + printerUtils.getDiffName(file) + "</div>\n" + | ||
" </div>\n" + | ||
@@ -79,2 +82,3 @@ " <div class=\"d2h-files-diff\">\n" + | ||
var oldLines = [], newLines = []; | ||
var tmpHtml = ""; | ||
@@ -107,3 +111,3 @@ for (var i = 0; i < block.lines.length; i++) { | ||
config.isTripleDiff = file.isTripleDiff; | ||
config.isCombined = file.isCombined; | ||
@@ -116,20 +120,5 @@ var diff = printerUtils.diffHighlight(oldEscapedLine, newEscapedLine, config); | ||
} else { | ||
var maxLinesNumber = Math.max(oldLines.length, newLines.length); | ||
for (j = 0; j < maxLinesNumber; j++) { | ||
oldLine = oldLines[j]; | ||
newLine = newLines[j]; | ||
if (oldLine && newLine) { | ||
fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, utils.escape(oldLine.content)); | ||
fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, utils.escape(newLine.content)); | ||
} else if (oldLine) { | ||
fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, utils.escape(oldLine.content)); | ||
fileHtml.right += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, "", "", ""); | ||
} else if (newLine) { | ||
fileHtml.left += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, "", "", ""); | ||
fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, utils.escape(newLine.content)); | ||
} else { | ||
console.error("How did it get here?"); | ||
} | ||
} | ||
tmpHtml = processLines(oldLines, newLines); | ||
fileHtml.left += tmpHtml.left; | ||
fileHtml.right += tmpHtml.right; | ||
} | ||
@@ -142,2 +131,6 @@ | ||
} | ||
tmpHtml = processLines(oldLines, newLines); | ||
fileHtml.left += tmpHtml.left; | ||
fileHtml.right += tmpHtml.right; | ||
}); | ||
@@ -148,2 +141,29 @@ | ||
function processLines(oldLines, newLines) { | ||
var fileHtml = {}; | ||
fileHtml.left = ""; | ||
fileHtml.right = ""; | ||
var maxLinesNumber = Math.max(oldLines.length, newLines.length); | ||
for (j = 0; j < maxLinesNumber; j++) { | ||
var oldLine = oldLines[j]; | ||
var newLine = newLines[j]; | ||
if (oldLine && newLine) { | ||
fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, utils.escape(oldLine.content)); | ||
fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, utils.escape(newLine.content)); | ||
} else if (oldLine) { | ||
fileHtml.left += generateSingleLineHtml(oldLine.type, oldLine.oldNumber, utils.escape(oldLine.content)); | ||
fileHtml.right += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, "", "", ""); | ||
} else if (newLine) { | ||
fileHtml.left += generateSingleLineHtml(diffParser.LINE_TYPE.CONTEXT, "", "", ""); | ||
fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, utils.escape(newLine.content)); | ||
} else { | ||
console.error("How did it get here?"); | ||
} | ||
} | ||
return fileHtml; | ||
} | ||
function generateSingleLineHtml(type, number, content) { | ||
@@ -158,2 +178,17 @@ return "<tr>\n" + | ||
function generateEmptyDiff() { | ||
var fileHtml = {}; | ||
fileHtml.right = ""; | ||
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"; | ||
return fileHtml; | ||
} | ||
if (typeof module !== 'undefined' && module.exports) { | ||
@@ -160,0 +195,0 @@ module.exports.SideBySidePrinter = new SideBySidePrinter(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49415
1146