compatibility with node code
This commit is contained in:
parent
a48fac6f1b
commit
0662960fae
11 changed files with 541 additions and 569 deletions
51
diff2html.js
51
diff2html.js
|
|
@ -1,51 +0,0 @@
|
||||||
/*
|
|
||||||
*
|
|
||||||
* Diff to HTML (diff2html.js)
|
|
||||||
* Author: rtfpessoa
|
|
||||||
*
|
|
||||||
* Diff commands:
|
|
||||||
* git diff
|
|
||||||
*/
|
|
||||||
|
|
||||||
var diffParser = new DiffParser();
|
|
||||||
var htmlPrinter = new HtmlPrinter();
|
|
||||||
|
|
||||||
function Diff2Html() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Generates pretty html from string diff input
|
|
||||||
*/
|
|
||||||
Diff2Html.prototype.getPrettyHtmlFromDiff = function (diffInput) {
|
|
||||||
var diffJson = diffParser.generateDiffJson(diffInput);
|
|
||||||
return htmlPrinter.generateLineByLineJsonHtml(diffJson);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Generates json object from string diff input
|
|
||||||
*/
|
|
||||||
Diff2Html.prototype.getJsonFromDiff = function (diffInput) {
|
|
||||||
return diffParser.generateDiffJson(diffInput);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Generates pretty html from a json object
|
|
||||||
*/
|
|
||||||
Diff2Html.prototype.getPrettyHtmlFromJson = function (diffJson) {
|
|
||||||
return htmlPrinter.generateLineByLineJsonHtml(diffJson);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Generates pretty side by side html from string diff input
|
|
||||||
*/
|
|
||||||
Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function (diffInput) {
|
|
||||||
var diffJson = diffParser.generateDiffJson(diffInput);
|
|
||||||
return htmlPrinter.generateSideBySideJsonHtml(diffJson);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Generates pretty side by side html from a json object
|
|
||||||
*/
|
|
||||||
Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function (diffJson) {
|
|
||||||
return htmlPrinter.generateSideBySideJsonHtml(diffJson);
|
|
||||||
};
|
|
||||||
|
|
@ -7,8 +7,6 @@
|
||||||
<!--
|
<!--
|
||||||
Diff to HTML (template.html)
|
Diff to HTML (template.html)
|
||||||
Author: rtfpessoa
|
Author: rtfpessoa
|
||||||
Date: Friday 29 August 2014
|
|
||||||
Last Update: Sunday 2 February 2015
|
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css">
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css">
|
||||||
|
|
@ -16,8 +14,8 @@
|
||||||
|
|
||||||
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
|
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
|
||||||
|
|
||||||
<!--<script type="text/javascript" src="lib/jsdiff.js"></script>-->
|
|
||||||
<script type="text/javascript" src="lib/diff.js"></script>
|
<script type="text/javascript" src="lib/diff.js"></script>
|
||||||
|
<script type="text/javascript" src="lib/fakeRequire.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="src/utils.js"></script>
|
<script type="text/javascript" src="src/utils.js"></script>
|
||||||
<script type="text/javascript" src="src/diff-parser.js"></script>
|
<script type="text/javascript" src="src/diff-parser.js"></script>
|
||||||
|
|
@ -25,7 +23,7 @@
|
||||||
<script type="text/javascript" src="src/side-by-side-printer.js"></script>
|
<script type="text/javascript" src="src/side-by-side-printer.js"></script>
|
||||||
<script type="text/javascript" src="src/line-by-line-printer.js"></script>
|
<script type="text/javascript" src="src/line-by-line-printer.js"></script>
|
||||||
<script type="text/javascript" src="src/html-printer.js"></script>
|
<script type="text/javascript" src="src/html-printer.js"></script>
|
||||||
<script type="text/javascript" src="diff2html.js"></script>
|
<script type="text/javascript" src="src/diff2html.js"></script>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/languages/scala.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/languages/scala.min.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
|
@ -188,7 +186,7 @@
|
||||||
'-});\n';
|
'-});\n';
|
||||||
|
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
var diff2Html = new Diff2Html();
|
var diff2Html = Diff2Html;
|
||||||
var diffJson = diff2Html.getJsonFromDiff(lineDiffExample);
|
var diffJson = diff2Html.getJsonFromDiff(lineDiffExample);
|
||||||
|
|
||||||
var languages = diffJson.map(function (line) {
|
var languages = diffJson.map(function (line) {
|
||||||
|
|
|
||||||
4
lib/fakeRequire.js
Normal file
4
lib/fakeRequire.js
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
var global = this;
|
||||||
|
function require() {
|
||||||
|
return global;
|
||||||
|
}
|
||||||
101
lib/jsdiff.js
101
lib/jsdiff.js
|
|
@ -1,101 +0,0 @@
|
||||||
/*
|
|
||||||
* Javascript Diff Algorithm
|
|
||||||
* By John Resig (http://ejohn.org/)
|
|
||||||
* Modified by Chu Alan "sprite"
|
|
||||||
* Modified by Rodrigo Fernandes "rtfpessoa"
|
|
||||||
*
|
|
||||||
* Released under the MIT license.
|
|
||||||
*
|
|
||||||
* More Info:
|
|
||||||
* http://ejohn.org/projects/javascript-diff-algorithm/
|
|
||||||
*/
|
|
||||||
|
|
||||||
function JsDiff(o, n) {
|
|
||||||
o = o.replace(/\s+$/, '');
|
|
||||||
n = n.replace(/\s+$/, '');
|
|
||||||
|
|
||||||
var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/));
|
|
||||||
var str = "";
|
|
||||||
|
|
||||||
var oSpace = o.match(/\s+/g);
|
|
||||||
if (oSpace == null) {
|
|
||||||
oSpace = ["\n"];
|
|
||||||
} else {
|
|
||||||
oSpace.push("\n");
|
|
||||||
}
|
|
||||||
var nSpace = n.match(/\s+/g);
|
|
||||||
if (nSpace == null) {
|
|
||||||
nSpace = ["\n"];
|
|
||||||
} else {
|
|
||||||
nSpace.push("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (out.n.length == 0) {
|
|
||||||
for (var i = 0; i < out.o.length; i++) {
|
|
||||||
str += '<del>' + out.o[i] + oSpace[i] + "</del>";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (out.n[0].text == null) {
|
|
||||||
for (n = 0; n < out.o.length && out.o[n].text == null; n++) {
|
|
||||||
str += '<del>' + out.o[n] + oSpace[n] + "</del>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < out.n.length; i++) {
|
|
||||||
if (out.n[i].text == null) {
|
|
||||||
str += '<ins>' + out.n[i] + nSpace[i] + "</ins>";
|
|
||||||
} else {
|
|
||||||
var pre = "";
|
|
||||||
|
|
||||||
for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++) {
|
|
||||||
pre += '<del>' + out.o[n] + oSpace[n] + "</del>";
|
|
||||||
}
|
|
||||||
str += " " + out.n[i].text + nSpace[i] + pre;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
function diff(o, n) {
|
|
||||||
var ns = new Object();
|
|
||||||
var os = new Object();
|
|
||||||
|
|
||||||
for (var i = 0; i < n.length; i++) {
|
|
||||||
if (ns[n[i]] == null)
|
|
||||||
ns[n[i]] = {rows: new Array(), o: null};
|
|
||||||
ns[n[i]].rows.push(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < o.length; i++) {
|
|
||||||
if (os[o[i]] == null)
|
|
||||||
os[o[i]] = {rows: new Array(), n: null};
|
|
||||||
os[o[i]].rows.push(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i in ns) {
|
|
||||||
if (ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1) {
|
|
||||||
n[ns[i].rows[0]] = {text: n[ns[i].rows[0]], row: os[i].rows[0]};
|
|
||||||
o[os[i].rows[0]] = {text: o[os[i].rows[0]], row: ns[i].rows[0]};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < n.length - 1; i++) {
|
|
||||||
if (n[i].text != null && n[i + 1].text == null && n[i].row + 1 < o.length && o[n[i].row + 1].text == null &&
|
|
||||||
n[i + 1] == o[n[i].row + 1]) {
|
|
||||||
n[i + 1] = {text: n[i + 1], row: n[i].row + 1};
|
|
||||||
o[n[i].row + 1] = {text: o[n[i].row + 1], row: i + 1};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = n.length - 1; i > 0; i--) {
|
|
||||||
if (n[i].text != null && n[i - 1].text == null && n[i].row > 0 && o[n[i].row - 1].text == null &&
|
|
||||||
n[i - 1] == o[n[i].row - 1]) {
|
|
||||||
n[i - 1] = {text: n[i - 1], row: n[i].row - 1};
|
|
||||||
o[n[i].row - 1] = {text: o[n[i].row - 1], row: i - 1};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {o: o, n: n};
|
|
||||||
}
|
|
||||||
|
|
@ -5,7 +5,9 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var utils = new Utils();
|
(function (global, undefined) {
|
||||||
|
|
||||||
|
var utils = require("./utils.js").Utils;
|
||||||
|
|
||||||
var LINE_TYPE = {
|
var LINE_TYPE = {
|
||||||
INSERTS: "d2h-ins",
|
INSERTS: "d2h-ins",
|
||||||
|
|
@ -145,3 +147,11 @@ DiffParser.prototype.generateDiffJson = function (diffInput) {
|
||||||
|
|
||||||
return files;
|
return files;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports.DiffParser = new DiffParser();
|
||||||
|
} else if (typeof global.DiffParser === 'undefined') {
|
||||||
|
global.DiffParser = new DiffParser();
|
||||||
|
}
|
||||||
|
|
||||||
|
})(this);
|
||||||
|
|
|
||||||
61
src/diff2html.js
Normal file
61
src/diff2html.js
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Diff to HTML (diff2html.js)
|
||||||
|
* Author: rtfpessoa
|
||||||
|
*
|
||||||
|
* Diff commands:
|
||||||
|
* git diff
|
||||||
|
*/
|
||||||
|
|
||||||
|
(function (global, undefined) {
|
||||||
|
|
||||||
|
var diffParser = require("./diff-parser.js").DiffParser;
|
||||||
|
var htmlPrinter = require("./html-printer.js").HtmlPrinter;
|
||||||
|
|
||||||
|
function Diff2Html() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generates pretty html from string diff input
|
||||||
|
*/
|
||||||
|
Diff2Html.prototype.getPrettyHtmlFromDiff = function (diffInput) {
|
||||||
|
var diffJson = diffParser.generateDiffJson(diffInput);
|
||||||
|
return htmlPrinter.generateLineByLineJsonHtml(diffJson);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generates json object from string diff input
|
||||||
|
*/
|
||||||
|
Diff2Html.prototype.getJsonFromDiff = function (diffInput) {
|
||||||
|
return diffParser.generateDiffJson(diffInput);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generates pretty html from a json object
|
||||||
|
*/
|
||||||
|
Diff2Html.prototype.getPrettyHtmlFromJson = function (diffJson) {
|
||||||
|
return htmlPrinter.generateLineByLineJsonHtml(diffJson);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generates pretty side by side html from string diff input
|
||||||
|
*/
|
||||||
|
Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function (diffInput) {
|
||||||
|
var diffJson = diffParser.generateDiffJson(diffInput);
|
||||||
|
return htmlPrinter.generateSideBySideJsonHtml(diffJson);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generates pretty side by side html from a json object
|
||||||
|
*/
|
||||||
|
Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function (diffJson) {
|
||||||
|
return htmlPrinter.generateSideBySideJsonHtml(diffJson);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports.Diff2Html = new Diff2Html();
|
||||||
|
} else if (typeof global.Diff2Html === 'undefined') {
|
||||||
|
global.Diff2Html = new Diff2Html();
|
||||||
|
}
|
||||||
|
|
||||||
|
})(this);
|
||||||
|
|
@ -5,8 +5,10 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var lineByLinePrinter = new LineByLinePrinter();
|
(function (global, undefined) {
|
||||||
var sideBySidePrinter = new SideBySidePrinter();
|
|
||||||
|
var lineByLinePrinter = require("./line-by-line-printer.js").LineByLinePrinter;
|
||||||
|
var sideBySidePrinter = require("./side-by-side-printer.js").SideBySidePrinter;
|
||||||
|
|
||||||
function HtmlPrinter() {
|
function HtmlPrinter() {
|
||||||
}
|
}
|
||||||
|
|
@ -14,3 +16,11 @@ function HtmlPrinter() {
|
||||||
HtmlPrinter.prototype.generateLineByLineJsonHtml = lineByLinePrinter.generateLineByLineJsonHtml;
|
HtmlPrinter.prototype.generateLineByLineJsonHtml = lineByLinePrinter.generateLineByLineJsonHtml;
|
||||||
|
|
||||||
HtmlPrinter.prototype.generateSideBySideJsonHtml = sideBySidePrinter.generateSideBySideJsonHtml;
|
HtmlPrinter.prototype.generateSideBySideJsonHtml = sideBySidePrinter.generateSideBySideJsonHtml;
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports.HtmlPrinter = new HtmlPrinter();
|
||||||
|
} else if (typeof global.HtmlPrinter === 'undefined') {
|
||||||
|
global.HtmlPrinter = new HtmlPrinter();
|
||||||
|
}
|
||||||
|
|
||||||
|
})(this);
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,11 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var diffParser = new DiffParser();
|
(function (global, undefined) {
|
||||||
var printerUtils = new PrinterUtils();
|
|
||||||
var utils = new Utils();
|
var diffParser = require("./diff-parser.js").DiffParser;
|
||||||
|
var printerUtils = require("./printer-utils.js").PrinterUtils;
|
||||||
|
var utils = require("./utils.js").Utils;
|
||||||
|
|
||||||
function LineByLinePrinter() {
|
function LineByLinePrinter() {
|
||||||
}
|
}
|
||||||
|
|
@ -118,3 +120,11 @@ function generateLineHtml(type, oldNumber, newNumber, content) {
|
||||||
" </td>\n" +
|
" </td>\n" +
|
||||||
"</tr>\n";
|
"</tr>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports.LineByLinePrinter = new LineByLinePrinter();
|
||||||
|
} else if (typeof global.LineByLinePrinter === 'undefined') {
|
||||||
|
global.LineByLinePrinter = new LineByLinePrinter();
|
||||||
|
}
|
||||||
|
|
||||||
|
})(this);
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,10 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var jsDiff = JsDiff;
|
(function (global, undefined) {
|
||||||
|
|
||||||
|
// dirty hack for browser compatibility
|
||||||
|
var jsDiff = (typeof JsDiff !== "undefined" && JsDiff) || require("../lib/diff.js");
|
||||||
|
|
||||||
function PrinterUtils() {
|
function PrinterUtils() {
|
||||||
}
|
}
|
||||||
|
|
@ -59,3 +62,11 @@ function removeIns(line) {
|
||||||
function removeDel(line) {
|
function removeDel(line) {
|
||||||
return line.replace(/(<del>((.|\n)*?)<\/del>)/g, "");
|
return line.replace(/(<del>((.|\n)*?)<\/del>)/g, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports.PrinterUtils = new PrinterUtils();
|
||||||
|
} else if (typeof global.PrinterUtils === 'undefined') {
|
||||||
|
global.PrinterUtils = new PrinterUtils();
|
||||||
|
}
|
||||||
|
|
||||||
|
})(this);
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,11 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var diffParser = new DiffParser();
|
(function (global, undefined) {
|
||||||
var printerUtils = new PrinterUtils();
|
|
||||||
var utils = new Utils();
|
var diffParser = require("./diff-parser.js").DiffParser;
|
||||||
|
var printerUtils = require("./printer-utils.js").PrinterUtils;
|
||||||
|
var utils = require("./utils.js").Utils;
|
||||||
|
|
||||||
function SideBySidePrinter() {
|
function SideBySidePrinter() {
|
||||||
}
|
}
|
||||||
|
|
@ -143,3 +145,11 @@ function generateSingleLineHtml(type, number, content) {
|
||||||
" </td>\n" +
|
" </td>\n" +
|
||||||
" </tr>\n";
|
" </tr>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports.SideBySidePrinter = new SideBySidePrinter();
|
||||||
|
} else if (typeof global.SideBySidePrinter === 'undefined') {
|
||||||
|
global.SideBySidePrinter = new SideBySidePrinter();
|
||||||
|
}
|
||||||
|
|
||||||
|
})(this);
|
||||||
|
|
|
||||||
10
src/utils.js
10
src/utils.js
|
|
@ -5,6 +5,8 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
(function (global, undefined) {
|
||||||
|
|
||||||
function Utils() {
|
function Utils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -23,3 +25,11 @@ Utils.prototype.startsWith = function (str, start) {
|
||||||
Utils.prototype.valueOrEmpty = function (value) {
|
Utils.prototype.valueOrEmpty = function (value) {
|
||||||
return value ? value : "";
|
return value ? value : "";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports.Utils = new Utils();
|
||||||
|
} else if (typeof global.Utils === 'undefined') {
|
||||||
|
global.Utils = new Utils();
|
||||||
|
}
|
||||||
|
|
||||||
|
})(this);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue