Skip to content

Commit

Permalink
Merge pull request #15 from rtfpessoa/cleaning
Browse files Browse the repository at this point in the history
Clean project, better module exposing and documentation
  • Loading branch information
Rodrigo Fernandes committed Jul 19, 2015
2 parents ffeaec2 + aac689e commit df6d5bb
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 128 deletions.
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Diff to Html by [rtfpessoa](https://github.com/rtfpessoa)

Diff to Html generates pretty HTML diffs from git word diff output.
Diff to Html generates pretty HTML diffs from git diff output.

## Features

Expand All @@ -12,6 +12,8 @@ Diff to Html generates pretty HTML diffs from git word diff output.

* GitHub like style

* Code syntax highlight

## Online Example

> Go to [Diff2HTML](http://rtfpessoa.github.io/diff2html/)
Expand All @@ -24,6 +26,8 @@ Diff to Html generates pretty HTML diffs from git word diff output.

* [Bower Package](http://bower.io/search/?q=diff2html)

* [Node CLI](https://www.npmjs.org/package/diff2html-cli)

* Manually download and import `dist/diff2html.min.js` into your page

## How to use
Expand All @@ -50,11 +54,60 @@ Diff to Html generates pretty HTML diffs from git word diff output.

> Check out the `index.html` for a complete example.
## Sintax Hightlight

> Add the dependencies.
Choose one color scheme, and add the main highlight code.
If your favourite language is not included in the default package also add its javascript highlight file.
jQuery is optional, just using it to help managing the highlight.

```html
<!-- Stylesheet -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/styles/github.min.css">

<!-- Javascripts -->
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/languages/scala.min.js"></script>
```

> Invoke the highlightjs plugin
```js
document.addEventListener("DOMContentLoaded", function () {
// parse the diff to json
var diffJson = Diff2Html.getJsonFromDiff(lineDiffExample);

// collect all the file extensions in the json
var allFileLanguages = diffJson.map(function (line) {
return line.language;
});

// remove duplicated languages
var distinctLanguages = allFileLanguages.filter(function (v, i) {
return allFileLanguages.indexOf(v) == i;
});

// pass the languages to the highlightjs plugin
hljs.configure({languages: distinctLanguages});

// generate and inject the diff HTML into the desired place
document.getElementById("line-by-line").innerHTML = Diff2Html.getPrettyHtmlFromJson(diffJson);
document.getElementById("side-by-side").innerHTML = Diff2Html.getPrettySideBySideHtmlFromJson(diffJson);

// collect all the code lines and execute the highlight on them
var codeLines = document.getElementsByClassName("d2h-code-line-ctn");
[].forEach.call(codeLines, function (line) {
hljs.highlightBlock(line);
});
});
```

## Contributions

All the contributions are welcome.

To contribute just send a pull request with your feature,fix,... and it will be reviewed asap.
To contribute just send a pull request with your changes and I will review it asap.

## License

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "diff2html",
"version": "0.2.5-1",
"homepage": "https://github.com/rtfpessoa/diff2html",
"homepage": "http://rtfpessoa.github.io/diff2html/",
"description": "Fast Diff to colorized HTML",
"keywords": [
"git",
Expand Down
140 changes: 83 additions & 57 deletions dist/diff2html.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
// Diff2Html minifier version (automatically generated)
var global = this;
/*
* Hack to allow nodejs require("package/file") in the browser
* How?
* Since every require is used as an object:
* `require("./utils.js").Utils` // (notice the `.Utils`)
*
* We can say that when there is no require method
* we use the global object in which the `Utils`
* object was already injected.
*/

var $globalHolder = (typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof this !== 'undefined' && this) ||
Function('return this')();
function require() {
return global;
}/* See LICENSE file for terms of use */
return $globalHolder;
}
/* See LICENSE file for terms of use */

/*
* Text diff implementation.
Expand Down Expand Up @@ -652,7 +669,7 @@ function require() {
*
*/

(function (global, undefined) {
(function (ctx, undefined) {

function Utils() {
}
Expand All @@ -673,11 +690,13 @@ function require() {
return value ? value : "";
};

if (typeof module !== 'undefined' && module.exports) {
module.exports.Utils = new Utils();
} else if (typeof global.Utils === 'undefined') {
global.Utils = new Utils();
}
// expose this module
((typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof $this !== 'undefined' && $this) ||
Function('return this')())["Utils"] = new Utils();

})(this);
/*
Expand All @@ -687,7 +706,7 @@ function require() {
*
*/

(function (global, undefined) {
(function (ctx, undefined) {

var utils = require("./utils.js").Utils;

Expand Down Expand Up @@ -895,11 +914,13 @@ function require() {
else return language;
}

if (typeof module !== 'undefined' && module.exports) {
module.exports.DiffParser = new DiffParser();
} else if (typeof global.DiffParser === 'undefined') {
global.DiffParser = new DiffParser();
}
// expose this module
((typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof $this !== 'undefined' && $this) ||
Function('return this')())["DiffParser"] = new DiffParser();

})(this);
/*
Expand All @@ -909,12 +930,10 @@ function require() {
*
*/

(function (global, undefined) {
(function (ctx, undefined) {

// dirty hack for browser compatibility
var jsDiff = (typeof JsDiff !== "undefined" && JsDiff) ||
require("diff") ||
require("../lib/diff.js");
var jsDiff = (typeof JsDiff !== "undefined" && JsDiff) || require("diff");
var utils = require("./utils.js").Utils;

function PrinterUtils() {
Expand Down Expand Up @@ -988,11 +1007,13 @@ function require() {
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();
}
// expose this module
((typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof $this !== 'undefined' && $this) ||
Function('return this')())["PrinterUtils"] = new PrinterUtils();

})(this);
/*
Expand All @@ -1002,7 +1023,7 @@ function require() {
*
*/

(function (global, undefined) {
(function (ctx, undefined) {

var diffParser = require("./diff-parser.js").DiffParser;
var printerUtils = require("./printer-utils.js").PrinterUtils;
Expand Down Expand Up @@ -1183,11 +1204,13 @@ function require() {
return fileHtml;
}

if (typeof module !== 'undefined' && module.exports) {
module.exports.SideBySidePrinter = new SideBySidePrinter();
} else if (typeof global.SideBySidePrinter === 'undefined') {
global.SideBySidePrinter = new SideBySidePrinter();
}
// expose this module
((typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof $this !== 'undefined' && $this) ||
Function('return this')())["SideBySidePrinter"] = new SideBySidePrinter();

})(this);
/*
Expand All @@ -1197,7 +1220,7 @@ function require() {
*
*/

(function (global, undefined) {
(function (ctx, undefined) {

var diffParser = require("./diff-parser.js").DiffParser;
var printerUtils = require("./printer-utils.js").PrinterUtils;
Expand Down Expand Up @@ -1342,11 +1365,13 @@ function require() {
"</tr>\n";
}

if (typeof module !== 'undefined' && module.exports) {
module.exports.LineByLinePrinter = new LineByLinePrinter();
} else if (typeof global.LineByLinePrinter === 'undefined') {
global.LineByLinePrinter = new LineByLinePrinter();
}
// expose this module
((typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof $this !== 'undefined' && $this) ||
Function('return this')())["LineByLinePrinter"] = new LineByLinePrinter();

})(this);
/*
Expand All @@ -1356,7 +1381,7 @@ function require() {
*
*/

(function (global, undefined) {
(function (ctx, undefined) {

var lineByLinePrinter = require("./line-by-line-printer.js").LineByLinePrinter;
var sideBySidePrinter = require("./side-by-side-printer.js").SideBySidePrinter;
Expand All @@ -1368,23 +1393,23 @@ function require() {

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();
}
// expose this module
((typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof $this !== 'undefined' && $this) ||
Function('return this')())["HtmlPrinter"] = new HtmlPrinter();

})(this);
/*
*
* Diff to HTML (diff2html.js)
* Author: rtfpessoa
*
* Diff commands:
* git diff
*/

(function (global, undefined) {
(function (ctx, undefined) {

var diffParser = require("./diff-parser.js").DiffParser;
var htmlPrinter = require("./html-printer.js").HtmlPrinter;
Expand All @@ -1393,13 +1418,12 @@ function require() {
}

/*
* config
* {
* "wordByWord" : true (default)
* OR
* "charByChar" : true
* }
*
* Line diff type configuration
var config = {
"wordByWord": true, // (default)
// OR
"charByChar": true
};
*/

/*
Expand Down Expand Up @@ -1444,10 +1468,12 @@ function require() {
return htmlPrinter.generateSideBySideJsonHtml(diffJson, configOrEmpty);
};

if (typeof module !== 'undefined' && module.exports) {
module.exports.Diff2Html = new Diff2Html();
} else if (typeof global.Diff2Html === 'undefined') {
global.Diff2Html = new Diff2Html();
}
// expose this module
((typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof $this !== 'undefined' && $this) ||
Function('return this')())["Diff2Html"] = new Diff2Html();

})(this);
2 changes: 1 addition & 1 deletion dist/diff2html.min.js

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions lib/fakeRequire.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
var global = this;
/*
* Hack to allow nodejs require("package/file") in the browser
* How?
* Since every require is used as an object:
* `require("./utils.js").Utils` // (notice the `.Utils`)
*
* We can say that when there is no require method
* we use the global object in which the `Utils`
* object was already injected.
*/

var $globalHolder = (typeof module !== 'undefined' && module.exports) ||
(typeof exports !== 'undefined' && exports) ||
(typeof window !== 'undefined' && window) ||
(typeof self !== 'undefined' && self) ||
(typeof this !== 'undefined' && this) ||
Function('return this')();
function require() {
return global;
}
return $globalHolder;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "diff2html",
"version": "0.2.5-1",
"homepage": "https://www.github.com/rtfpessoa/diff2html",
"version": "0.2.6",
"homepage": "http://rtfpessoa.github.io/diff2html/",
"description": "Fast Diff to colorized HTML",
"keywords": [
"git",
Expand Down
Loading

0 comments on commit df6d5bb

Please sign in to comment.