diff --git a/diff2html.css b/diff2html.css
index 0f04f16..586f356 100644
--- a/diff2html.css
+++ b/diff2html.css
@@ -2,8 +2,6 @@
*
* Diff to HTML (diff2html.css)
* Author: rtfpessoa
- * Date: Friday 29 August 2014
- * Last Update: Friday 30 January 2015
*
*/
diff --git a/diff2html.js b/diff2html.js
index f1db26c..435a6cf 100644
--- a/diff2html.js
+++ b/diff2html.js
@@ -2,448 +2,421 @@
*
* Diff to HTML (diff2html.js)
* Author: rtfpessoa
- * Date: Friday 29 August 2014
- * Last Update: Sunday 2 February 2015
*
- * Diff command:
+ * Diff commands:
* git diff
*/
-(function (window) {
- var ClassVariable;
+var LINE_TYPE = {
+ INSERTS: "d2h-ins",
+ DELETES: "d2h-del",
+ CONTEXT: "d2h-cntx",
+ INFO: "d2h-info"
+};
- ClassVariable = (function () {
+function Diff2Html() {
+}
- var LINE_TYPE = {
- INSERTS: "d2h-ins",
- DELETES: "d2h-del",
- CONTEXT: "d2h-cntx",
- INFO: "d2h-info"
- };
+/*
+ * Generates pretty html from string diff input
+ */
+Diff2Html.prototype.getPrettyHtmlFromDiff = function (diffInput) {
+ var diffJson = generateDiffJson(diffInput);
+ return generateJsonHtml(diffJson);
+};
- function Diff2Html() {
+/*
+ * 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);
+};
+
+/*
+ * Generates pretty side by side html from string diff input
+ */
+Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function (diffInput) {
+ var diffJson = generateDiffJson(diffInput);
+ return generateSideBySideJsonHtml(diffJson);
+};
+
+/*
+ * Generates pretty side by side html from a json object
+ */
+Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function (diffJson) {
+ return generateSideBySideJsonHtml(diffJson);
+};
+
+var generateDiffJson = function (diffInput) {
+ var files = [],
+ currentFile = null,
+ currentBlock = null,
+ oldLine = null,
+ newLine = null;
+
+ var saveBlock = function () {
+ /* add previous block(if exists) before start a new file */
+ if (currentBlock) {
+ currentFile.blocks.push(currentBlock);
+ currentBlock = null;
+ }
+ };
+
+ var saveFile = function () {
+ /*
+ * add previous file(if exists) before start a new one
+ * if it has name (to avoid binary files errors)
+ */
+ if (currentFile && currentFile.newName) {
+ files.push(currentFile);
+ currentFile = null;
+ }
+ };
+
+ var startFile = function () {
+ saveBlock();
+ saveFile();
+
+ /* create file structure */
+ currentFile = {};
+ currentFile.blocks = [];
+ currentFile.deletedLines = 0;
+ currentFile.addedLines = 0;
+ };
+
+ var startBlock = function (line) {
+ saveBlock();
+
+ var values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line) ||
+ /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line) ||
+ [0, 0, 0];
+
+ oldLine = values[1];
+ newLine = values[2];
+
+ /* create block metadata */
+ currentBlock = {};
+ currentBlock.lines = [];
+ currentBlock.oldStartLine = oldLine;
+ currentBlock.newStartLine = newLine;
+ currentBlock.header = line;
+ };
+
+ var createLine = function (line) {
+ var currentLine = {};
+ currentLine.content = line;
+
+ /* fill the line data */
+ if (startsWith(line, "+") || startsWith(line, " +")) {
+ currentFile.addedLines++;
+
+ currentLine.type = LINE_TYPE.INSERTS;
+ currentLine.oldNumber = null;
+ currentLine.newNumber = newLine++;
+
+ currentBlock.lines.push(currentLine);
+
+ } else if (startsWith(line, "-") || startsWith(line, " -")) {
+ currentFile.deletedLines++;
+
+ currentLine.type = LINE_TYPE.DELETES;
+ currentLine.oldNumber = oldLine++;
+ currentLine.newNumber = null;
+
+ currentBlock.lines.push(currentLine);
+
+ } else {
+ currentLine.type = LINE_TYPE.CONTEXT;
+ currentLine.oldNumber = oldLine++;
+ currentLine.newNumber = newLine++;
+
+ currentBlock.lines.push(currentLine);
+ }
+ };
+
+ var diffLines = diffInput.split("\n");
+ diffLines.forEach(function (line) {
+ // Unmerged paths, and possibly other non-diffable files
+ // https://github.com/scottgonzalez/pretty-diff/issues/11
+ // Also, remove some useless lines
+ if (!line || startsWith(line, "*") ||
+ startsWith(line, "new") || startsWith(line, "index")) {
+ return;
}
- /*
- * Generates pretty html from string diff input
- */
- Diff2Html.prototype.getPrettyHtmlFromDiff = function (diffInput) {
- var diffJson = generateDiffJson(diffInput);
- return generateJsonHtml(diffJson);
- };
+ var values = [];
+ if (startsWith(line, "diff")) {
+ startFile();
+ } else if (currentFile && !currentFile.oldName && (values = /^--- a\/(\S+).*$/.exec(line))) {
+ currentFile.oldName = values[1];
+ } else if (currentFile && !currentFile.newName && (values = /^\+\+\+ [b]?\/(\S+).*$/.exec(line))) {
+ currentFile.newName = values[1];
- /*
- * Generates json object from string diff input
- */
- Diff2Html.prototype.getJsonFromDiff = function (diffInput) {
- return generateDiffJson(diffInput);
- };
+ var fileSplit = currentFile.newName.split(".");
+ currentFile.language = fileSplit[fileSplit.length - 1];
+ } else if (currentFile && startsWith(line, "@@")) {
+ startBlock(line);
+ } else if (currentBlock) {
+ createLine(line);
+ }
+ });
- /*
- * Generates pretty html from a json object
- */
- Diff2Html.prototype.getPrettyHtmlFromJson = function (diffJson) {
- return generateJsonHtml(diffJson);
- };
+ saveBlock();
+ saveFile();
- /*
- * Generates pretty side by side html from string diff input
- */
- Diff2Html.prototype.getPrettySideBySideHtmlFromDiff = function (diffInput) {
- var diffJson = generateDiffJson(diffInput);
- return generateSideBySideJsonHtml(diffJson);
- };
+ return files;
+};
- /*
- * Generates pretty side by side html from a json object
- */
- Diff2Html.prototype.getPrettySideBySideHtmlFromJson = function (diffJson) {
- return generateSideBySideJsonHtml(diffJson);
- };
+/*
+ * Line By Line HTML
+ */
- var generateDiffJson = function (diffInput) {
- var files = [],
- currentFile = null,
- currentBlock = null,
- oldLine = null,
- newLine = null;
+var generateJsonHtml = function (diffFiles) {
+ return "
\n" +
+ diffFiles.map(function (file) {
+ return "
\n" +
+ " \n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ " \n" +
+ " " + generateFileHtml(file) +
+ " \n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ "
\n";
+ }).join("\n") +
+ "
\n";
+};
- var saveBlock = function () {
- /* add previous block(if exists) before start a new file */
- if (currentBlock) {
- currentFile.blocks.push(currentBlock);
- currentBlock = null;
- }
- };
+var generateFileHtml = function (file) {
+ return file.blocks.map(function (block) {
- var saveFile = function () {
- /*
- * add previous file(if exists) before start a new one
- * if it has name (to avoid binary files errors)
- */
- if (currentFile && currentFile.newName) {
- files.push(currentFile);
- currentFile = null;
- }
- };
+ var lines = "\n" +
+ " | \n" +
+ " " +
+ " " + escape(block.header) + " " +
+ " | \n" +
+ "
\n";
- var startFile = function () {
- saveBlock();
- saveFile();
+ for (var i = 0; i < block.lines.length; i++) {
+ var prevLine = block.lines[i - 1];
+ var line = block.lines[i];
+ var newLine = block.lines[i + 1];
+ var nextNewLine = block.lines[i + 2];
- /* create file structure */
- currentFile = {};
- currentFile.blocks = [];
- currentFile.deletedLines = 0;
- currentFile.addedLines = 0;
- };
+ var isOppositeTypeTwoLineBlock =
+ line.type == LINE_TYPE.DELETES &&
+ newLine && newLine.type == LINE_TYPE.INSERTS &&
+ (!nextNewLine || nextNewLine && nextNewLine.type != LINE_TYPE.INSERTS) &&
+ (!prevLine || prevLine && prevLine.type != LINE_TYPE.DELETES);
- var startBlock = function (line) {
- saveBlock();
+ var escapedLine = escape(line.content);
- var values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line) ||
- /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line) ||
- [0, 0, 0];
+ if (isOppositeTypeTwoLineBlock) {
+ var nextEscapedLine = escape(newLine.content);
- oldLine = values[1];
- newLine = values[2];
+ var diff = diffHighlight(escapedLine, nextEscapedLine);
- /* create block metadata */
- currentBlock = {};
- currentBlock.lines = [];
- currentBlock.oldStartLine = oldLine;
- currentBlock.newStartLine = newLine;
- currentBlock.header = line;
- };
+ lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, diff.o) +
+ generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, diff.n);
- var createLine = function (line) {
- var currentLine = {};
- currentLine.content = line;
-
- /* fill the line data */
- if (startsWith(line, "+") || startsWith(line, " +")) {
- currentFile.addedLines++;
-
- currentLine.type = LINE_TYPE.INSERTS;
- currentLine.oldNumber = null;
- currentLine.newNumber = newLine++;
-
- currentBlock.lines.push(currentLine);
-
- } else if (startsWith(line, "-") || startsWith(line, " -")) {
- currentFile.deletedLines++;
-
- currentLine.type = LINE_TYPE.DELETES;
- currentLine.oldNumber = oldLine++;
- currentLine.newNumber = null;
-
- currentBlock.lines.push(currentLine);
-
- } else {
- currentLine.type = LINE_TYPE.CONTEXT;
- currentLine.oldNumber = oldLine++;
- currentLine.newNumber = newLine++;
-
- currentBlock.lines.push(currentLine);
- }
- };
-
- var diffLines = diffInput.split("\n");
- diffLines.forEach(function (line) {
- // Unmerged paths, and possibly other non-diffable files
- // https://github.com/scottgonzalez/pretty-diff/issues/11
- // Also, remove some useless lines
- if (!line || startsWith(line, "*") ||
- startsWith(line, "new") || startsWith(line, "index")) {
- return;
- }
-
- var values = [];
- if (startsWith(line, "diff")) {
- startFile();
- } else if (currentFile && !currentFile.oldName && (values = /^--- a\/(\S+).*$/.exec(line))) {
- currentFile.oldName = values[1];
- } else if (currentFile && !currentFile.newName && (values = /^\+\+\+ [b]?\/(\S+).*$/.exec(line))) {
- currentFile.newName = values[1];
-
- var fileSplit = currentFile.newName.split(".");
- currentFile.language = fileSplit[fileSplit.length - 1];
- } else if (currentFile && startsWith(line, "@@")) {
- startBlock(line);
- } else if (currentBlock) {
- createLine(line);
- }
- });
-
- saveBlock();
- saveFile();
-
- return files;
- };
-
- /*
- * Line By Line HTML
- */
-
- var generateJsonHtml = function (diffFiles) {
- return "\n" +
- diffFiles.map(function (file) {
- return "
\n" +
- " \n" +
- "
\n" +
- "
\n" +
- "
\n" +
- " \n" +
- " " + generateFileHtml(file) +
- " \n" +
- "
\n" +
- "
\n" +
- "
\n" +
- "
\n";
- }).join("\n") +
- "
\n";
- };
-
- var generateFileHtml = function (file) {
- return file.blocks.map(function (block) {
-
- var lines = "\n" +
- " | \n" +
- " " +
- " " + escape(block.header) + " " +
- " | \n" +
- "
\n";
-
- for (var i = 0; i < block.lines.length; i++) {
- var prevLine = block.lines[i - 1];
- var line = block.lines[i];
- var newLine = block.lines[i + 1];
- var nextNewLine = block.lines[i + 2];
-
- var isOppositeTypeTwoLineBlock =
- line.type == LINE_TYPE.DELETES &&
- newLine && newLine.type == LINE_TYPE.INSERTS &&
- (!nextNewLine || nextNewLine && nextNewLine.type != LINE_TYPE.INSERTS) &&
- (!prevLine || prevLine && prevLine.type != LINE_TYPE.DELETES);
-
- var escapedLine = escape(line.content);
-
- if (isOppositeTypeTwoLineBlock) {
- var nextEscapedLine = escape(newLine.content);
-
- var diff = diffHighlight(escapedLine, nextEscapedLine);
-
- lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, diff.o) +
- generateLineHtml(newLine.type, newLine.oldNumber, newLine.newNumber, diff.n);
-
- i++;
- } else {
- lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
- }
- }
-
- return lines;
- }).join("\n");
- };
-
- var generateLineHtml = function (type, oldNumber, newNumber, content) {
- return "\n" +
- " | " +
- " " + valueOrEmpty(oldNumber) + " " +
- " " + valueOrEmpty(newNumber) + " " +
- " | \n" +
- " " +
- " " + content + " " +
- " | \n" +
- "
\n";
- };
-
- /*
- * Side By Side HTML (work in progress)
- */
-
- var generateSideBySideJsonHtml = function (diffFiles) {
- return "\n" +
- diffFiles.map(function (file) {
- var diffs = generateSideBySideFileHtml(file);
-
- return "
\n" +
- " \n" +
- "
\n" +
- "
\n" +
- "
\n" +
- "
\n" +
- " \n" +
- " " + diffs.left +
- " \n" +
- "
\n" +
- "
\n" +
- "
\n" +
- "
\n" +
- "
\n" +
- "
\n" +
- " \n" +
- " " + diffs.right +
- " \n" +
- "
\n" +
- "
\n" +
- "
\n" +
- "
\n" +
- "
\n";
- }).join("\n") +
- "
\n";
- };
-
- var generateSideBySideFileHtml = function (file) {
- var fileHtml = {};
- fileHtml.left = "";
- fileHtml.right = "";
-
- file.blocks.forEach(function (block) {
-
- fileHtml.left += "\n" +
- " | \n" +
- " " +
- " " + escape(block.header) + " " +
- " | \n" +
- "
\n";
-
- fileHtml.right += "\n" +
- " | \n" +
- " " +
- " " +
- " | \n" +
- "
\n";
-
- for (var i = 0; i < block.lines.length; i++) {
- var prevLine = block.lines[i - 1];
- var line = block.lines[i];
- var newLine = block.lines[i + 1];
- var nextNewLine = block.lines[i + 2];
-
- var isOpositeTypeTwoLineBlock = line.type == LINE_TYPE.DELETES && newLine && newLine.type == LINE_TYPE.INSERTS &&
- (!nextNewLine || nextNewLine && nextNewLine.type != LINE_TYPE.INSERTS) &&
- (!prevLine || prevLine && prevLine.type != LINE_TYPE.DELETES);
-
- var escapedLine = escape(line.content);
-
- if (isOpositeTypeTwoLineBlock) {
- var nextEscapedLine = escape(newLine.content);
-
- var diff = diffHighlight(escapedLine, nextEscapedLine);
-
- fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, diff.o);
- fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, diff.n);
-
- i++;
- } else if (line.type == LINE_TYPE.DELETES) {
- fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine);
- fileHtml.right += generateSingleLineHtml(LINE_TYPE.CONTEXT, "", "", "");
- } else if (line.type == LINE_TYPE.INSERTS) {
- fileHtml.left += generateSingleLineHtml(LINE_TYPE.CONTEXT, "", "", "");
- fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine);
- } else {
- fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine);
- fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine);
- }
- }
-
- });
-
- return fileHtml;
- };
-
- var generateSingleLineHtml = function (type, number, content) {
- return "\n" +
- " | " + number + " | \n" +
- " " +
- " " + content + " " +
- " | \n" +
- "
\n";
- };
-
- /*
- * HTML Helpers
- */
-
- var getDiffName = function (oldFilename, newFilename) {
- if (oldFilename && newFilename && oldFilename !== newFilename) {
- return oldFilename + " -> " + newFilename;
- } else if (newFilename) {
- return newFilename;
- } else if (oldFilename) {
- return oldFilename;
+ i++;
} else {
- return "Unknown filename";
- }
- };
-
- var removeIns = function (line) {
- return line.replace(/(((.|\n)*?)<\/ins>)/g, "");
- };
-
- var removeDel = function (line) {
- return line.replace(/(((.|\n)*?)<\/del>)/g, "");
- };
-
- /*
- * Utils
- */
-
- function escape(str) {
- return str.slice(0)
- .replace(/&/g, "&")
- .replace(//g, ">")
- .replace(/\t/g, " ");
- }
-
- function startsWith(str, start) {
- return str.indexOf(start) === 0;
- }
-
- function valueOrEmpty(value) {
- return value ? value : "";
- }
-
- function diffHighlight(diffLine1, diffLine2) {
- /* remove the initial -/+ to avoid always having diff in the first char */
- var highlightedLine = diffString(diffLine1.substr(1), diffLine2.substr(1));
-
- return {
- o: diffLine1.charAt(0) + removeIns(highlightedLine),
- n: diffLine2.charAt(0) + removeDel(highlightedLine)
+ lines += generateLineHtml(line.type, line.oldNumber, line.newNumber, escapedLine);
}
}
- /* 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;
+ return lines;
+ }).join("\n");
+};
+
+var generateLineHtml = function (type, oldNumber, newNumber, content) {
+ return "\n" +
+ " | " +
+ " " + valueOrEmpty(oldNumber) + " " +
+ " " + valueOrEmpty(newNumber) + " " +
+ " | \n" +
+ " " +
+ " " + content + " " +
+ " | \n" +
+ "
\n";
+};
+
+/*
+ * Side By Side HTML (work in progress)
+ */
+
+var generateSideBySideJsonHtml = function (diffFiles) {
+ return "\n" +
+ diffFiles.map(function (file) {
+ var diffs = generateSideBySideFileHtml(file);
+
+ return "
\n" +
+ " \n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ " \n" +
+ " " + diffs.left +
+ " \n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ " \n" +
+ " " + diffs.right +
+ " \n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ "
\n" +
+ "
\n";
+ }).join("\n") +
+ "
\n";
+};
+
+var generateSideBySideFileHtml = function (file) {
+ var fileHtml = {};
+ fileHtml.left = "";
+ fileHtml.right = "";
+
+ file.blocks.forEach(function (block) {
+
+ fileHtml.left += "\n" +
+ " | \n" +
+ " " +
+ " " + escape(block.header) + " " +
+ " | \n" +
+ "
\n";
+
+ fileHtml.right += "\n" +
+ " | \n" +
+ " " +
+ " " +
+ " | \n" +
+ "
\n";
+
+ for (var i = 0; i < block.lines.length; i++) {
+ var prevLine = block.lines[i - 1];
+ var line = block.lines[i];
+ var newLine = block.lines[i + 1];
+ var nextNewLine = block.lines[i + 2];
+
+ var isOpositeTypeTwoLineBlock = line.type == LINE_TYPE.DELETES && newLine && newLine.type == LINE_TYPE.INSERTS &&
+ (!nextNewLine || nextNewLine && nextNewLine.type != LINE_TYPE.INSERTS) &&
+ (!prevLine || prevLine && prevLine.type != LINE_TYPE.DELETES);
+
+ var escapedLine = escape(line.content);
+
+ if (isOpositeTypeTwoLineBlock) {
+ var nextEscapedLine = escape(newLine.content);
+
+ var diff = diffHighlight(escapedLine, nextEscapedLine);
+
+ fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, diff.o);
+ fileHtml.right += generateSingleLineHtml(newLine.type, newLine.newNumber, diff.n);
+
+ i++;
+ } else if (line.type == LINE_TYPE.DELETES) {
+ fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine);
+ fileHtml.right += generateSingleLineHtml(LINE_TYPE.CONTEXT, "", "", "");
+ } else if (line.type == LINE_TYPE.INSERTS) {
+ fileHtml.left += generateSingleLineHtml(LINE_TYPE.CONTEXT, "", "", "");
+ fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine);
+ } else {
+ fileHtml.left += generateSingleLineHtml(line.type, line.oldNumber, escapedLine);
+ fileHtml.right += generateSingleLineHtml(line.type, line.newNumber, escapedLine);
}
- };
+ }
- })();
+ });
- window.Diff2Html = ClassVariable.getInstance();
- return window.Diff2Html;
+ return fileHtml;
+};
-})(window);
+var generateSingleLineHtml = function (type, number, content) {
+ return "\n" +
+ " | " + number + " | \n" +
+ " " +
+ " " + content + " " +
+ " | \n" +
+ "
\n";
+};
+
+/*
+ * HTML Helpers
+ */
+
+var getDiffName = function (oldFilename, newFilename) {
+ if (oldFilename && newFilename && oldFilename !== newFilename) {
+ return oldFilename + " -> " + newFilename;
+ } else if (newFilename) {
+ return newFilename;
+ } else if (oldFilename) {
+ return oldFilename;
+ } else {
+ return "Unknown filename";
+ }
+};
+
+var removeIns = function (line) {
+ return line.replace(/(((.|\n)*?)<\/ins>)/g, "");
+};
+
+var removeDel = function (line) {
+ return line.replace(/(((.|\n)*?)<\/del>)/g, "");
+};
+
+/*
+ * Utils
+ */
+
+function escape(str) {
+ return str.slice(0)
+ .replace(/&/g, "&")
+ .replace(//g, ">")
+ .replace(/\t/g, " ");
+}
+
+function startsWith(str, start) {
+ return str.indexOf(start) === 0;
+}
+
+function valueOrEmpty(value) {
+ return value ? value : "";
+}
+
+function diffHighlight(diffLine1, diffLine2) {
+ /* remove the initial -/+ to avoid always having diff in the first char */
+ var highlightedLine = JsDiff(diffLine1.substr(1), diffLine2.substr(1));
+
+ return {
+ o: diffLine1.charAt(0) + removeIns(highlightedLine),
+ n: diffLine2.charAt(0) + removeDel(highlightedLine)
+ }
+}
diff --git a/diff2html.min.css b/diff2html.min.css
index e869a31..7f7ce1b 100644
--- a/diff2html.min.css
+++ b/diff2html.min.css
@@ -1 +1 @@
-.d2h-wrapper{display:block;margin:0 auto;text-align:left;width:100%}.d2h-file-wrapper{border:1px solid #ddd;border-radius:3px;margin-bottom:1em}.d2h-file-header{padding:5px 10px;border-bottom:1px solid #d8d8d8;background-color:#f7f7f7;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-stats{display:inline;font-size:12px;text-align:center;max-width:15%}.d2h-lines-added{background-color:#ceffce;border:1px solid #b4e2b4;color:#399839;border-radius:5px 0 0 5px;padding:2px;width:25px}.d2h-lines-deleted{background-color:#f7c8c8;border:1px solid #e9aeae;color:#c33;border-radius:0 5px 5px 0;padding:2px;width:25px}.d2h-file-name{display:inline;height:33px;line-height:33px;max-width:80%;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}.d2h-diff-table{border-collapse:collapse;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;height:18px;line-height:18px;width:100%}.d2h-files-diff{width:100%}.d2h-file-diff{overflow-x:scroll}.d2h-file-side-diff{display:inline-block;overflow-x:scroll;width:50%;margin-right:-4px}.d2h-code-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:80px;color:inherit;overflow-x:inherit;background:0 0}.d2h-code-side-line.hljs{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:50px;color:inherit;overflow-x:inherit;background:0 0}.d2h-code-line del,.d2h-code-side-line del{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#ffb6ba;border-radius:.2em}.d2h-code-line ins,.d2h-code-side-line ins{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#97f295;border-radius:.2em}.line-num1{display:inline-block;float:left;width:30px;overflow:hidden;text-overflow:ellipsis}.line-num2{display:inline-block;float:right;width:30px;overflow:hidden;text-overflow:ellipsis}.d2h-code-linenumber{position:absolute;width:2%;min-width:65px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,.3);text-align:right;border:solid #eee;border-width:0 1px;cursor:pointer}.d2h-code-side-linenumber{position:absolute;width:35px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,.3);text-align:right;border:solid #eee;border-width:0 1px;cursor:pointer;overflow:hidden;text-overflow:ellipsis}.d2h-del{background-color:#fee8e9;border-color:#e9aeae}.d2h-ins{background-color:#dfd;border-color:#b4e2b4}.d2h-info{background-color:#f8fafd;color:rgba(0,0,0,.3);border-color:#d5e4f2}
+.d2h-wrapper{display:block;margin:0 auto;text-align:left;width:100%}.d2h-file-wrapper{border:1px solid #ddd;border-radius:3px;margin-bottom:1em}.d2h-file-header{padding:5px 10px;border-bottom:1px solid #d8d8d8;background-color:#f7f7f7;font:13px Helvetica,arial,freesans,clean,sans-serif,"Segoe UI Emoji","Segoe UI Symbol"}.d2h-file-stats{display:inline;font-size:12px;text-align:center;max-width:15%}.d2h-lines-added{background-color:#ceffce;border:1px solid #b4e2b4;color:#399839;border-radius:5px 0 0 5px;padding:2px;width:25px}.d2h-lines-deleted{background-color:#f7c8c8;border:1px solid #e9aeae;color:#c33;border-radius:0 5px 5px 0;padding:2px;width:25px}.d2h-file-name{display:inline;height:33px;line-height:33px;max-width:80%;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}.d2h-diff-table{border-collapse:collapse;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;height:18px;line-height:18px;width:100%}.d2h-files-diff{width:100%}.d2h-file-diff{overflow-x:scroll}.d2h-file-side-diff{display:inline-block;overflow-x:scroll;width:50%;margin-right:-4px}.d2h-code-line{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:80px;color:inherit;overflow-x:inherit;background:0 0}.d2h-code-side-line.hljs{display:block;white-space:pre;padding:0 10px;height:18px;line-height:18px;margin-left:50px;color:inherit;overflow-x:inherit;background:0 0}.d2h-code-line del,.d2h-code-side-line del{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#ffb6ba;border-radius:.2em}.d2h-code-line ins,.d2h-code-side-line ins{display:inline-block;margin-top:-1px;text-decoration:none;background-color:#97f295;border-radius:.2em}.line-num1{display:inline-block;float:left;width:30px;overflow:hidden;text-overflow:ellipsis}.line-num2{display:inline-block;float:right;width:30px;overflow:hidden;text-overflow:ellipsis}.d2h-code-linenumber{position:absolute;width:2%;min-width:65px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,.3);text-align:right;border:solid #eee;border-width:0 1px;cursor:pointer}.d2h-code-side-linenumber{position:absolute;width:35px;padding-left:10px;padding-right:10px;height:18px;line-height:18px;background-color:#fff;color:rgba(0,0,0,.3);text-align:right;border:solid #eee;border-width:0 1px;cursor:pointer;overflow:hidden;text-overflow:ellipsis}.d2h-del{background-color:#fee8e9;border-color:#e9aeae}.d2h-ins{background-color:#dfd;border-color:#b4e2b4}.d2h-info{background-color:#f8fafd;color:rgba(0,0,0,.3);border-color:#d5e4f2}
\ No newline at end of file
diff --git a/diff2html.min.js b/diff2html.min.js
index a54636b..a35f90a 100644
--- a/diff2html.min.js
+++ b/diff2html.min.js
@@ -1 +1 @@
-!function(window){var ClassVariable;return ClassVariable=function(){function Diff2Html(){}function escape(str){return str.slice(0).replace(/&/g,"&").replace(//g,">").replace(/\t/g," ")}function startsWith(str,start){return 0===str.indexOf(start)}function valueOrEmpty(value){return value?value:""}function diffHighlight(diffLine1,diffLine2){var highlightedLine=diffString(diffLine1.substr(1),diffLine2.substr(1));return{o:diffLine1.charAt(0)+removeIns(highlightedLine),n:diffLine2.charAt(0)+removeDel(highlightedLine)}}var LINE_TYPE={INSERTS:"d2h-ins",DELETES:"d2h-del",CONTEXT:"d2h-cntx",INFO:"d2h-info"};Diff2Html.prototype.getPrettyHtmlFromDiff=function(diffInput){var diffJson=generateDiffJson(diffInput);return generateJsonHtml(diffJson)},Diff2Html.prototype.getJsonFromDiff=function(diffInput){return generateDiffJson(diffInput)},Diff2Html.prototype.getPrettyHtmlFromJson=function(diffJson){return generateJsonHtml(diffJson)},Diff2Html.prototype.getPrettySideBySideHtmlFromDiff=function(diffInput){var diffJson=generateDiffJson(diffInput);return generateSideBySideJsonHtml(diffJson)},Diff2Html.prototype.getPrettySideBySideHtmlFromJson=function(diffJson){return generateSideBySideJsonHtml(diffJson)};var instance,generateDiffJson=function(diffInput){var files=[],currentFile=null,currentBlock=null,oldLine=null,newLine=null,saveBlock=function(){currentBlock&&(currentFile.blocks.push(currentBlock),currentBlock=null)},saveFile=function(){currentFile&¤tFile.newName&&(files.push(currentFile),currentFile=null)},startFile=function(){saveBlock(),saveFile(),currentFile={},currentFile.blocks=[],currentFile.deletedLines=0,currentFile.addedLines=0},startBlock=function(line){saveBlock();var values=/^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line)||/^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line)||[0,0,0];oldLine=values[1],newLine=values[2],currentBlock={},currentBlock.lines=[],currentBlock.oldStartLine=oldLine,currentBlock.newStartLine=newLine,currentBlock.header=line},createLine=function(line){var currentLine={};currentLine.content=line,startsWith(line,"+")||startsWith(line," +")?(currentFile.addedLines++,currentLine.type=LINE_TYPE.INSERTS,currentLine.oldNumber=null,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):startsWith(line,"-")||startsWith(line," -")?(currentFile.deletedLines++,currentLine.type=LINE_TYPE.DELETES,currentLine.oldNumber=oldLine++,currentLine.newNumber=null,currentBlock.lines.push(currentLine)):(currentLine.type=LINE_TYPE.CONTEXT,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine))},diffLines=diffInput.split("\n");return diffLines.forEach(function(line){if(line&&!startsWith(line,"*")&&!startsWith(line,"new")&&!startsWith(line,"index")){var values=[];startsWith(line,"diff")?startFile():currentFile&&!currentFile.oldName&&(values=/^--- a\/(\S+).*$/.exec(line))?currentFile.oldName=values[1]:currentFile&&!currentFile.newName&&(values=/^\+\+\+ b\/(\S+).*$/.exec(line))?currentFile.newName=values[1]:currentFile&&startsWith(line,"@@")?startBlock(line):currentBlock&&createLine(line)}}),saveBlock(),saveFile(),files},generateJsonHtml=function(diffFiles){return'\n'+diffFiles.map(function(file){return'
\n \n
\n
\n
\n \n '+generateFileHtml(file)+" \n
\n
\n
\n
\n"}).join("\n")+"
\n"},generateFileHtml=function(file){return file.blocks.map(function(block){for(var lines='\n | \n '+escape(block.header)+" | \n
\n",i=0;i\n '+valueOrEmpty(oldNumber)+' '+valueOrEmpty(newNumber)+' | \n '+content+" | \n\n"},generateSideBySideJsonHtml=function(diffFiles){return'\n'+diffFiles.map(function(file){var diffs=generateSideBySideFileHtml(file);return'
\n"}).join("\n")+"
\n"},generateSideBySideFileHtml=function(file){var fileHtml={};return fileHtml.left="",fileHtml.right="",file.blocks.forEach(function(block){fileHtml.left+='\n | \n '+escape(block.header)+" | \n
\n",fileHtml.right+='\n | \n | \n
\n';for(var i=0;i\n '+number+' | \n '+content+" | \n \n"},getDiffName=function(oldFilename,newFilename){return oldFilename&&newFilename&&oldFilename!==newFilename?oldFilename+" -> "+newFilename:newFilename?newFilename:oldFilename?oldFilename:"Unknown filename"},removeIns=function(line){return line.replace(/(((.|\n)*?)<\/ins>)/g,"")},removeDel=function(line){return line.replace(/(((.|\n)*?)<\/del>)/g,"")};return{getInstance:function(){return void 0===instance&&(instance=new Diff2Html,instance.constructor=null),instance}}}(),window.Diff2Html=ClassVariable.getInstance(),window.Diff2Html}(window);
\ No newline at end of file
+function Diff2Html(){}function escape(str){return str.slice(0).replace(/&/g,"&").replace(//g,">").replace(/\t/g," ")}function startsWith(str,start){return 0===str.indexOf(start)}function valueOrEmpty(value){return value?value:""}function diffHighlight(diffLine1,diffLine2){var highlightedLine=JsDiff(diffLine1.substr(1),diffLine2.substr(1));return{o:diffLine1.charAt(0)+removeIns(highlightedLine),n:diffLine2.charAt(0)+removeDel(highlightedLine)}}var LINE_TYPE={INSERTS:"d2h-ins",DELETES:"d2h-del",CONTEXT:"d2h-cntx",INFO:"d2h-info"};Diff2Html.prototype.getPrettyHtmlFromDiff=function(diffInput){var diffJson=generateDiffJson(diffInput);return generateJsonHtml(diffJson)},Diff2Html.prototype.getJsonFromDiff=function(diffInput){return generateDiffJson(diffInput)},Diff2Html.prototype.getPrettyHtmlFromJson=function(diffJson){return generateJsonHtml(diffJson)},Diff2Html.prototype.getPrettySideBySideHtmlFromDiff=function(diffInput){var diffJson=generateDiffJson(diffInput);return generateSideBySideJsonHtml(diffJson)},Diff2Html.prototype.getPrettySideBySideHtmlFromJson=function(diffJson){return generateSideBySideJsonHtml(diffJson)};var generateDiffJson=function(diffInput){var files=[],currentFile=null,currentBlock=null,oldLine=null,newLine=null,saveBlock=function(){currentBlock&&(currentFile.blocks.push(currentBlock),currentBlock=null)},saveFile=function(){currentFile&¤tFile.newName&&(files.push(currentFile),currentFile=null)},startFile=function(){saveBlock(),saveFile(),currentFile={},currentFile.blocks=[],currentFile.deletedLines=0,currentFile.addedLines=0},startBlock=function(line){saveBlock();var values=/^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line)||/^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line)||[0,0,0];oldLine=values[1],newLine=values[2],currentBlock={},currentBlock.lines=[],currentBlock.oldStartLine=oldLine,currentBlock.newStartLine=newLine,currentBlock.header=line},createLine=function(line){var currentLine={};currentLine.content=line,startsWith(line,"+")||startsWith(line," +")?(currentFile.addedLines++,currentLine.type=LINE_TYPE.INSERTS,currentLine.oldNumber=null,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine)):startsWith(line,"-")||startsWith(line," -")?(currentFile.deletedLines++,currentLine.type=LINE_TYPE.DELETES,currentLine.oldNumber=oldLine++,currentLine.newNumber=null,currentBlock.lines.push(currentLine)):(currentLine.type=LINE_TYPE.CONTEXT,currentLine.oldNumber=oldLine++,currentLine.newNumber=newLine++,currentBlock.lines.push(currentLine))},diffLines=diffInput.split("\n");return diffLines.forEach(function(line){if(line&&!startsWith(line,"*")&&!startsWith(line,"new")&&!startsWith(line,"index")){var values=[];if(startsWith(line,"diff"))startFile();else if(currentFile&&!currentFile.oldName&&(values=/^--- a\/(\S+).*$/.exec(line)))currentFile.oldName=values[1];else if(currentFile&&!currentFile.newName&&(values=/^\+\+\+ [b]?\/(\S+).*$/.exec(line))){currentFile.newName=values[1];var fileSplit=currentFile.newName.split(".");currentFile.language=fileSplit[fileSplit.length-1]}else currentFile&&startsWith(line,"@@")?startBlock(line):currentBlock&&createLine(line)}}),saveBlock(),saveFile(),files},generateJsonHtml=function(diffFiles){return'\n'+diffFiles.map(function(file){return'
\n \n
\n
\n
\n \n '+generateFileHtml(file)+" \n
\n
\n
\n
\n"}).join("\n")+"
\n"},generateFileHtml=function(file){return file.blocks.map(function(block){for(var lines='\n | \n '+escape(block.header)+" | \n
\n",i=0;i\n '+valueOrEmpty(oldNumber)+' '+valueOrEmpty(newNumber)+' | \n '+content+" | \n\n"},generateSideBySideJsonHtml=function(diffFiles){return'\n'+diffFiles.map(function(file){var diffs=generateSideBySideFileHtml(file);return'
\n"}).join("\n")+"
\n"},generateSideBySideFileHtml=function(file){var fileHtml={};return fileHtml.left="",fileHtml.right="",file.blocks.forEach(function(block){fileHtml.left+='\n | \n '+escape(block.header)+" | \n
\n",fileHtml.right+='\n | \n | \n
\n';for(var i=0;i\n '+number+' | \n '+content+" | \n \n"},getDiffName=function(oldFilename,newFilename){return oldFilename&&newFilename&&oldFilename!==newFilename?oldFilename+" -> "+newFilename:newFilename?newFilename:oldFilename?oldFilename:"Unknown filename"},removeIns=function(line){return line.replace(/(((.|\n)*?)<\/ins>)/g,"")},removeDel=function(line){return line.replace(/(((.|\n)*?)<\/del>)/g,"")};
\ No newline at end of file
diff --git a/index.html b/index.html
index 380122a..f788c02 100644
--- a/index.html
+++ b/index.html
@@ -181,7 +181,8 @@
'-});\n';
$(document).ready(function () {
- var diffJson = Diff2Html.getJsonFromDiff(lineDiffExample);
+ var diff2Html = new Diff2Html();
+ var diffJson = diff2Html.getJsonFromDiff(lineDiffExample);
var languages = diffJson.map(function (line) {
return line.language;
@@ -191,13 +192,13 @@
});
hljs.configure({languages: uniqueLanguages});
- $("#line-by-line").html(Diff2Html.getPrettyHtmlFromJson(diffJson));
+ $("#line-by-line").html(diff2Html.getPrettyHtmlFromJson(diffJson));
var code = $(".d2h-code-line");
code.map(function (i, line) {
hljs.highlightBlock(line);
});
- $("#side-by-side").html(Diff2Html.getPrettySideBySideHtmlFromJson(diffJson));
+ $("#side-by-side").html(diff2Html.getPrettySideBySideHtmlFromJson(diffJson));
var codeSide = $(".d2h-code-side-line");
codeSide.map(function (i, line) {
hljs.highlightBlock(line);
diff --git a/jsdiff.js b/jsdiff.js
index 70792d4..1175278 100644
--- a/jsdiff.js
+++ b/jsdiff.js
@@ -10,7 +10,7 @@
* http://ejohn.org/projects/javascript-diff-algorithm/
*/
-function diffString(o, n) {
+function JsDiff(o, n) {
o = o.replace(/\s+$/, '');
n = n.replace(/\s+$/, '');
@@ -83,7 +83,7 @@ function diff(o, n) {
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] == 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};
}
@@ -91,7 +91,7 @@ function diff(o, n) {
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] == 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};
}