diff --git a/diff2html.js b/diff2html.js deleted file mode 100644 index 435a6cff..00000000 --- a/diff2html.js +++ /dev/null @@ -1,422 +0,0 @@ -/* - * - * Diff to HTML (diff2html.js) - * Author: rtfpessoa - * - * Diff commands: - * git diff - */ - -var LINE_TYPE = { - INSERTS: "d2h-ins", - DELETES: "d2h-del", - CONTEXT: "d2h-cntx", - INFO: "d2h-info" -}; - -function Diff2Html() { -} - -/* - * Generates pretty html from string diff input - */ -Diff2Html.prototype.getPrettyHtmlFromDiff = function (diffInput) { - var diffJson = generateDiffJson(diffInput); - return generateJsonHtml(diffJson); -}; - -/* - * Generates json object from string diff input - */ -Diff2Html.prototype.getJsonFromDiff = function (diffInput) { - return generateDiffJson(diffInput); -}; - -/* - * Generates pretty html from a json object - */ -Diff2Html.prototype.getPrettyHtmlFromJson = function (diffJson) { - return generateJsonHtml(diffJson); -}; - -/* - * Generates pretty side by side html from string diff input - */ -Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function (diffInput) { - var diffJson = generateDiffJson(diffInput); - return generateSideBySideJsonHtml(diffJson); -}; - -/* - * Generates pretty side by side html from a json object - */ -Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function (diffJson) { - return generateSideBySideJsonHtml(diffJson); -}; - -var generateDiffJson = function (diffInput) { - var files = [], - currentFile = null, - currentBlock = null, - oldLine = null, - newLine = null; - - var saveBlock = function () { - /* add previous block(if exists) before start a new file */ - if (currentBlock) { - currentFile.blocks.push(currentBlock); - currentBlock = null; - } - }; - - var saveFile = function () { - /* - * add previous file(if exists) before start a new one - * if it has name (to avoid binary files errors) - */ - if (currentFile && currentFile.newName) { - files.push(currentFile); - currentFile = null; - } - }; - - var startFile = function () { - saveBlock(); - saveFile(); - - /* create file structure */ - currentFile = {}; - currentFile.blocks = []; - currentFile.deletedLines = 0; - currentFile.addedLines = 0; - }; - - var startBlock = function (line) { - saveBlock(); - - var values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line) || - /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line) || - [0, 0, 0]; - - oldLine = values[1]; - newLine = values[2]; - - /* create block metadata */ - currentBlock = {}; - currentBlock.lines = []; - currentBlock.oldStartLine = oldLine; - currentBlock.newStartLine = newLine; - currentBlock.header = line; - }; - - var createLine = function (line) { - var currentLine = {}; - currentLine.content = line; - - /* fill the line data */ - if (startsWith(line, "+") || startsWith(line, " +")) { - currentFile.addedLines++; - - currentLine.type = LINE_TYPE.INSERTS; - currentLine.oldNumber = null; - currentLine.newNumber = newLine++; - - currentBlock.lines.push(currentLine); - - } else if (startsWith(line, "-") || startsWith(line, " -")) { - currentFile.deletedLines++; - - currentLine.type = LINE_TYPE.DELETES; - currentLine.oldNumber = oldLine++; - currentLine.newNumber = null; - - currentBlock.lines.push(currentLine); - - } else { - currentLine.type = LINE_TYPE.CONTEXT; - currentLine.oldNumber = oldLine++; - currentLine.newNumber = newLine++; - - currentBlock.lines.push(currentLine); - } - }; - - var diffLines = diffInput.split("\n"); - diffLines.forEach(function (line) { - // Unmerged paths, and possibly other non-diffable files - // https://github.com/scottgonzalez/pretty-diff/issues/11 - // Also, remove some useless lines - if (!line || startsWith(line, "*") || - startsWith(line, "new") || startsWith(line, "index")) { - return; - } - - var values = []; - if (startsWith(line, "diff")) { - startFile(); - } else if (currentFile && !currentFile.oldName && (values = /^--- a\/(\S+).*$/.exec(line))) { - currentFile.oldName = values[1]; - } 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]; - } else if (currentFile && startsWith(line, "@@")) { - startBlock(line); - } else if (currentBlock) { - createLine(line); - } - }); - - saveBlock(); - saveFile(); - - return files; -}; - -/* - * Line By Line HTML - */ - -var generateJsonHtml = function (diffFiles) { - return "
\n" + - diffFiles.map(function (file) { - return "
\n" + - "
\n" + - "
\n" + - " +" + file.addedLines + "\n" + - " -" + file.deletedLines + "\n" + - "
\n" + - "
" + getDiffName(file.oldName, file.newName) + "
\n" + - "
\n" + - "
\n" + - "
\n" + - " \n" + - " \n" + - " " + generateFileHtml(file) + - " \n" + - "
\n" + - "
\n" + - "
\n" + - "
\n"; - }).join("\n") + - "
\n"; -}; - -var generateFileHtml = function (file) { - return file.blocks.map(function (block) { - - var lines = "\n" + - " \n" + - " " + - "
" + escape(block.header) + "
" + - " \n" + - "\n"; - - for (var i = 0; i < block.lines.length; i++) { - var prevLine = block.lines[i - 1]; - var line = block.lines[i]; - var newLine = block.lines[i + 1]; - var nextNewLine = block.lines[i + 2]; - - var isOppositeTypeTwoLineBlock = - line.type == LINE_TYPE.DELETES && - newLine && newLine.type == LINE_TYPE.INSERTS && - (!nextNewLine || nextNewLine && nextNewLine.type != LINE_TYPE.INSERTS) && - (!prevLine || prevLine && prevLine.type != LINE_TYPE.DELETES); - - var escapedLine = escape(line.content); - - if (isOppositeTypeTwoLineBlock) { - var nextEscapedLine = escape(newLine.content); - - var diff = diffHighlight(escapedLine, nextEscapedLine); - - lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, diff.o) + - generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, diff.n); - - i++; - } else { - lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine); - } - } - - return lines; - }).join("\n"); -}; - -var generateLineHtml = function (type, oldNumber, newNumber, content) { - return "\n" + - " " + - "
" + valueOrEmpty(oldNumber) + "
" + - "
" + valueOrEmpty(newNumber) + "
" + - " \n" + - " " + - "
" + content + "
" + - " \n" + - "\n"; -}; - -/* - * Side By Side HTML (work in progress) - */ - -var generateSideBySideJsonHtml = function (diffFiles) { - return "
\n" + - diffFiles.map(function (file) { - var diffs = generateSideBySideFileHtml(file); - - return "
\n" + - "
\n" + - "
\n" + - " +" + file.addedLines + "\n" + - " -" + file.deletedLines + "\n" + - "
\n" + - "
" + getDiffName(file.oldName, file.newName) + "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - " \n" + - " \n" + - " " + diffs.left + - " \n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - " \n" + - " \n" + - " " + diffs.right + - " \n" + - "
\n" + - "
\n" + - "
\n" + - "
\n" + - "
\n"; - }).join("\n") + - "
\n"; -}; - -var generateSideBySideFileHtml = function (file) { - var fileHtml = {}; - fileHtml.left = ""; - fileHtml.right = ""; - - file.blocks.forEach(function (block) { - - fileHtml.left += "\n" + - " \n" + - " " + - "
" + escape(block.header) + "
" + - " \n" + - "\n"; - - fileHtml.right += "\n" + - " \n" + - " " + - "
" + - " \n" + - "\n"; - - for (var i = 0; i < block.lines.length; i++) { - var prevLine = block.lines[i - 1]; - var line = block.lines[i]; - var newLine = block.lines[i + 1]; - var nextNewLine = block.lines[i + 2]; - - var isOpositeTypeTwoLineBlock = line.type == LINE_TYPE.DELETES && newLine && newLine.type == LINE_TYPE.INSERTS && - (!nextNewLine || nextNewLine && nextNewLine.type != LINE_TYPE.INSERTS) && - (!prevLine || prevLine && prevLine.type != LINE_TYPE.DELETES); - - var escapedLine = escape(line.content); - - if (isOpositeTypeTwoLineBlock) { - var nextEscapedLine = escape(newLine.content); - - var diff = diffHighlight(escapedLine, nextEscapedLine); - - fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, diff.o); - fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, diff.n); - - i++; - } else if (line.type == LINE_TYPE.DELETES) { - fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine); - fileHtml.right += generateSingleLineHtml(LINE_TYPE.CONTEXT, "", "", ""); - } else if (line.type == LINE_TYPE.INSERTS) { - fileHtml.left += generateSingleLineHtml(LINE_TYPE.CONTEXT, "", "", ""); - fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine); - } else { - fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine); - fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine); - } - } - - }); - - return fileHtml; -}; - -var generateSingleLineHtml = function (type, number, content) { - return "\n" + - " " + number + "\n" + - " " + - "
" + content + "
" + - " \n" + - " \n"; -}; - -/* - * HTML Helpers - */ - -var getDiffName = function (oldFilename, newFilename) { - if (oldFilename && newFilename && oldFilename !== newFilename) { - return oldFilename + " -> " + newFilename; - } else if (newFilename) { - return newFilename; - } else if (oldFilename) { - return oldFilename; - } else { - return "Unknown filename"; - } -}; - -var removeIns = function (line) { - return line.replace(/(((.|\n)*?)<\/ins>)/g, ""); -}; - -var removeDel = function (line) { - return line.replace(/(((.|\n)*?)<\/del>)/g, ""); -}; - -/* - * Utils - */ - -function escape(str) { - return str.slice(0) - .replace(/&/g, "&") - .replace(//g, ">") - .replace(/\t/g, " "); -} - -function startsWith(str, start) { - return str.indexOf(start) === 0; -} - -function valueOrEmpty(value) { - return value ? value : ""; -} - -function diffHighlight(diffLine1, diffLine2) { - /* remove the initial -/+ to avoid always having diff in the first char */ - var highlightedLine = JsDiff(diffLine1.substr(1), diffLine2.substr(1)); - - return { - o: diffLine1.charAt(0) + removeIns(highlightedLine), - n: diffLine2.charAt(0) + removeDel(highlightedLine) - } -} diff --git a/diff2html.min.css b/diff2html.min.css deleted file mode 100644 index 7f7ce1b6..00000000 --- a/diff2html.min.css +++ /dev/null @@ -1 +0,0 @@ -.d2h-wrapper{display:block;margin:0 auto;text-align:left;width:100%}.d2h-file-wrapper{border:1px solid #ddd;border-radius:3px;margin-bottom:1em}.d2h-file-header{padding:5px 10px;border-bottom:1px solid #d8d8d8;background-color:#f7f7f7;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-stats{display:inline;font-size:12px;text-align:center;max-width:15%}.d2h-lines-added{background-color:#ceffce;border:1px solid #b4e2b4;color:#399839;border-radius:5px 0 0 5px;padding:2px;width:25px}.d2h-lines-deleted{background-color:#f7c8c8;border:1px solid #e9aeae;color:#c33;border-radius:0 5px 5px 0;padding:2px;width:25px}.d2h-file-name{display:inline;height:33px;line-height:33px;max-width:80%;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}.d2h-diff-table{border-collapse:collapse;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;height:18px;line-height:18px;width:100%}.d2h-files-diff{width:100%}.d2h-file-diff{overflow-x:scroll}.d2h-file-side-diff{display:inline-block;overflow-x:scroll;width:50%;margin-right:-4px}.d2h-code-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:80px;color:inherit;overflow-x:inherit;background:0 0}.d2h-code-side-line.hljs{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:50px;color:inherit;overflow-x:inherit;background:0 0}.d2h-code-line del,.d2h-code-side-line del{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#ffb6ba;border-radius:.2em}.d2h-code-line ins,.d2h-code-side-line ins{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#97f295;border-radius:.2em}.line-num1{display:inline-block;float:left;width:30px;overflow:hidden;text-overflow:ellipsis}.line-num2{display:inline-block;float:right;width:30px;overflow:hidden;text-overflow:ellipsis}.d2h-code-linenumber{position:absolute;width:2%;min-width:65px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,.3);text-align:right;border:solid #eee;border-width:0 1px;cursor:pointer}.d2h-code-side-linenumber{position:absolute;width:35px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,.3);text-align:right;border:solid #eee;border-width:0 1px;cursor:pointer;overflow:hidden;text-overflow:ellipsis}.d2h-del{background-color:#fee8e9;border-color:#e9aeae}.d2h-ins{background-color:#dfd;border-color:#b4e2b4}.d2h-info{background-color:#f8fafd;color:rgba(0,0,0,.3);border-color:#d5e4f2} \ No newline at end of file diff --git a/diff2html.min.js b/diff2html.min.js deleted file mode 100644 index a35f90ac..00000000 --- a/diff2html.min.js +++ /dev/null @@ -1 +0,0 @@ -function Diff2Html(){}function escape(str){return str.slice(0).replace(/&/g,"&").replace(//g,">").replace(/\t/g," ")}function startsWith(str,start){return 0===str.indexOf(start)}function valueOrEmpty(value){return value?value:""}function diffHighlight(diffLine1,diffLine2){var highlightedLine=JsDiff(diffLine1.substr(1),diffLine2.substr(1));return{o:diffLine1.charAt(0)+removeIns(highlightedLine),n:diffLine2.charAt(0)+removeDel(highlightedLine)}}var LINE_TYPE={INSERTS:"d2h-ins",DELETES:"d2h-del",CONTEXT:"d2h-cntx",INFO:"d2h-info"};Diff2Html.prototype.getPrettyHtmlFromDiff=function(diffInput){var diffJson=generateDiffJson(diffInput);return generateJsonHtml(diffJson)},Diff2Html.prototype.getJsonFromDiff=function(diffInput){return generateDiffJson(diffInput)},Diff2Html.prototype.getPrettyHtmlFromJson=function(diffJson){return generateJsonHtml(diffJson)},Diff2Html.prototype.getPrettySideBySideHtmlFromDiff=function(diffInput){var diffJson=generateDiffJson(diffInput);return generateSideBySideJsonHtml(diffJson)},Diff2Html.prototype.getPrettySideBySideHtmlFromJson=function(diffJson){return generateSideBySideJsonHtml(diffJson)};var generateDiffJson=function(diffInput){var files=[],currentFile=null,currentBlock=null,oldLine=null,newLine=null,saveBlock=function(){currentBlock&&(currentFile.blocks.push(currentBlock),currentBlock=null)},saveFile=function(){currentFile&¤tFile.newName&&(files.push(currentFile),currentFile=null)},startFile=function(){saveBlock(),saveFile(),currentFile={},currentFile.blocks=[],currentFile.deletedLines=0,currentFile.addedLines=0},startBlock=function(line){saveBlock();var values=/^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line)||/^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line)||[0,0,0];oldLine=values[1],newLine=values[2],currentBlock={},currentBlock.lines=[],currentBlock.oldStartLine=oldLine,currentBlock.newStartLine=newLine,currentBlock.header=line},createLine=function(line){var currentLine={};currentLine.content=line,startsWith(line,"+")||startsWith(line," +")?(currentFile.addedLines++,currentLine.type=LINE_TYPE.INSERTS,currentLine.oldNumber=null,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):startsWith(line,"-")||startsWith(line," -")?(currentFile.deletedLines++,currentLine.type=LINE_TYPE.DELETES,currentLine.oldNumber=oldLine++,currentLine.newNumber=null,currentBlock.lines.push(currentLine)):(currentLine.type=LINE_TYPE.CONTEXT,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine))},diffLines=diffInput.split("\n");return diffLines.forEach(function(line){if(line&&!startsWith(line,"*")&&!startsWith(line,"new")&&!startsWith(line,"index")){var values=[];if(startsWith(line,"diff"))startFile();else if(currentFile&&!currentFile.oldName&&(values=/^--- a\/(\S+).*$/.exec(line)))currentFile.oldName=values[1];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]}else currentFile&&startsWith(line,"@@")?startBlock(line):currentBlock&&createLine(line)}}),saveBlock(),saveFile(),files},generateJsonHtml=function(diffFiles){return'
\n'+diffFiles.map(function(file){return'
\n
\n
\n +'+file.addedLines+'\n -'+file.deletedLines+'\n
\n
'+getDiffName(file.oldName,file.newName)+'
\n
\n
\n
\n \n \n '+generateFileHtml(file)+" \n
\n
\n
\n
\n"}).join("\n")+"
\n"},generateFileHtml=function(file){return file.blocks.map(function(block){for(var lines='\n \n
'+escape(block.header)+"
\n\n",i=0;i\n
'+valueOrEmpty(oldNumber)+'
'+valueOrEmpty(newNumber)+'
\n
'+content+"
\n\n"},generateSideBySideJsonHtml=function(diffFiles){return'
\n'+diffFiles.map(function(file){var diffs=generateSideBySideFileHtml(file);return'
\n
\n
\n +'+file.addedLines+'\n -'+file.deletedLines+'\n
\n
'+getDiffName(file.oldName,file.newName)+'
\n
\n
\n
\n
\n \n \n '+diffs.left+' \n
\n
\n
\n
\n
\n \n \n '+diffs.right+" \n
\n
\n
\n
\n
\n"}).join("\n")+"
\n"},generateSideBySideFileHtml=function(file){var fileHtml={};return fileHtml.left="",fileHtml.right="",file.blocks.forEach(function(block){fileHtml.left+='\n \n
'+escape(block.header)+"
\n\n",fileHtml.right+='\n \n
\n\n';for(var i=0;i\n '+number+'\n
'+content+"
\n \n"},getDiffName=function(oldFilename,newFilename){return oldFilename&&newFilename&&oldFilename!==newFilename?oldFilename+" -> "+newFilename:newFilename?newFilename:oldFilename?oldFilename:"Unknown filename"},removeIns=function(line){return line.replace(/(((.|\n)*?)<\/ins>)/g,"")},removeDel=function(line){return line.replace(/(((.|\n)*?)<\/del>)/g,"")}; \ No newline at end of file diff --git a/index.html b/index.html index f788c02e..1ebfe860 100644 --- a/index.html +++ b/index.html @@ -7,18 +7,23 @@ - - - + + + + + + + + + +