diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f676a81 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +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 new file mode 100644 index 0000000..bafba7d --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Diff to Html by [rtfpessoa](https://github.com/rtfpessoa) + +Diff to Html generates pretty HTML diffs from regular command line tools output. + +### Inspiration + +This project is inspired in [pretty-diff](https://github.com/scottgonzalez/pretty-diff) by [Scott González](https://github.com/scottgonzalez). +This project started with some code from `pretty-diff` but it was then re-written and is now a +browser library. + +## Features + +* line-by-line diff (not side-by-side) + +* new and old line numbers + +* GitHub like style + +## Setup + +> Simply import the diff2html.js invoke: + + var diff2Html = Diff2Html.getInstance(); + diff2Html.generatePrettyDiff(exInput); + +> Check out the `template.html` for a complete example. + +## Contribution + +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. + +--- diff --git a/diff2html.js b/diff2html.js index 57c6b23..0a1756c 100644 --- a/diff2html.js +++ b/diff2html.js @@ -1,188 +1,210 @@ /* * - * Diff(git) to HTML + * Diff to HTML (diff2html.js) * Author: rtfpessoa * Date: Friday 29 August 2014 * */ -var exInput = 'diff --git a/conf/load-analysis.conf b/conf/load-analysis.conf\n' + -'new file mode 100644\n' + -'index 0000000..b170d2b\n' + -'--- /dev/null\n' + -'+++ b/conf/load-analysis.conf\n' + -'@@ -0,0 +1,5 @@\n' + -'+include "load-master-analysis.conf"\n' + -'+\n' + -'+\n' + -'+# if should manage tasks\n' + -'+codacy.isTaskManager=false\n' + -'diff --git a/conf/load-master-analysis.conf b/conf/load-master-analysis.conf\n' + -'new file mode 100644\n' + -'index 0000000..53e049f\n' + -'--- /dev/null\n' + -'+++ b/conf/load-master-analysis.conf\n' + -'@@ -0,0 +1,26 @@\n' + -'+include "load.conf"\n' + -'+\n' + -'+# Sockets\n' + -'+# ~~~~~\n' + -'+codacy.sockets.remote=true\n' + -'+codacy.sockets.url="http://load.codacy.com/socket/notify"\n' + -'+\n' + -'+# enables the analysis server to run\n' + -'+codacy.isAnalysisServer=true\n' + -'+# if should manage analysis server work\n' + -'+codacy.isServerManager.active=false\n' + -'+# if should manage tasks\n' + -'+codacy.isTaskManager=true\n' + -'+\n' + -'+\n' + -'+# enables the repositoryListener to run\n' + -'+codacy.isRepositoryListener=true\n' + -'+#enables the payment system pooling for stripe payments\n' + -'+codacy.payments.active=false\n' + -'+\n' + -'+#third party\n' + -'+codacy.thirdPartyNotification.akka.active=true\n' + -'+codacy.thirdPartyNotification.users.active=false\n' + -'+\n' + -'+# DB values\n' + -'+db.default.hikaricp.file="conf/database/hikaricp.production-server.properties"\n' + -'diff --git a/conf/load-website.conf b/conf/load-website.conf\n' + -'new file mode 100644\n' + -'index 0000000..aa43adb\n' + -'--- /dev/null\n' + -'+++ b/conf/load-website.conf\n' + -'@@ -0,0 +1,26 @@\n' + -'+include "load.conf"\n' + -'+\n' + -'+# Sockets\n' + -'+# ~~~~~\n' + -'+codacy.sockets.remote=false\n' + -'+codacy.sockets.url=""\n' + -'+\n' + -'+# enables the analysis server to run\n' + -'+codacy.isAnalysisServer=false\n' + -'+# if should manage analysis server work\n' + -'+codacy.isServerManager.active=true\n' + -'+# if should manage tasks\n' + -'+codacy.isTaskManager=false\n' + -'+\n' + -'+\n' + -'+# enables the repositoryListener to run\n' + -'+codacy.isRepositoryListener=false\n' + -'+#enables the payment system pooling for stripe payments\n' + -'+codacy.payments.active=true\n' + -'+\n' + -'+#third party\n' + -'+codacy.thirdPartyNotification.akka.active=false\n' + -'+codacy.thirdPartyNotification.users.active=true\n' + -'+\n' + -'+#DB values\n' + -'+db.default.hikaricp.file="conf/database/hikaricp.production-website.properties"\n' + -'diff --git a/conf/load.conf b/conf/load.conf\n' + -'new file mode 100644\n' + -'index 0000000..8463b41\n' + -'--- /dev/null\n' + -'+++ b/conf/load.conf\n' + -'@@ -0,0 +1,27 @@\n' + -'+include "application.conf"\n' + -'+include "auth/integration.conf"\n' + -'+include "payments/test.conf"\n' + -'+include "actors/production.conf"\n' + -'+\n' + -'+# Application configuration\n' + -'+# ~~~~~~~~~~~~~~~~~~~~~~~~~\n' + -'+application.mode=prod\n' + -'+\n' + -'+#Prevents integration from sending emails. DO NOT CHANGE\n' + -'+mail.debug=true\n' + -'+\n' + -'+# Codacy\n' + -'+# ~~~~~\n' + -'+codacy.js.extension=".js"\n' + -'+codacy.keys.location="/data/codacy/keys/"\n' + -'+codacy.repository.location="/data/codacy/repos/"\n' + -'+codacy.secure=false\n' + -'+codacy.algorithms.location="public/javascripts/_engine/_algos/"\n' + -'+\n' + -'+codacy.url="http://load.codacy.com"\n' + -'+\n' + -'+db.default.maxActive=20\n' + -'+\n' + -'+db.default.url="jdbc:postgresql://loaddb.codacy.com:5432/codacy"\n' + -'+db.default.user="codacy"\n' + -'+db.default.password="codacy"'; +(function($, window) { + var ClassVariable; -function generatePrettyDiff( diffInput ) { - var diffHtml = ""; - var diffFiles = splitByFile( diffInput ); + ClassVariable = (function() { - for ( var file in diffFiles ) { - diffHtml += "
" + escape( line ) + ""; - }).join( "\n" ); - }; -}(); + /* add previous block(if exists) before start a new file */ + if (currentBlock) { + currentFile.blocks.push(currentBlock); + currentBlock = null; + } + + /* add previous file(if exists) before start a new one */ + if (currentFile) { + files.push(currentFile); + currentFile = null; + } + + /* create file structure */ + currentFile = {}; + currentFile.blocks = []; + + /* 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]; + + } else if (line.indexOf("@@") === 0) { + /* Diff Block Header Line */ + + var values = /^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(line); + + /* add previous block(if exists) before start a new one */ + if (currentBlock) { + currentFile.blocks.push(currentBlock); + currentBlock = null; + } + + /* create block metadata */ + currentBlock = {}; + currentBlock.lines = []; + currentBlock.oldStartLine = oldLine = values[2]; + currentBlock.newStartLine = newLine = values[4]; + currentBlock.deletedLines = values[3]; + currentBlock.addedLines = values[5]; + + /* create block header line */ + var currentLine = {}; + currentLine.type = CSS_STYLES.INFO; + currentLine.content = line; + currentLine.oldNumber = BLOCK_HEADER_LINE; + currentLine.newNumber = BLOCK_HEADER_LINE; + + /* add line to block */ + currentBlock.lines.push(currentLine); + + } else { + /* Regular Diff Line */ + + var currentLine = {}; + currentLine.content = line; + + if (line.indexOf("+") === 0) { + currentLine.type = CSS_STYLES.NEW; + currentLine.oldNumber = null; + currentLine.newNumber = newLine++; + + } else if (line.indexOf("-") === 0) { + currentLine.type = CSS_STYLES.DELETED; + currentLine.oldNumber = oldLine++; + currentLine.newNumber = null; + + } else { + currentLine.type = CSS_STYLES.CONTEXT; + currentLine.oldNumber = oldLine++; + currentLine.newNumber = newLine++; + + } + + /* add line to block */ + currentBlock.lines.push(currentLine); + } + }); + + /* add previous block(if exists) before start a new file */ + if (currentBlock) { + currentFile.blocks.push(currentBlock); + currentBlock = null; + } + + /* add previous file(if exists) before start a new one */ + if (currentFile) { + files.push(currentFile); + currentFile = null; + } + + return files; + }; + + var generateHtml = function( diffFiles ) { + return diffFiles.map(function( file ) { + var fileHeader = file.oldName === file.newName ? file.newName : file.oldName + " -> " + file.newName + + return "