From 97a46fe7ea25729929379bf98dbb140a6fd87a57 Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Mon, 8 Sep 2014 18:18:31 +0100 Subject: [PATCH] update to version 3 --- LICENSE | 20 --- README.md | 49 ------ diff2html.css | 133 ---------------- diff2html.js | 383 ---------------------------------------------- diff2html.min.css | 1 + diff2html.min.js | 2 +- index.html | 19 ++- 7 files changed, 16 insertions(+), 591 deletions(-) delete mode 100644 LICENSE delete mode 100644 README.md delete mode 100644 diff2html.css delete mode 100644 diff2html.js create mode 100644 diff2html.min.css diff --git a/LICENSE b/LICENSE deleted file mode 100644 index f676a81..0000000 --- a/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright 2014 Rodrigo Fernandes https://rtfpessoa.github.io/ - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md deleted file mode 100644 index 7476e25..0000000 --- a/README.md +++ /dev/null @@ -1,49 +0,0 @@ -# Diff to Html by [rtfpessoa](https://github.com/rtfpessoa) - -Diff to Html generates pretty HTML diffs from git word diff output. - -## Features - -* line-by-line diff (not side-by-side) - -* char-by-char highlight - -* new and old line numbers - -* inserted and removed lines - -* GitHub like style - -## Setup - -Import the diff2html.js in your html. - -> Pretty Html From Git Word Diff Output - - Diff2Html.getPrettyHtmlFromDiff(exInput); - -> Intermediate Json From Git Word Diff Output - - Diff2Html.getJsonFromDiff(exInput); - -> Pretty Html From Json - - Diff2Html.getPrettyHtmlFromJson(exInput); - -> Check out the `template.html` for a complete example. - -## Contributions - -All the contributions are welcome. - -To contribute just send a pull request with your feature,fix,... and it will be reviewed asap. - -## License - -Copyright 2014 Rodrigo Fernandes. Released under the terms of the MIT license. - -## Thanks - -This project is inspired in [pretty-diff](https://github.com/scottgonzalez/pretty-diff) by [Scott González](https://github.com/scottgonzalez). - ---- diff --git a/diff2html.css b/diff2html.css deleted file mode 100644 index f6a1c1d..0000000 --- a/diff2html.css +++ /dev/null @@ -1,133 +0,0 @@ -/* - * - * Diff to HTML (diff2html.css) - * Author: rtfpessoa - * Date: Friday 29 August 2014 - * Last Update: Saturday 6 September 2014 - * - */ - -#d2h-wrapper { - display: block; - margin: 0 auto; - text-align: left; - width: 900px; -} - -.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-block; - font-size: 12px; - text-align: center; - width: 70px; -} - -.d2h-lines-added { - display: inline-block; - background-color: #ceffce; - border: 1px solid #b4e2b4; - color: #399839; - border-radius: 5px 0 0 5px; - padding: 2px; - width: 25px; -} - -.d2h-lines-deleted { - display: inline-block; - background-color: #f7c8c8; - border: 1px solid #e9aeae; - color: #c33; - border-radius: 0 5px 5px 0; - padding: 2px; - width: 25px; -} - -.d2h-file-name { - display: inline-block; - height: 33px; - line-height: 33px; - width: 750px; -} - -.d2h-file-diff > div { - width: 100%; -} - -.d2h-diff-table { - border-collapse: collapse; - font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; - font-size: 12px; - height: 18px; - line-height: 18px; -} - -.d2h-code-line { - white-space: pre; - padding-left: 10px; - padding-right: 10px; - height: 18px; - line-height: 18px; - - width: 776px; - overflow: hidden; -} - -.d2h-code-line del { - display: inline-block; - margin-top: -1px; - text-decoration: none; - background-color: #f99; - font-weight: 700; - border-radius: 0.2em; -} - -.d2h-code-line ins { - display: inline-block; - margin-top: -1px; - text-decoration: none; - background-color: #6f7; - font-weight: 700; - border-radius: 0.2em; -} - -.d2h-code-linenumber { - width: 1%; - min-width: 30px; - padding-left: 10px; - padding-right: 10px; - height: 18px; - line-height: 18px; - color: rgba(0, 0, 0, 0.3); - text-align: right; - border: solid #eeeeee; - border-width: 0 1px 0 0; - cursor: pointer; -} - -.d2h-del { - background-color: #f7c8c8; - border-color: #e9aeae; -} - -.d2h-ins { - background-color: #ceffce; - border-color: #b4e2b4; -} - -.d2h-info { - background-color: #f8fafd; - color: rgba(0, 0, 0, 0.3); - border-color: #d5e4f2; -} diff --git a/diff2html.js b/diff2html.js deleted file mode 100644 index 385f7a1..0000000 --- a/diff2html.js +++ /dev/null @@ -1,383 +0,0 @@ -/* - * - * Diff to HTML (diff2html.js) - * Author: rtfpessoa - * Date: Friday 29 August 2014 - * Last Update: Saturday 6 September 2014 - * - * Diff command: - * git diff --word-diff-regex=. HEAD~1 - */ - -(function (window) { - var ClassVariable; - - ClassVariable = (function () { - - var LINE_TYPE = { - INSERTS: "d2h-ins", - ALL_NEW: "d2h-all-ins", - - DELETES: "d2h-del", - ALL_DELETED: "d2h-all-del", - - INSERTS_AND_DELETES: "d2h-ins-and-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); - }; - - 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 (currentFile) { - files.push(currentFile); - currentFile = null; - } - }; - - var startFile = function (line) { - saveBlock(); - saveFile(); - - /* create file structure */ - currentFile = {}; - currentFile.blocks = []; - currentFile.deletedLines = 0; - currentFile.addedLines = 0; - - /* save file paths, before and after the diff */ - var values = /^diff --git a\/(\S+) b\/(\S+).*$/.exec(line); - currentFile.oldName = values[1]; - currentFile.newName = values[2]; - }; - - var startBlock = function (line) { - saveBlock(); - - var values = /^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(line); - - /* create block metadata */ - currentBlock = {}; - currentBlock.lines = []; - currentBlock.oldStartLine = oldLine = values[2]; - currentBlock.newStartLine = newLine = values[4]; - currentBlock.header = line; - }; - - var createLine = function (line) { - /* Line Types */ - var isLineWithInserts = /{\+.*?\+}/.exec(line); - var isNewLine = /^{\+.*?\+}$/.exec(line); - - var isLineWithDeletes = /\[-.*?-\]/.exec(line); - var isRemovedLine = /^\[-.*?-\]$/.exec(line); - - var isLineWithBoth = isLineWithInserts && isLineWithDeletes; - var isContextLine = !isLineWithInserts && !isLineWithDeletes; - - var currentLine = {}; - currentLine.content = line; - - /* fill the line data */ - if (isLineWithBoth) { - currentFile.deletedLines++; - currentFile.addedLines++; - - currentLine.type = LINE_TYPE.INSERTS_AND_DELETES; - currentLine.oldNumber = oldLine++; - currentLine.newNumber = newLine++; - - currentBlock.lines.push(currentLine); - - } else if (isContextLine) { - - currentLine.type = LINE_TYPE.CONTEXT; - currentLine.oldNumber = oldLine++; - currentLine.newNumber = newLine++; - - currentBlock.lines.push(currentLine); - - } else if (isNewLine) { - currentFile.addedLines++; - - currentLine.type = LINE_TYPE.ALL_NEW; - currentLine.oldNumber = null; - currentLine.newNumber = newLine++; - - currentBlock.lines.push(currentLine); - - } else if (isRemovedLine) { - currentFile.deletedLines++; - - currentLine.type = LINE_TYPE.ALL_DELETED; - currentLine.oldNumber = oldLine++; - currentLine.newNumber = null; - - currentBlock.lines.push(currentLine); - - } else if (isLineWithInserts) { - currentFile.addedLines++; - - currentLine.type = LINE_TYPE.INSERTS; - currentLine.oldNumber = oldLine++; - currentLine.newNumber = newLine++; - - currentBlock.lines.push(currentLine); - - } else if (isLineWithDeletes) { - currentFile.deletedLines++; - - currentLine.type = LINE_TYPE.DELETES; - 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") || - startsWith(line, "---") || startsWith(line, "+++")) { - return; - } - - if (startsWith(line, "diff")) { - startFile(line); - } 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) { - - return "\n" + - " \n" + - " " + - "
" + escape(block.header) + "
" + - " \n" + - "\n" + - - block.lines.map(function (line) { - - var newLine = function () { - var lineData = {}; - lineData.oldLine = valueOrEmpty(line.oldNumber); - lineData.newLine = valueOrEmpty(line.newNumber); - - return lineData; - }; - - var escapedLine = escape(line.content); - - var lines = []; - var lineData = {}; - - switch (line.type) { - case LINE_TYPE.INSERTS: - case LINE_TYPE.ALL_NEW: - lineData = newLine(); - lineData.content = generateLineInsertions(escapedLine); - lineData.type = LINE_TYPE.INSERTS; - lines.push(lineData); - break; - case LINE_TYPE.DELETES: - case LINE_TYPE.ALL_DELETED: - lineData = newLine(); - lineData.content = generateLineDeletions(escapedLine); - lineData.type = LINE_TYPE.DELETES; - lines.push(lineData); - break; - case LINE_TYPE.INSERTS_AND_DELETES: - lineData = newLine(); - lineData.content = generateLineDeletions(escapedLine); - lineData.type = LINE_TYPE.DELETES; - lines.push(lineData); - - lineData = newLine(); - lineData.content = generateLineInsertions(escapedLine); - lineData.type = LINE_TYPE.INSERTS; - lines.push(lineData); - break; - default: - lineData = newLine(); - lineData.content = escapedLine; - lineData.type = LINE_TYPE.CONTEXT; - lines.push(lineData); - break; - } - - return lines.map(generateLineHtml).join("\n"); - }).join("\n"); - }).join("\n"); - }; - - var generateLineHtml = function (line) { - return "\n" + - " " + line.oldLine + "\n" + - " " + line.newLine + "\n" + - " " + - "
" + line.content + "
" + - " \n" + - "\n"; - }; - - /* - * Side By Side HTML (work in progress) - */ - - /* - * HTML Helpers - */ - - var getDiffName = function (oldFilename, newFilename) { - return oldFilename === newFilename ? newFilename : oldFilename + " -> " + newFilename; - }; - - var generateLineInsertions = function (line) { - return line.slice(0).replace(/(\[-.*?-\])/g, ""). - replace(/({\+(.*?)\+})/g, "$2"); - }; - - var generateLineDeletions = function (line) { - return line.slice(0).replace(/({\+.*?\+})/g, ""). - replace(/(\[-(.*?)-\])/g, "$2"); - }; - - var removeDeletes = function (line) { - return line.slice(0).replace(/({\+.*?\+})/g, ""). - replace(/(\[-.*?-\])/g, ""); - }; - - var removeInserts = function (line) { - return line.slice(0).replace(/({\+.*?\+})/g, ""). - replace(/(\[-.*?-\])/g, ""); - }; - - var cleanLine = function (line) { - return line.slice(0).replace(/({\+(.*?)\+})/g, "$2"). - replace(/(\[-(.*?)-\])/g, "$2"); - }; - - /* - * 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 : ""; - } - - /* singleton pattern */ - var instance; - return { - getInstance: function () { - if (instance === undefined) { - instance = new Diff2Html(); - /* Hide the constructor so the returned objected can't be new'd */ - instance.constructor = null; - } - return instance; - } - }; - - })(); - - window.Diff2Html = ClassVariable.getInstance(); - return window.Diff2Html; - -})(window); diff --git a/diff2html.min.css b/diff2html.min.css new file mode 100644 index 0000000..c17704e --- /dev/null +++ b/diff2html.min.css @@ -0,0 +1 @@ +.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-file-diff{overflow-x:scroll}.d2h-code-line{white-space:pre;padding-left:10px;padding-right:10px;height:18px;line-height:18px;margin-left:80px}.d2h-code-line del{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#f99;font-weight:700;border-radius:.2em}.d2h-code-line ins{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#6f7;font-weight:700;border-radius:.2em}.line-num1{display:inline-block;float:left;width:30px}.line-num2{display:inline-block;float:right;width:30px}.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 0 0;cursor:pointer}.d2h-code-linenumber:not(:first-child){border-width:0 1px}.d2h-del{background-color:#f7c8c8;border-color:#e9aeae}.d2h-ins{background-color:#ceffce;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 index ae7566a..9fb204e 100644 --- a/diff2html.min.js +++ b/diff2html.min.js @@ -1 +1 @@ -!function(window){var ClassVariable;return ClassVariable=function(){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:""}var LINE_TYPE={INSERTS:"d2h-ins",ALL_NEW:"d2h-all-ins",DELETES:"d2h-del",ALL_DELETED:"d2h-all-del",INSERTS_AND_DELETES:"d2h-ins-and-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)};var instance,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&&(files.push(currentFile),currentFile=null)},startFile=function(line){saveBlock(),saveFile(),currentFile={},currentFile.blocks=[],currentFile.deletedLines=0,currentFile.addedLines=0;var values=/^diff --git a\/(\S+) b\/(\S+).*$/.exec(line);currentFile.oldName=values[1],currentFile.newName=values[2]},startBlock=function(line){saveBlock();var values=/^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(line);currentBlock={},currentBlock.lines=[],currentBlock.oldStartLine=oldLine=values[2],currentBlock.newStartLine=newLine=values[4],currentBlock.header=line},createLine=function(line){var isLineWithInserts=/{\+.*?\+}/.exec(line),isNewLine=/^{\+.*?\+}$/.exec(line),isLineWithDeletes=/\[-.*?-\]/.exec(line),isRemovedLine=/^\[-.*?-\]$/.exec(line),isLineWithBoth=isLineWithInserts&&isLineWithDeletes,isContextLine=!isLineWithInserts&&!isLineWithDeletes,currentLine={};currentLine.content=line,isLineWithBoth?(currentFile.deletedLines++,currentFile.addedLines++,currentLine.type=LINE_TYPE.INSERTS_AND_DELETES,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):isContextLine?(currentLine.type=LINE_TYPE.CONTEXT,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):isNewLine?(currentFile.addedLines++,currentLine.type=LINE_TYPE.ALL_NEW,currentLine.oldNumber=null,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):isRemovedLine?(currentFile.deletedLines++,currentLine.type=LINE_TYPE.ALL_DELETED,currentLine.oldNumber=oldLine++,currentLine.newNumber=null,currentBlock.lines.push(currentLine)):isLineWithInserts?(currentFile.addedLines++,currentLine.type=LINE_TYPE.INSERTS,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):isLineWithDeletes&&(currentFile.deletedLines++,currentLine.type=LINE_TYPE.DELETES,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine))},diffLines=diffInput.split("\n");return diffLines.forEach(function(line){!line||startsWith(line,"*")||startsWith(line,"new")||startsWith(line,"index")||startsWith(line,"---")||startsWith(line,"+++")||(startsWith(line,"diff")?startFile(line):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){return'\n \n
'+escape(block.header)+"
\n\n"+block.lines.map(function(line){var newLine=function(){var lineData={};return lineData.oldLine=valueOrEmpty(line.oldNumber),lineData.newLine=valueOrEmpty(line.newNumber),lineData},escapedLine=escape(line.content),lines=[],lineData={};switch(line.type){case LINE_TYPE.INSERTS:case LINE_TYPE.ALL_NEW:lineData=newLine(),lineData.content=generateLineInsertions(escapedLine),lineData.type=LINE_TYPE.INSERTS,lines.push(lineData);break;case LINE_TYPE.DELETES:case LINE_TYPE.ALL_DELETED:lineData=newLine(),lineData.content=generateLineDeletions(escapedLine),lineData.type=LINE_TYPE.DELETES,lines.push(lineData);break;case LINE_TYPE.INSERTS_AND_DELETES:lineData=newLine(),lineData.content=generateLineDeletions(escapedLine),lineData.type=LINE_TYPE.DELETES,lines.push(lineData),lineData=newLine(),lineData.content=generateLineInsertions(escapedLine),lineData.type=LINE_TYPE.INSERTS,lines.push(lineData);break;default:lineData=newLine(),lineData.content=escapedLine,lineData.type=LINE_TYPE.CONTEXT,lines.push(lineData)}return lines.map(generateLineHtml).join("\n")}).join("\n")}).join("\n")},generateLineHtml=function(line){return'\n '+line.oldLine+'\n '+line.newLine+'\n
'+line.content+"
\n\n"},getDiffName=function(oldFilename,newFilename){return oldFilename===newFilename?newFilename:oldFilename+" -> "+newFilename},generateLineInsertions=function(line){return line.slice(0).replace(/(\[-.*?-\])/g,"").replace(/({\+(.*?)\+})/g,"$2")},generateLineDeletions=function(line){return line.slice(0).replace(/({\+.*?\+})/g,"").replace(/(\[-(.*?)-\])/g,"$2")};return{getInstance:function(){return void 0===instance&&(instance=new Diff2Html,instance.constructor=null),instance}}}(),window.Diff2Html=ClassVariable.getInstance(),window.Diff2Html}(window); \ No newline at end of file +!function(window){var ClassVariable;return ClassVariable=function(){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:""}var LINE_TYPE={INSERTS:"d2h-ins",ALL_NEW:"d2h-all-ins",DELETES:"d2h-del",ALL_DELETED:"d2h-all-del",INSERTS_AND_DELETES:"d2h-ins-and-del",CONTEXT:"d2h-cntx",INFO:"d2h-info"};Diff2Html.prototype.getPrettyHtmlFromDiff=function(diffInput){var diffJson=generateDiffJson(diffInput);return generateJsonHtml(diffJson,generateFileHtml)},Diff2Html.prototype.getJsonFromDiff=function(diffInput){return generateDiffJson(diffInput)},Diff2Html.prototype.getPrettyHtmlFromJson=function(diffJson){return generateJsonHtml(diffJson,generateFileHtml)};var instance,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&&(files.push(currentFile),currentFile=null)},startFile=function(line){saveBlock(),saveFile(),currentFile={},currentFile.blocks=[],currentFile.deletedLines=0,currentFile.addedLines=0;var values=/^diff --git a\/(\S+) b\/(\S+).*$/.exec(line);currentFile.oldName=values[1],currentFile.newName=values[2]},startBlock=function(line){saveBlock();var values;(values=/^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(line))?(oldLine=values[2],newLine=values[4]):(oldLine=0,newLine=0),currentBlock={},currentBlock.lines=[],currentBlock.oldStartLine=oldLine,currentBlock.newStartLine=newLine,currentBlock.header=line},createLine=function(line){var isLineWithInserts=/{\+.*?\+}/.exec(line),isNewLine=/^{\+.*?\+}$/.exec(line),isLineWithDeletes=/\[-.*?-\]/.exec(line),isRemovedLine=/^\[-.*?-\]$/.exec(line),isLineWithBoth=isLineWithInserts&&isLineWithDeletes,isContextLine=!isLineWithInserts&&!isLineWithDeletes,currentLine={};currentLine.content=line,isLineWithBoth?(currentFile.deletedLines++,currentFile.addedLines++,currentLine.type=LINE_TYPE.INSERTS_AND_DELETES,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):isContextLine?(currentLine.type=LINE_TYPE.CONTEXT,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):isNewLine?(currentFile.addedLines++,currentLine.type=LINE_TYPE.ALL_NEW,currentLine.oldNumber=null,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):isRemovedLine?(currentFile.deletedLines++,currentLine.type=LINE_TYPE.ALL_DELETED,currentLine.oldNumber=oldLine++,currentLine.newNumber=null,currentBlock.lines.push(currentLine)):isLineWithInserts?(currentFile.addedLines++,currentLine.type=LINE_TYPE.INSERTS,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):isLineWithDeletes&&(currentFile.deletedLines++,currentLine.type=LINE_TYPE.DELETES,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine))},diffLines=diffInput.split("\n");return diffLines.forEach(function(line){!line||startsWith(line,"*")||startsWith(line,"new")||startsWith(line,"index")||startsWith(line,"---")||startsWith(line,"+++")||(startsWith(line,"diff")?startFile(line):currentFile&&startsWith(line,"@@")?startBlock(line):currentBlock&&createLine(line))}),saveBlock(),saveFile(),files},generateJsonHtml=function(diffFiles,htmlTypeFunction){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 '+htmlTypeFunction(file)+" \n
\n
\n
\n
\n"}).join("\n")+"
\n"},generateFileHtml=function(file){return file.blocks.map(function(block){return'\n \n
'+escape(block.header)+"
\n\n"+block.lines.map(function(line){var newLine=function(){var lineData={};return lineData.oldLine=valueOrEmpty(line.oldNumber),lineData.newLine=valueOrEmpty(line.newNumber),lineData},escapedLine=escape(line.content),lines=[],lineData={};switch(line.type){case LINE_TYPE.INSERTS:case LINE_TYPE.ALL_NEW:lineData=newLine(),lineData.content=generateLineInsertions(escapedLine),lineData.type=LINE_TYPE.INSERTS,lines.push(lineData);break;case LINE_TYPE.DELETES:case LINE_TYPE.ALL_DELETED:lineData=newLine(),lineData.content=generateLineDeletions(escapedLine),lineData.type=LINE_TYPE.DELETES,lines.push(lineData);break;case LINE_TYPE.INSERTS_AND_DELETES:lineData=newLine(),lineData.content=generateLineDeletions(escapedLine),lineData.type=LINE_TYPE.DELETES,lines.push(lineData),lineData=newLine(),lineData.content=generateLineInsertions(escapedLine),lineData.type=LINE_TYPE.INSERTS,lines.push(lineData);break;default:lineData=newLine(),lineData.content=escapedLine,lineData.type=LINE_TYPE.CONTEXT,lines.push(lineData)}return lines.map(generateLineHtml).join("\n")}).join("\n")}).join("\n")},generateLineHtml=function(line){return'\n
'+line.oldLine+'
'+line.newLine+'
\n
'+line.content+"
\n\n"},getDiffName=function(oldFilename,newFilename){return oldFilename===newFilename?newFilename:oldFilename+" -> "+newFilename},generateLineInsertions=function(line){return line.slice(0).replace(/(\[-.*?-\])/g,"").replace(/({\+(.*?)\+})/g,"$2")},generateLineDeletions=function(line){return line.slice(0).replace(/({\+.*?\+})/g,"").replace(/(\[-(.*?)-\])/g,"$2")};return{getInstance:function(){return void 0===instance&&(instance=new Diff2Html,instance.constructor=null),instance}}}(),window.Diff2Html=ClassVariable.getInstance(),window.Diff2Html}(window); \ No newline at end of file diff --git a/index.html b/index.html index 2f20648..681f758 100644 --- a/index.html +++ b/index.html @@ -11,9 +11,11 @@ Last Update: Saturday 6 September 2014 --> - + + - + + - + +

Diff to HTML by rtfpessoa

+ +

Line by Line

+ +
+