char by char diff & clean a bit

This commit is contained in:
Rodrigo Fernandes 2014-08-30 23:16:38 +01:00
parent 66970564dc
commit cc9d271cc0
5 changed files with 426 additions and 651 deletions

16
.gitignore vendored Normal file
View file

@ -0,0 +1,16 @@
# Eclipse
.classpath
.project
.settings/
# Intellij
.idea/
*.iml
*.iws
# Mac
.DS_Store
# Maven
log/
target/

View file

@ -3,254 +3,290 @@
* Diff to HTML (diff2html.js) * Diff to HTML (diff2html.js)
* Author: rtfpessoa * Author: rtfpessoa
* Date: Friday 29 August 2014 * Date: Friday 29 August 2014
* Last Update: Saturday 30 August 2014
* *
* Useful commands: * Diff command:
* git diff HEAD~1 * git diff --word-diff-regex=. HEAD~1
* git diff HEAD~1 --word-diff-regex='[[:alnum:]]+|[^[:space:]]'
* git diff HEAD~1 --word-diff-regex=.
*/ */
(function($, window) { (function (window) {
var ClassVariable; var ClassVariable;
ClassVariable = (function() { ClassVariable = (function () {
var CSS_STYLES = { var CSS_STYLES = {
INFO: "info", INFO: "info",
CONTEXT: "context", CONTEXT: "context",
NEW: "insert", NEW: "insert",
DELETED: "delete" DELETED: "delete"
}; };
var BLOCK_HEADER_LINE = "..."; var BLOCK_HEADER_LINE = "...";
var wordDiffParser = WordDiffParser.getInstance(); function Diff2Html() {
function Diff2Html() {}
Diff2Html.prototype.generatePrettyDiff = function(diffInput, wordDiffInput) {
var diffFiles = splitByFile(diffInput);
var changedWords = wordDiffParser.generateChangedWords(wordDiffInput);
if (!changedWords) {
changedWords = {};
}
var html = generateHtml(diffFiles, changedWords);
return html;
};
var splitByFile = function(diffInput) {
var files = [],
currentFile = null,
currentBlock = null,
oldLine = null,
newLine = null;
diffInput.split("\n").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 || line.charAt(0) === "*" ||
line.indexOf("new") === 0 ||
line.indexOf("index") === 0 ||
line.indexOf("---") === 0 ||
line.indexOf("+++") === 0) {
return;
} }
if (line.indexOf("diff") === 0) { /*
/* File Diff Line */ * Generates pretty html from string diff input
*/
Diff2Html.prototype.getPrettyHtmlFromDiff = function (diffInput) {
var diffJson = generateDiffJson(diffInput);
return generateJsonHtml(diffJson);
};
/* add previous block(if exists) before start a new file */ /*
if (currentBlock) { * Generates json object from string diff input
currentFile.blocks.push(currentBlock); */
currentBlock = null; Diff2Html.prototype.getJsonFromDiff = function (diffInput) {
} return generateDiffJson(diffInput);
};
/* add previous file(if exists) before start a new one */ /*
if (currentFile) { * Generates pretty html from a json object
files.push(currentFile); */
currentFile = null; Diff2Html.prototype.getPrettyHtmlFromJson = function (diffJson) {
} return generateJsonHtml(diffJson);
};
/* create file structure */ var generateDiffJson = function (diffInput) {
currentFile = {}; var files = [],
currentFile.blocks = []; currentFile = null,
currentFile.deletedLines = 0, currentBlock = null,
currentFile.addedLines = 0; oldLine = null,
newLine = null;
/* save file paths, before and after the diff */ var saveBlock = function () {
var values = /^diff --git a\/(\S+) b\/(\S+).*$/.exec(line); /* add previous block(if exists) before start a new file */
currentFile.oldName = values[1]; if (currentBlock) {
currentFile.newName = values[2]; currentFile.blocks.push(currentBlock);
currentBlock = null;
}
};
} else if (line.indexOf("@@") === 0) { var saveFile = function () {
/* Diff Block Header Line */ /* add previous file(if exists) before start a new one */
if (currentFile) {
files.push(currentFile);
currentFile = null;
}
};
var values = /^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(line); var startFile = function (line) {
saveBlock();
saveFile();
/* add previous block(if exists) before start a new one */ /* create file structure */
if (currentBlock) { currentFile = {};
currentFile.blocks.push(currentBlock); currentFile.blocks = [];
currentBlock = null; currentFile.deletedLines = 0;
} currentFile.addedLines = 0;
/* create block metadata */ /* save file paths, before and after the diff */
currentBlock = {}; var values = /^diff --git a\/(\S+) b\/(\S+).*$/.exec(line);
currentBlock.lines = []; currentFile.oldName = values[1];
currentBlock.oldStartLine = oldLine = values[2]; currentFile.newName = values[2];
currentBlock.newStartLine = newLine = values[4]; };
/* update file added and deleted lines */
currentFile.deletedLines += currentBlock.deletedLines = parseInt(values[3], 10);
currentFile.addedLines += currentBlock.addedLines = parseInt(values[5], 10);
/* create block header line */ var startBlock = function (line) {
var currentLine = {}; saveBlock();
currentLine.type = CSS_STYLES.INFO;
currentLine.content = line;
currentLine.oldNumber = BLOCK_HEADER_LINE;
currentLine.newNumber = BLOCK_HEADER_LINE;
/* add line to block */ var values = /^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(line);
currentBlock.lines.push(currentLine);
} else { /* create block metadata */
/* Regular Diff Line */ currentBlock = {};
currentBlock.lines = [];
currentBlock.oldStartLine = oldLine = values[2];
currentBlock.newStartLine = newLine = values[4];
var currentLine = {}; /* create block header line */
currentLine.content = line; var currentLine = {};
currentLine.type = CSS_STYLES.INFO;
currentLine.content = line;
currentLine.oldNumber = BLOCK_HEADER_LINE;
currentLine.newNumber = BLOCK_HEADER_LINE;
if (line.indexOf("+") === 0) { /* add line to block */
currentLine.type = CSS_STYLES.NEW; currentBlock.lines.push(currentLine);
currentLine.oldNumber = null; };
currentLine.newNumber = newLine++;
} else if (line.indexOf("-") === 0) { var createLine = function (line) {
currentLine.type = CSS_STYLES.DELETED; var isLineWithInserts = /{\+.*?\+}/.exec(line);
currentLine.oldNumber = oldLine++; var isLineWithDeletes = /\[-.*?-\]/.exec(line);
currentLine.newNumber = null; var isNewLine = /^{\+.*?\+}$/.exec(line);
var isContextLine = !isLineWithInserts && !isLineWithDeletes;
} else { var currentLine = {};
currentLine.type = CSS_STYLES.CONTEXT;
currentLine.oldNumber = oldLine++;
currentLine.newNumber = newLine++;
} if (isContextLine) {
currentLine = {};
currentLine.type = CSS_STYLES.CONTEXT;
currentLine.oldNumber = oldLine++;
currentLine.newNumber = newLine++;
currentLine.content = line;
/* add line to block */ currentBlock.lines.push(currentLine);
currentBlock.lines.push(currentLine); } else {
if (isLineWithDeletes) {
currentFile.deletedLines++;
currentLine = {};
currentLine.type = CSS_STYLES.DELETED;
currentLine.oldNumber = oldLine++;
currentLine.newNumber = null;
currentLine.content = line;
currentBlock.lines.push(currentLine);
}
if (isLineWithInserts) {
currentFile.addedLines++;
currentLine = {};
currentLine.type = CSS_STYLES.NEW;
currentLine.oldNumber = null;
currentLine.newNumber = newLine++;
currentLine.content = line;
/* fix line numbers when new chars but no deletes and no whole new line */
if (isLineWithInserts && !isLineWithDeletes && !isNewLine) {
currentFile.deletedLines++;
currentLine.oldNumber = oldLine++;
}
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;
};
var generateJsonHtml = function (diffFiles) {
return "<div id=\"wrapper\">\n" +
diffFiles.map(function (file) {
return "<div class=\"file-wrapper\">\n" +
" <div class=\"file-header\">\n" +
" <div class=\"file-stats\">\n" +
" <span class=\"lines-added\">+" + file.addedLines + "</span>\n" +
" <span class=\"lines-deleted\">-" + file.deletedLines + "</span>\n" +
" </div>\n" +
" <div class=\"file-name\">" + getDiffName(file.oldName, file.newName) + "</div>\n" +
" </div>\n" +
" <div class=\"file-diff\">\n" +
" <div class=\"code-wrapper\">\n" +
" <table class=\"diff-table\">\n" +
" <tbody>\n" +
" " + generateFileHtml(file) +
" </tbody>\n" +
" </table>\n" +
" </div>\n" +
" </div>\n" +
" </div>\n";
}).join("\n") +
"</div>\n";
};
var getDiffName = function (oldFilename, newFilename) {
return oldFilename === newFilename ? newFilename : oldFilename + " -> " + newFilename;
};
var generateFileHtml = function (file) {
return file.blocks.map(function (block) {
return block.lines.map(function (line) {
var oldLine = valueOrEmpty(line.oldNumber);
var newLine = valueOrEmpty(line.newNumber);
var escapedLine = escape(line.content);
if (line.type === CSS_STYLES.NEW) {
escapedLine = generateLineInsertions(escapedLine);
} else if (line.type === CSS_STYLES.DELETED) {
escapedLine = generateLineDeletions(escapedLine);
}
return "<tr>\n" +
" <td class=\"code-linenumber " + line.type + "\">" + oldLine + "</td>\n" +
" <td class=\"code-linenumber " + line.type + "\">" + newLine + "</td>\n" +
" <td class=\"code-line " + line.type + "\"><pre class=\"" + line.type + "\">" + escapedLine + "</pre></td>\n" +
"</tr>\n";
}).join("\n");
}).join("\n");
};
var generateLineInsertions = function (line) {
return line.replace(/(\[-.*?-\])/g, "").
replace(/({\+(.*?)\+})/g, "<ins>$2</ins>");
};
var generateLineDeletions = function (line) {
return line.replace(/({\+.*?\+})/g, "").
replace(/(\[-(.*?)-\])/g, "<del>$2</del>");
};
/*
* Utils
*/
function escape(str) {
return str
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\t/g, " ");
} }
});
/* add previous block(if exists) before start a new file */ function startsWith(str, start) {
if (currentBlock) { return str.indexOf(start) === 0;
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, changedWords) {
return diffFiles.map(function(file, index) {
var fileHeader = file.oldName === file.newName ? file.newName : file.oldName + " -> " + file.newName
return "<div class=\"file-wrapper\">" +
" <div class=\"file-header\">" +
" <div class=\"file-stats\">" +
" <span class=\"lines-added\">+" + file.addedLines + "</span>" +
" <span class=\"lines-deleted\">-" + file.deletedLines + "</span>" +
" </div>" +
" <div class=\"file-name\">" + fileHeader + "</div>" +
" </div>" +
" <div class=\"file-diff\">" +
" <div class=\"code-wrapper\">" +
" <table class=\"diff-table\">" +
" <tbody>" +
generateFileHtml(file, changedWords[index]) +
" </tbody>" +
" </table>" +
" </div>" +
" </div>" +
"</div>";
});
};
var generateFileHtml = function(file, changedWords) {
return file.blocks.map(function(block) {
return block.lines.map(function(line) {
var oldLine = line.oldNumber ? line.oldNumber : "";
var newLine = line.newNumber ? line.newNumber : "";
var oldWords = [];
var newWords = [];
if (oldLine && oldLine !== BLOCK_HEADER_LINE &&
changedWords && changedWords.deletedWords && changedWords.deletedWords[oldLine]) {
oldWords = changedWords.deletedWords[oldLine];
}
if (newLine && newLine !== BLOCK_HEADER_LINE &&
changedWords && changedWords.addedWords && changedWords.addedWords[newLine]) {
newWords = changedWords.addedWords[newLine];
}
//var newLine = escape(line.content);
var newCodeLine = line.content;
newCodeLine = markWords(oldWords, newCodeLine, "del");
newCodeLine = markWords(newWords, newCodeLine, "ins");
return "<tr>" +
" <td class=\"code-linenumber " + line.type + "\">" + oldLine + "</td>" +
" <td class=\"code-linenumber " + line.type + "\">" + newLine + "</td>" +
" <td class=\"code-line " + line.type + "\"><pre>" + newCodeLine + "</pre></td>" +
"</tr>";
}).join("\n");
}).join("\n");
};
var markWords = function(words, line, clazz) {
var newLine = line;
words.forEach(function(word) {
newLine = newLine.replace(word, "<" + clazz + ">" + word + "</" + clazz + ">");
});
return newLine;
};
var escape = function(str) {
return str
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\t/g, " ");
};
/* 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;
}
};
})(); function valueOrEmpty(value) {
return value ? value : "";
}
window.Diff2Html = ClassVariable; /* singleton pattern */
return window.Diff2Html; 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;
}
};
})(jQuery, window); })();
window.Diff2Html = ClassVariable.getInstance();
return window.Diff2Html;
})(window);

215
style.css
View file

@ -3,186 +3,183 @@
* Diff to HTML (style.css) * Diff to HTML (style.css)
* Author: rtfpessoa * Author: rtfpessoa
* Date: Friday 29 August 2014 * Date: Friday 29 August 2014
* Last Update: Saturday 30 August 2014
* *
*/ */
body { body {
text-align: center; text-align: center;
} }
#wrapper { #wrapper {
display: inline-block; display: inline-block;
margin-top: 1em; margin-top: 1em;
text-align: left; text-align: left;
width: 920px; width: 920px;
} }
.file-wrapper { .file-wrapper {
border: 1px solid #ddd; border: 1px solid #ddd;
border-radius: 3px; border-radius: 3px;
margin-bottom: 1em; margin-bottom: 1em;
} }
.file-header { .file-header {
padding: 5px 10px; padding: 5px 10px;
text-shadow: 0 1px 0 #fff; text-shadow: 0 1px 0 #fff;
border-bottom: 1px solid #d8d8d8; border-bottom: 1px solid #d8d8d8;
background-color: #f7f7f7; background-color: #f7f7f7;
border-top-left-radius: 4px; border-top-left-radius: 4px;
border-top-right-radius: 4px; border-top-right-radius: 4px;
box-sizing: border-box; box-sizing: border-box;
} }
.file-name { .file-name {
display: inline-block; display: inline-block;
font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"; font: 13px Helvetica, arial, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol";
line-height: 1.4; height: 33px;
height: 33px; line-height: 33px;
line-height: 33px;
} }
.file-stats { .file-stats {
display: inline-block; display: inline-block;
font-family: monospace,monospace; font-family: monospace, monospace;
font-size: 12px; font-size: 12px;
vertical-align: middle; vertical-align: middle;
} }
.lines-added { .lines-added {
background-color: #ceffce; background-color: #ceffce;
border-color: #b4e2b4; border: 1px solid #b4e2b4;
border: 1px solid; color: #399839;
color: #399839; border-radius: 5px 0 0 5px;
border-radius: 5px 0 0 5px; padding: 5px;
padding: 5px;
} }
.lines-deleted { .lines-deleted {
background-color: #f7c8c8; background-color: #f7c8c8;
border-color: #e9aeae; border: 1px solid #e9aeae;
border: 1px solid; color: #c33;
color: #c33; border-radius: 0 5px 5px 0;
border-radius: 0 5px 5px 0; padding: 5px;
padding: 5px;
} }
.file-diff { .file-diff {
overflow: auto; overflow: auto;
} }
.file-diff > div { .file-diff > div {
width: 100%: width: 100%;
} }
pre { pre {
margin: 0; margin: 0;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px; font-size: 12px;
line-height: 18px; line-height: 18px;
text-indent: 5px; text-indent: 5px;
color: #333; color: #333;
border: solid #eeeeee; border: solid #eeeeee;
border-width: 0 1px 0 0; border-width: 0 1px 0 0;
cursor: pointer; cursor: pointer;
} }
.code-wrapper { .code-wrapper {
overflow-x: auto; overflow-x: auto;
overflow-y: hidden; overflow-y: hidden;
border-bottom-left-radius: 3px; border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px; border-bottom-right-radius: 3px;
} }
.diff-table { .diff-table {
border-collapse: separate; border-collapse: separate;
} }
table { table {
border-collapse: collapse; border-collapse: collapse;
border-spacing: 0; border-spacing: 0;
} }
tbody { tbody {
display: table-row-group; display: table-row-group;
vertical-align: middle; vertical-align: middle;
border-color: inherit; border-color: inherit;
} }
tr { tr {
display: table-row; display: table-row;
vertical-align: inherit; vertical-align: inherit;
border-color: inherit; border-color: inherit;
} }
td, th { td, th {
padding: 0; padding: 0;
}
pre {
text-indent: 0;
border: none;
} }
.code-linenumber { .code-linenumber {
width: 1%; width: 1%;
min-width: 50px; min-width: 25px;
padding-left: 10px; padding-left: 10px;
padding-right: 10px; padding-right: 10px;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px; font-size: 12px;
line-height: 18px; line-height: 18px;
color: rgba(0,0,0,0.3); color: rgba(0, 0, 0, 0.3);
vertical-align: top; vertical-align: top;
text-align: right; text-align: right;
border: solid #eeeeee; border: solid #eeeeee;
border-width: 0 1px 0 0; border-width: 0 1px 0 0;
cursor: pointer; cursor: pointer;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
} }
.code-line { .code-line {
position: relative; position: relative;
padding-left: 10px; padding-left: 10px;
padding-right: 10px; padding-right: 10px;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px; font-size: 12px;
color: #333; color: #333;
vertical-align: top; vertical-align: top;
white-space: pre; white-space: pre;
overflow: visible; overflow: visible;
} }
.delete { .delete {
vertical-align: middle; vertical-align: middle;
background-color: #f7c8c8; background-color: #f7c8c8;
border-color: #e9aeae; border-color: #e9aeae;
} }
.insert { .insert {
vertical-align: middle; vertical-align: middle;
background-color: #ceffce; background-color: #ceffce;
border-color: #b4e2b4; border-color: #b4e2b4;
} }
.info { .info {
background-color: #f8fafd; background-color: #f8fafd;
vertical-align: middle; vertical-align: middle;
color: rgba(0,0,0,0.3); color: rgba(0, 0, 0, 0.3);
border-color: #d5e4f2; border-color: #d5e4f2;
} }
.code-line del { .code-line del {
display: inline-block; display: inline-block;
margin-top: -1px; margin-top: -1px;
text-decoration: none; text-decoration: none;
background-color: #ffb6ba; background-color: #ffb6ba;
font-weight: 700; font-weight: 700;
} }
.code-line ins { .code-line ins {
display: inline-block; display: inline-block;
margin-top: -1px; margin-top: -1px;
text-decoration: none; text-decoration: none;
background-color: #97f295; background-color: #97f295;
font-weight: 700; font-weight: 700;
} }

View file

@ -1,199 +1,61 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Diff to HTML by rtfpessoa</title> <title>Diff to HTML by rtfpessoa</title>
<!-- <!--
Diff to HTML (template.html) Diff to HTML (template.html)
Author: rtfpessoa Author: rtfpessoa
Date: Friday 29 August 2014 Date: Friday 29 August 2014
--> Last Update: Saturday 30 August 2014
-->
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="style.css">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" src="word-diff-parser.js"></script> <script type="text/javascript" src="diff2html.js"></script>
<script type="text/javascript" src="diff2html.js"></script> <script>
<script> var exInput = 'diff --git a/components/enginePlugins/src/main/scala/metrics/Tools.scala b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
var exInput = 'diff --git a/components/enginePlugins/src/main/scala/metrics/Tools.scala b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' + 'index c68cfb8..a1edc93 100644\n' +
'index c68cfb8..a1edc93 100644\n' + '--- a/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' + '+++ b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' + '@@ -1,6 +1,7 @@\n' +
'@@ -1,6 +1,7 @@\n' + 'package metrics\n' +
' package metrics\n' + ' \n' +
' \n' + 'import java.io\n' +
' import java.io\n' + '{+import java.nio.file.Files._+}\n' +
'+import java.nio.file.Files._\n' + ' \n' +
' \n' + 'import framework.common.Logger.LoggerSystem\n' +
' import framework.common.Logger.LoggerSystem\n' + 'import org.apache.commons.io.FileUtils\n' +
' import org.apache.commons.io.FileUtils\n' + '@@ -43,7 +44,7 @@ object Tools {\n' +
'@@ -43,7 +44,7 @@ object Tools {\n' + ' }\n' +
' }\n' + ' \n' +
' \n' + ' def withTempDir[A](block: io.File => A): A = {\n' +
' def withTempDir[A](block: io.File => A): A = {\n' + ' val dir = [-io.File.-]createTemp[-F-]{+D+}i[-l-]{+r+}e{+ctory+}("codacy"[-, "-]{+)+}.t[-mp")-]{+oFile+}\n' +
'- val dir = io.File.createTempFile("codacy", ".tmp")\n' + ' \n' +
'+ val dir = createTempDirectory("codacy").toFile\n' + ' val res = block(dir)\n' +
' \n' + ' FileUtils.deleteDirectory(dir)\n' +
' val res = block(dir)\n' + 'diff --git a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
' FileUtils.deleteDirectory(dir)\n' + 'index 5935d36..4e7e085 100644\n' +
'diff --git a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' + '--- a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'index 5935d36..4e7e085 100644\n' + '+++ b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' + '@@ -24,7 +24,7 @@ object Plato extends PlatoReads {\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' + ' withTempDir {\n' +
'@@ -24,7 +24,7 @@ object Plato extends PlatoReads {\n' + ' outputDirectory =>\n' +
' withTempDir {\n' + ' runTool(directory, files, outputDirectory) match {\n' +
' outputDirectory =>\n' + ' case ({+Some(+}_{+)+}, tmpFileMapper) =>\n' +
' runTool(directory, files, outputDirectory) match {\n' + ' val fileFolders = new io.File(outputDirectory, "files")\n' +
'- case (_, tmpFileMapper) =>\n' + ' \n' +
'+ case (Some(_), tmpFileMapper) =>\n' + ' fileFolders.list().flatMap {';
' val fileFolders = new io.File(outputDirectory, "files")\n' +
' \n' +
' fileFolders.list().flatMap {';
var exWordInput = 'diff --git a/components/enginePlugins/src/main/scala/metrics/Tools.scala b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' + $(document).ready(function () {
'index c68cfb8..a1edc93 100644\n' + var content = Diff2Html.getPrettyHtmlFromDiff(exInput);
'--- a/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' + $("body").html(content);
'+++ b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' + });
'@@ -1,6 +1,7 @@\n' + </script>
'package metrics\n' +
' \n' +
'import java.io\n' +
'{+import java.nio.file.Files._+}\n' +
' \n' +
'import framework.common.Logger.LoggerSystem\n' +
'import org.apache.commons.io.FileUtils\n' +
'@@ -43,7 +44,7 @@ object Tools {\n' +
' }\n' +
' \n' +
' def withTempDir[A](block: io.File => A): A = {\n' +
' val dir = [-io.File.createTempFile("codacy", ".tmp")-]{+createTempDirectory("codacy").toFile+}\n' +
' \n' +
' val res = block(dir)\n' +
' FileUtils.deleteDirectory(dir)\n' +
'diff --git a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'index 5935d36..4e7e085 100644\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'@@ -24,7 +24,7 @@ object Plato extends PlatoReads {\n' +
' withTempDir {\n' +
' outputDirectory =>\n' +
' runTool(directory, files, outputDirectory) match {\n' +
' case [-(_,-]{+(Some(_),+} tmpFileMapper) =>\n' +
' val fileFolders = new io.File(outputDirectory, "files")\n' +
' \n' +
' fileFolders.list().flatMap {';
var exDiffInput = 'diff --git a/components/enginePlugins/src/main/scala/metrics/Tools.scala b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'index c68cfb8..a1edc93 100644\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'@@ -1,6 +1,7 @@\n' +
' package metrics\n' +
' \n' +
' import java.io\n' +
'+import java.nio.file.Files._\n' +
' \n' +
' import framework.common.Logger.LoggerSystem\n' +
' import org.apache.commons.io.FileUtils\n' +
'@@ -43,7 +44,7 @@ object Tools {\n' +
' }\n' +
' \n' +
' def withTempDir[A](block: io.File => A): A = {\n' +
'- val dir = io.File.createTempFile("codacy", ".tmp")\n' +
'+ val dir = createTempDirectory("codacy").toFile\n' +
' \n' +
' val res = block(dir)\n' +
' FileUtils.deleteDirectory(dir)\n' +
'diff --git a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'index 5935d36..4e7e085 100644\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'@@ -24,7 +24,7 @@ object Plato extends PlatoReads {\n' +
' withTempDir {\n' +
' outputDirectory =>\n' +
' runTool(directory, files, outputDirectory) match {\n' +
'- case (_, tmpFileMapper) =>\n' +
'+ case (Some(_), tmpFileMapper) =>\n' +
' val fileFolders = new io.File(outputDirectory, "files")\n' +
' \n' +
' fileFolders.list().flatMap {';
var exBetterDiffInput = 'diff --git a/components/enginePlugins/src/main/scala/metrics/Tools.scala b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'index c68cfb8..a1edc93 100644\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'@@ -1,6 +1,7 @@\n' +
'package metrics\n' +
' \n' +
'import java.io\n' +
'{+import java.nio.file.Files._+}\n' +
' \n' +
'import framework.common.Logger.LoggerSystem\n' +
'import org.apache.commons.io.FileUtils\n' +
'@@ -43,7 +44,7 @@ object Tools {\n' +
' }\n' +
' \n' +
' def withTempDir[A](block: io.File => A): A = {\n' +
' val dir = [-io.File.createTempFile-]{+createTempDirectory+}("codacy"[-, ".tmp"-]){+.toFile+}\n' +
' \n' +
' val res = block(dir)\n' +
' FileUtils.deleteDirectory(dir)\n' +
'diff --git a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'index 5935d36..4e7e085 100644\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'@@ -24,7 +24,7 @@ object Plato extends PlatoReads {\n' +
' withTempDir {\n' +
' outputDirectory =>\n' +
' runTool(directory, files, outputDirectory) match {\n' +
' case ({+Some(+}_{+)+}, tmpFileMapper) =>\n' +
' val fileFolders = new io.File(outputDirectory, "files")\n' +
' \n' +
' fileFolders.list().flatMap {';
var exEvenBetterDiffInput = 'diff --git a/components/enginePlugins/src/main/scala/metrics/Tools.scala b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'index c68cfb8..a1edc93 100644\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/Tools.scala\n' +
'@@ -1,6 +1,7 @@\n' +
'package metrics\n' +
' \n' +
'import java.io\n' +
'{+import java.nio.file.Files._+}\n' +
' \n' +
'import framework.common.Logger.LoggerSystem\n' +
'import org.apache.commons.io.FileUtils\n' +
'@@ -43,7 +44,7 @@ object Tools {\n' +
' }\n' +
' \n' +
' def withTempDir[A](block: io.File => A): A = {\n' +
' val dir = [-io.File.-]createTemp[-F-]{+D+}i[-l-]{+r+}e{+ctory+}("codacy"[-, "-]{+)+}.t[-mp")-]{+oFile+}\n' +
' \n' +
' val res = block(dir)\n' +
' FileUtils.deleteDirectory(dir)\n' +
'diff --git a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'index 5935d36..4e7e085 100644\n' +
'--- a/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'+++ b/components/enginePlugins/src/main/scala/metrics/js/Plato.scala\n' +
'@@ -24,7 +24,7 @@ object Plato extends PlatoReads {\n' +
' withTempDir {\n' +
' outputDirectory =>\n' +
' runTool(directory, files, outputDirectory) match {\n' +
' case ({+Some(+}_{+)+}, tmpFileMapper) =>\n' +
' val fileFolders = new io.File(outputDirectory, "files")\n' +
' \n' +
' fileFolders.list().flatMap {';
$( document ).ready(function() {
$("#wrapper").html(Diff2Html.getInstance().generatePrettyDiff(exDiffInput, exEvenBetterDiffInput));
//console.log(WordDiffParser.getInstance().generateChangedWords(exWordInput));
});
</script>
</head> </head>
<body> <body>
<div id="wrapper">
</div>
</body> </body>
</html> </html>

View file

@ -1,136 +0,0 @@
/*
*
* Word Diff Parser (word-diff-parser.js)
* Author: rtfpessoa
* Date: Saturday 30 August 2014
*
*/
(function($, window) {
var ClassVariable;
ClassVariable = (function() {
function WordDiffParser() {}
WordDiffParser.prototype.generateChangedWords = function(wordDiffInput) {
return wordDiffInput ? parseChangedWords(wordDiffInput) : null;
};
var parseChangedWords = function(wordDiffInput) {
var files = [],
currentFile = null,
oldLine = null,
newLine = null;
wordDiffInput.split("\n").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 || line.charAt(0) === "*" ||
line.indexOf("new") === 0 ||
line.indexOf("index") === 0 ||
line.indexOf("---") === 0 ||
line.indexOf("+++") === 0) {
return;
}
if (line.indexOf("diff") === 0) {
/* File Diff Line */
/* add previous file(if exists) before start a new one */
if (currentFile &&
(currentFile.addedWords.length || currentFile.deletedWords.length)) {
files.push(currentFile);
currentFile = null;
}
/* create file structure */
currentFile = {};
currentFile.addedWords = [];
currentFile.deletedWords = [];
/* 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);
oldLine = values[2];
newLine = values[4];
} else {
/* Regular Diff Line */
var addedWords = [];
if (addedWords = line.match(/\{\+(.+?)\+\}/g)) {
addedWords = addedWords.map(function(word) {
return cleanWordMatch(word);
});
} else {
addedWords = [];
}
var deletedWords = [];
if (deletedWords = line.match(/\[-(.+?)-\]/g)) {
deletedWords = deletedWords.map(function(word) {
return cleanWordMatch(word);
});
} else {
deletedWords = [];
}
if (!addedWords.length && !deletedWords.length) {
oldLine++;
newLine++;
} else {
if (addedWords.length) {
currentFile.addedWords[newLine] = addedWords;
newLine++;
}
if (deletedWords.length) {
currentFile.deletedWords[oldLine] = deletedWords;
oldLine++;
}
}
}
});
/* add previous file(if exists) before start a new one */
if (currentFile &&
(currentFile.addedWords.length || currentFile.deletedWords.length)) {
files.push(currentFile);
currentFile = null;
}
return files;
};
var cleanWordMatch = function(str) {
return str.substr(2, str.length - 4);
};
/* singleton pattern */
var instance;
return {
getInstance: function() {
if (instance === undefined) {
instance = new WordDiffParser();
/* Hide the constructor so the returned objected can't be new'd */
instance.constructor = null;
}
return instance;
}
};
})();
window.WordDiffParser = ClassVariable;
return window.WordDiffParser;
})(jQuery, window);