char by char diff & clean a bit
This commit is contained in:
parent
66970564dc
commit
cc9d271cc0
5 changed files with 426 additions and 651 deletions
16
.gitignore
vendored
Normal file
16
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Intellij
|
||||||
|
.idea/
|
||||||
|
*.iml
|
||||||
|
*.iws
|
||||||
|
|
||||||
|
# Mac
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# Maven
|
||||||
|
log/
|
||||||
|
target/
|
||||||
302
diff2html.js
302
diff2html.js
|
|
@ -3,17 +3,16 @@
|
||||||
* 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",
|
||||||
|
|
@ -24,87 +23,80 @@
|
||||||
|
|
||||||
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);
|
/*
|
||||||
|
* Generates pretty html from string diff input
|
||||||
return html;
|
*/
|
||||||
|
Diff2Html.prototype.getPrettyHtmlFromDiff = function (diffInput) {
|
||||||
|
var diffJson = generateDiffJson(diffInput);
|
||||||
|
return generateJsonHtml(diffJson);
|
||||||
};
|
};
|
||||||
|
|
||||||
var splitByFile = function(diffInput) {
|
/*
|
||||||
|
* 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 = [],
|
var files = [],
|
||||||
currentFile = null,
|
currentFile = null,
|
||||||
currentBlock = null,
|
currentBlock = null,
|
||||||
oldLine = null,
|
oldLine = null,
|
||||||
newLine = null;
|
newLine = null;
|
||||||
|
|
||||||
diffInput.split("\n").forEach(function(line) {
|
var saveBlock = function () {
|
||||||
// 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 block(if exists) before start a new file */
|
/* add previous block(if exists) before start a new file */
|
||||||
if (currentBlock) {
|
if (currentBlock) {
|
||||||
currentFile.blocks.push(currentBlock);
|
currentFile.blocks.push(currentBlock);
|
||||||
currentBlock = null;
|
currentBlock = null;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var saveFile = function () {
|
||||||
/* add previous file(if exists) before start a new one */
|
/* add previous file(if exists) before start a new one */
|
||||||
if (currentFile) {
|
if (currentFile) {
|
||||||
files.push(currentFile);
|
files.push(currentFile);
|
||||||
currentFile = null;
|
currentFile = null;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var startFile = function (line) {
|
||||||
|
saveBlock();
|
||||||
|
saveFile();
|
||||||
|
|
||||||
/* create file structure */
|
/* create file structure */
|
||||||
currentFile = {};
|
currentFile = {};
|
||||||
currentFile.blocks = [];
|
currentFile.blocks = [];
|
||||||
currentFile.deletedLines = 0,
|
currentFile.deletedLines = 0;
|
||||||
currentFile.addedLines = 0;
|
currentFile.addedLines = 0;
|
||||||
|
|
||||||
/* save file paths, before and after the diff */
|
/* save file paths, before and after the diff */
|
||||||
var values = /^diff --git a\/(\S+) b\/(\S+).*$/.exec(line);
|
var values = /^diff --git a\/(\S+) b\/(\S+).*$/.exec(line);
|
||||||
currentFile.oldName = values[1];
|
currentFile.oldName = values[1];
|
||||||
currentFile.newName = values[2];
|
currentFile.newName = values[2];
|
||||||
|
};
|
||||||
|
|
||||||
} else if (line.indexOf("@@") === 0) {
|
var startBlock = function (line) {
|
||||||
/* Diff Block Header Line */
|
saveBlock();
|
||||||
|
|
||||||
var values = /^(@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*/.exec(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 */
|
/* create block metadata */
|
||||||
currentBlock = {};
|
currentBlock = {};
|
||||||
currentBlock.lines = [];
|
currentBlock.lines = [];
|
||||||
currentBlock.oldStartLine = oldLine = values[2];
|
currentBlock.oldStartLine = oldLine = values[2];
|
||||||
currentBlock.newStartLine = newLine = values[4];
|
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 */
|
/* create block header line */
|
||||||
var currentLine = {};
|
var currentLine = {};
|
||||||
|
|
@ -115,130 +107,174 @@
|
||||||
|
|
||||||
/* add line to block */
|
/* add line to block */
|
||||||
currentBlock.lines.push(currentLine);
|
currentBlock.lines.push(currentLine);
|
||||||
|
};
|
||||||
|
|
||||||
} else {
|
var createLine = function (line) {
|
||||||
/* Regular Diff Line */
|
var isLineWithInserts = /{\+.*?\+}/.exec(line);
|
||||||
|
var isLineWithDeletes = /\[-.*?-\]/.exec(line);
|
||||||
|
var isNewLine = /^{\+.*?\+}$/.exec(line);
|
||||||
|
var isContextLine = !isLineWithInserts && !isLineWithDeletes;
|
||||||
|
|
||||||
var currentLine = {};
|
var currentLine = {};
|
||||||
currentLine.content = line;
|
|
||||||
|
|
||||||
if (line.indexOf("+") === 0) {
|
if (isContextLine) {
|
||||||
currentLine.type = CSS_STYLES.NEW;
|
currentLine = {};
|
||||||
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.type = CSS_STYLES.CONTEXT;
|
||||||
currentLine.oldNumber = oldLine++;
|
currentLine.oldNumber = oldLine++;
|
||||||
currentLine.newNumber = newLine++;
|
currentLine.newNumber = newLine++;
|
||||||
|
currentLine.content = line;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* add line to block */
|
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);
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/* add previous block(if exists) before start a new file */
|
saveBlock();
|
||||||
if (currentBlock) {
|
saveFile();
|
||||||
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;
|
return files;
|
||||||
};
|
};
|
||||||
|
|
||||||
var generateHtml = function(diffFiles, changedWords) {
|
var generateJsonHtml = function (diffFiles) {
|
||||||
return diffFiles.map(function(file, index) {
|
return "<div id=\"wrapper\">\n" +
|
||||||
var fileHeader = file.oldName === file.newName ? file.newName : file.oldName + " -> " + file.newName
|
diffFiles.map(function (file) {
|
||||||
|
return "<div class=\"file-wrapper\">\n" +
|
||||||
return "<div class=\"file-wrapper\">" +
|
" <div class=\"file-header\">\n" +
|
||||||
" <div class=\"file-header\">" +
|
" <div class=\"file-stats\">\n" +
|
||||||
" <div class=\"file-stats\">" +
|
" <span class=\"lines-added\">+" + file.addedLines + "</span>\n" +
|
||||||
" <span class=\"lines-added\">+" + file.addedLines + "</span>" +
|
" <span class=\"lines-deleted\">-" + file.deletedLines + "</span>\n" +
|
||||||
" <span class=\"lines-deleted\">-" + file.deletedLines + "</span>" +
|
" </div>\n" +
|
||||||
" </div>" +
|
" <div class=\"file-name\">" + getDiffName(file.oldName, file.newName) + "</div>\n" +
|
||||||
" <div class=\"file-name\">" + fileHeader + "</div>" +
|
" </div>\n" +
|
||||||
" </div>" +
|
" <div class=\"file-diff\">\n" +
|
||||||
" <div class=\"file-diff\">" +
|
" <div class=\"code-wrapper\">\n" +
|
||||||
" <div class=\"code-wrapper\">" +
|
" <table class=\"diff-table\">\n" +
|
||||||
" <table class=\"diff-table\">" +
|
" <tbody>\n" +
|
||||||
" <tbody>" +
|
" " + generateFileHtml(file) +
|
||||||
generateFileHtml(file, changedWords[index]) +
|
" </tbody>\n" +
|
||||||
" </tbody>" +
|
" </table>\n" +
|
||||||
" </table>" +
|
" </div>\n" +
|
||||||
" </div>" +
|
" </div>\n" +
|
||||||
" </div>" +
|
" </div>\n";
|
||||||
"</div>";
|
}).join("\n") +
|
||||||
});
|
"</div>\n";
|
||||||
};
|
};
|
||||||
|
|
||||||
var generateFileHtml = function(file, changedWords) {
|
var getDiffName = function (oldFilename, newFilename) {
|
||||||
return file.blocks.map(function(block) {
|
return oldFilename === newFilename ? newFilename : oldFilename + " -> " + newFilename;
|
||||||
return block.lines.map(function(line) {
|
};
|
||||||
|
|
||||||
var oldLine = line.oldNumber ? line.oldNumber : "";
|
var generateFileHtml = function (file) {
|
||||||
var newLine = line.newNumber ? line.newNumber : "";
|
return file.blocks.map(function (block) {
|
||||||
|
return block.lines.map(function (line) {
|
||||||
|
|
||||||
var oldWords = [];
|
var oldLine = valueOrEmpty(line.oldNumber);
|
||||||
var newWords = [];
|
var newLine = valueOrEmpty(line.newNumber);
|
||||||
|
|
||||||
if (oldLine && oldLine !== BLOCK_HEADER_LINE &&
|
var escapedLine = escape(line.content);
|
||||||
changedWords && changedWords.deletedWords && changedWords.deletedWords[oldLine]) {
|
|
||||||
oldWords = changedWords.deletedWords[oldLine];
|
if (line.type === CSS_STYLES.NEW) {
|
||||||
|
escapedLine = generateLineInsertions(escapedLine);
|
||||||
|
} else if (line.type === CSS_STYLES.DELETED) {
|
||||||
|
escapedLine = generateLineDeletions(escapedLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newLine && newLine !== BLOCK_HEADER_LINE &&
|
return "<tr>\n" +
|
||||||
changedWords && changedWords.addedWords && changedWords.addedWords[newLine]) {
|
" <td class=\"code-linenumber " + line.type + "\">" + oldLine + "</td>\n" +
|
||||||
newWords = changedWords.addedWords[newLine];
|
" <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";
|
||||||
//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");
|
||||||
}).join("\n");
|
}).join("\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
var markWords = function(words, line, clazz) {
|
var generateLineInsertions = function (line) {
|
||||||
var newLine = line;
|
return line.replace(/(\[-.*?-\])/g, "").
|
||||||
words.forEach(function(word) {
|
replace(/({\+(.*?)\+})/g, "<ins>$2</ins>");
|
||||||
newLine = newLine.replace(word, "<" + clazz + ">" + word + "</" + clazz + ">");
|
|
||||||
});
|
|
||||||
|
|
||||||
return newLine;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var escape = function(str) {
|
var generateLineDeletions = function (line) {
|
||||||
|
return line.replace(/({\+.*?\+})/g, "").
|
||||||
|
replace(/(\[-(.*?)-\])/g, "<del>$2</del>");
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Utils
|
||||||
|
*/
|
||||||
|
|
||||||
|
function escape(str) {
|
||||||
return str
|
return str
|
||||||
.replace(/&/g, "&")
|
.replace(/&/g, "&")
|
||||||
.replace(/</g, "<")
|
.replace(/</g, "<")
|
||||||
.replace(/>/g, ">")
|
.replace(/>/g, ">")
|
||||||
.replace(/\t/g, " ");
|
.replace(/\t/g, " ");
|
||||||
};
|
}
|
||||||
|
|
||||||
|
function startsWith(str, start) {
|
||||||
|
return str.indexOf(start) === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function valueOrEmpty(value) {
|
||||||
|
return value ? value : "";
|
||||||
|
}
|
||||||
|
|
||||||
/* singleton pattern */
|
/* singleton pattern */
|
||||||
var instance;
|
var instance;
|
||||||
return {
|
return {
|
||||||
getInstance: function() {
|
getInstance: function () {
|
||||||
if (instance === undefined) {
|
if (instance === undefined) {
|
||||||
instance = new Diff2Html();
|
instance = new Diff2Html();
|
||||||
/* Hide the constructor so the returned objected can't be new'd */
|
/* Hide the constructor so the returned objected can't be new'd */
|
||||||
|
|
@ -250,7 +286,7 @@
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
window.Diff2Html = ClassVariable;
|
window.Diff2Html = ClassVariable.getInstance();
|
||||||
return window.Diff2Html;
|
return window.Diff2Html;
|
||||||
|
|
||||||
})(jQuery, window);
|
})(window);
|
||||||
|
|
|
||||||
29
style.css
29
style.css
|
|
@ -3,6 +3,7 @@
|
||||||
* 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
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -36,22 +37,20 @@ body {
|
||||||
.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;
|
||||||
|
|
@ -59,8 +58,7 @@ body {
|
||||||
|
|
||||||
.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;
|
||||||
|
|
@ -71,7 +69,7 @@ body {
|
||||||
}
|
}
|
||||||
|
|
||||||
.file-diff > div {
|
.file-diff > div {
|
||||||
width: 100%:
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
pre {
|
pre {
|
||||||
|
|
@ -118,26 +116,25 @@ 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 {
|
||||||
|
|
@ -167,7 +164,7 @@ td, th {
|
||||||
.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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
146
template.html
146
template.html
|
|
@ -8,12 +8,12 @@
|
||||||
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' +
|
||||||
|
|
@ -21,142 +21,6 @@
|
||||||
'--- 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' +
|
|
||||||
' \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 exWordInput = '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")-]{+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' +
|
'package metrics\n' +
|
||||||
' \n' +
|
' \n' +
|
||||||
'import java.io\n' +
|
'import java.io\n' +
|
||||||
|
|
@ -185,15 +49,13 @@
|
||||||
' \n' +
|
' \n' +
|
||||||
' fileFolders.list().flatMap {';
|
' fileFolders.list().flatMap {';
|
||||||
|
|
||||||
$( document ).ready(function() {
|
$(document).ready(function () {
|
||||||
$("#wrapper").html(Diff2Html.getInstance().generatePrettyDiff(exDiffInput, exEvenBetterDiffInput));
|
var content = Diff2Html.getPrettyHtmlFromDiff(exInput);
|
||||||
//console.log(WordDiffParser.getInstance().generateChangedWords(exWordInput));
|
$("body").html(content);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="wrapper">
|
|
||||||
</div>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -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);
|
|
||||||
Loading…
Reference in a new issue