a post with code diff
You can display diff code by using the regular markdown syntax:
```diff
diff --git a/sample.js b/sample.js
index 0000001..0ddf2ba
--- a/sample.js
+++ b/sample.js
@@ -1 +1 @@
-console.log("Hello World!")
+console.log("Hello from Diff2Html!")
```
Which generates:
diff --git a/sample.js b/sample.js
index 0000001..0ddf2ba
--- a/sample.js
+++ b/sample.js
@@ -1 +1 @@
-console.log("Hello World!")
+console.log("Hello from Diff2Html!")
But this is difficult to read, specially if you have a large diff. You can use diff2html to display a more readable version of the diff. For this, just use diff2html
instead of diff
for the code block language:
```diff2html
diff --git a/sample.js b/sample.js
index 0000001..0ddf2ba
--- a/sample.js
+++ b/sample.js
@@ -1 +1 @@
-console.log("Hello World!")
+console.log("Hello from Diff2Html!")
```
If we use a longer example, for example this commit from diff2html, it will generate the following output:
From 2aaae31cc2a37bfff83430c2c914b140bee59b6a Mon Sep 17 00:00:00 2001
From: Rodrigo Fernandes <rtfrodrigo@gmail.com>
Date: Sun, 9 Oct 2016 16:41:54 +0100
Subject: [PATCH 1/2] Initial template override support
---
scripts/hulk.js | 4 ++--
src/diff2html.js | 3 +--
src/file-list-printer.js | 11 ++++++++---
src/hoganjs-utils.js | 29 +++++++++++++++++------------
src/html-printer.js | 6 ++++++
src/line-by-line-printer.js | 6 +++++-
src/side-by-side-printer.js | 6 +++++-
test/file-list-printer-tests.js | 2 +-
test/hogan-cache-tests.js | 18 +++++++++++++++---
test/line-by-line-tests.js | 3 +--
test/side-by-side-printer-tests.js | 3 +--
11 files changed, 62 insertions(+), 29 deletions(-)
diff --git a/scripts/hulk.js b/scripts/hulk.js
index 5a793c18..a4b1a4d5 100755
--- a/scripts/hulk.js
+++ b/scripts/hulk.js
@@ -173,11 +173,11 @@ function namespace(name) {
// write a template foreach file that matches template extension
templates = extractFiles(options.argv.remain)
.map(function(file) {
- var openedFile = fs.readFileSync(file, 'utf-8');
+ var openedFile = fs.readFileSync(file, 'utf-8').trim();
var name;
if (!openedFile) return;
name = namespace(path.basename(file).replace(/\..*$/, ''));
- openedFile = removeByteOrderMark(openedFile.trim());
+ openedFile = removeByteOrderMark(openedFile);
openedFile = wrap(file, name, openedFile);
if (!options.outputdir) return openedFile;
fs.writeFileSync(path.join(options.outputdir, name + '.js')
diff --git a/src/diff2html.js b/src/diff2html.js
index 21b0119e..64e138f5 100644
--- a/src/diff2html.js
+++ b/src/diff2html.js
@@ -7,7 +7,6 @@
(function() {
var diffParser = require('./diff-parser.js').DiffParser;
- var fileLister = require('./file-list-printer.js').FileListPrinter;
var htmlPrinter = require('./html-printer.js').HtmlPrinter;
function Diff2Html() {
@@ -43,7 +42,7 @@
var fileList = '';
if (configOrEmpty.showFiles === true) {
- fileList = fileLister.generateFileList(diffJson, configOrEmpty);
+ fileList = htmlPrinter.generateFileListSummary(diffJson, configOrEmpty);
}
var diffOutput = '';
diff --git a/src/file-list-printer.js b/src/file-list-printer.js
index e408d9b2..1e0a2c61 100644
--- a/src/file-list-printer.js
+++ b/src/file-list-printer.js
@@ -8,11 +8,16 @@
(function() {
var printerUtils = require('./printer-utils.js').PrinterUtils;
- var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
+ var hoganUtils;
+
var baseTemplatesPath = 'file-summary';
var iconsBaseTemplatesPath = 'icon';
- function FileListPrinter() {
+ function FileListPrinter(config) {
+ this.config = config;
+
+ var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
+ hoganUtils = new HoganJsUtils(config);
}
FileListPrinter.prototype.generateFileList = function(diffFiles) {
@@ -38,5 +43,5 @@
});
};
- module.exports.FileListPrinter = new FileListPrinter();
+ module.exports.FileListPrinter = FileListPrinter;
})();
diff --git a/src/hoganjs-utils.js b/src/hoganjs-utils.js
index 9949e5fa..0dda08d7 100644
--- a/src/hoganjs-utils.js
+++ b/src/hoganjs-utils.js
@@ -8,18 +8,19 @@
(function() {
var fs = require('fs');
var path = require('path');
-
var hogan = require('hogan.js');
var hoganTemplates = require('./templates/diff2html-templates.js');
- var templatesPath = path.resolve(__dirname, 'templates');
+ var extraTemplates;
- function HoganJsUtils() {
+ function HoganJsUtils(configuration) {
+ this.config = configuration || {};
+ extraTemplates = this.config.templates || {};
}
- HoganJsUtils.prototype.render = function(namespace, view, params, configuration) {
- var template = this.template(namespace, view, configuration);
+ HoganJsUtils.prototype.render = function(namespace, view, params) {
+ var template = this.template(namespace, view);
if (template) {
return template.render(params);
}
@@ -27,17 +28,16 @@
return null;
};
- HoganJsUtils.prototype.template = function(namespace, view, configuration) {
- var config = configuration || {};
+ HoganJsUtils.prototype.template = function(namespace, view) {
var templateKey = this._templateKey(namespace, view);
- return this._getTemplate(templateKey, config);
+ return this._getTemplate(templateKey);
};
- HoganJsUtils.prototype._getTemplate = function(templateKey, config) {
+ HoganJsUtils.prototype._getTemplate = function(templateKey) {
var template;
- if (!config.noCache) {
+ if (!this.config.noCache) {
template = this._readFromCache(templateKey);
}
@@ -53,6 +53,7 @@
try {
if (fs.readFileSync) {
+ var templatesPath = path.resolve(__dirname, 'templates');
var templatePath = path.join(templatesPath, templateKey);
var templateContent = fs.readFileSync(templatePath + '.mustache', 'utf8');
template = hogan.compile(templateContent);
@@ -66,12 +67,16 @@
};
HoganJsUtils.prototype._readFromCache = function(templateKey) {
- return hoganTemplates[templateKey];
+ return extraTemplates[templateKey] || hoganTemplates[templateKey];
};
HoganJsUtils.prototype._templateKey = function(namespace, view) {
return namespace + '-' + view;
};
- module.exports.HoganJsUtils = new HoganJsUtils();
+ HoganJsUtils.prototype.compile = function(templateStr) {
+ return hogan.compile(templateStr);
+ };
+
+ module.exports.HoganJsUtils = HoganJsUtils;
})();
diff --git a/src/html-printer.js b/src/html-printer.js
index 585d5b66..13f83047 100644
--- a/src/html-printer.js
+++ b/src/html-printer.js
@@ -8,6 +8,7 @@
(function() {
var LineByLinePrinter = require('./line-by-line-printer.js').LineByLinePrinter;
var SideBySidePrinter = require('./side-by-side-printer.js').SideBySidePrinter;
+ var FileListPrinter = require('./file-list-printer.js').FileListPrinter;
function HtmlPrinter() {
}
@@ -22,5 +23,10 @@
return sideBySidePrinter.generateSideBySideJsonHtml(diffFiles);
};
+ HtmlPrinter.prototype.generateFileListSummary = function(diffJson, config) {
+ var fileListPrinter = new FileListPrinter(config);
+ return fileListPrinter.generateFileList(diffJson);
+ };
+
module.exports.HtmlPrinter = new HtmlPrinter();
})();
diff --git a/src/line-by-line-printer.js b/src/line-by-line-printer.js
index b07eb53c..d230bedd 100644
--- a/src/line-by-line-printer.js
+++ b/src/line-by-line-printer.js
@@ -11,7 +11,8 @@
var utils = require('./utils.js').Utils;
var Rematch = require('./rematch.js').Rematch;
- var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
+ var hoganUtils;
+
var genericTemplatesPath = 'generic';
var baseTemplatesPath = 'line-by-line';
var iconsBaseTemplatesPath = 'icon';
@@ -19,6 +20,9 @@
function LineByLinePrinter(config) {
this.config = config;
+
+ var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
+ hoganUtils = new HoganJsUtils(config);
}
LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) {
diff --git a/src/side-by-side-printer.js b/src/side-by-side-printer.js
index bbf1dc8d..5e3033b3 100644
--- a/src/side-by-side-printer.js
+++ b/src/side-by-side-printer.js
@@ -11,7 +11,8 @@
var utils = require('./utils.js').Utils;
var Rematch = require('./rematch.js').Rematch;
- var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
+ var hoganUtils;
+
var genericTemplatesPath = 'generic';
var baseTemplatesPath = 'side-by-side';
var iconsBaseTemplatesPath = 'icon';
@@ -26,6 +27,9 @@
function SideBySidePrinter(config) {
this.config = config;
+
+ var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
+ hoganUtils = new HoganJsUtils(config);
}
SideBySidePrinter.prototype.makeDiffHtml = function(file, diffs) {
diff --git a/test/file-list-printer-tests.js b/test/file-list-printer-tests.js
index a502a46f..60ea3208 100644
--- a/test/file-list-printer-tests.js
+++ b/test/file-list-printer-tests.js
@@ -1,6 +1,6 @@
var assert = require('assert');
-var fileListPrinter = require('../src/file-list-printer.js').FileListPrinter;
+var fileListPrinter = new (require('../src/file-list-printer.js').FileListPrinter)();
describe('FileListPrinter', function() {
describe('generateFileList', function() {
diff --git a/test/hogan-cache-tests.js b/test/hogan-cache-tests.js
index 190bf6f8..3bb754ac 100644
--- a/test/hogan-cache-tests.js
+++ b/test/hogan-cache-tests.js
@@ -1,6 +1,6 @@
var assert = require('assert');
-var HoganJsUtils = require('../src/hoganjs-utils.js').HoganJsUtils;
+var HoganJsUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)();
var diffParser = require('../src/diff-parser.js').DiffParser;
describe('HoganJsUtils', function() {
@@ -21,16 +21,28 @@ describe('HoganJsUtils', function() {
});
assert.equal(emptyDiffHtml, result);
});
+
it('should render view without cache', function() {
var result = HoganJsUtils.render('generic', 'empty-diff', {
contentClass: 'd2h-code-line',
diffParser: diffParser
}, {noCache: true});
- assert.equal(emptyDiffHtml + '\n', result);
+ assert.equal(emptyDiffHtml, result);
});
+
it('should return null if template is missing', function() {
- var result = HoganJsUtils.render('generic', 'missing-template', {}, {noCache: true});
+ var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)({noCache: true});
+ var result = hoganUtils.render('generic', 'missing-template', {});
assert.equal(null, result);
});
+
+ it('should allow templates to be overridden', function() {
+ var emptyDiffTemplate = HoganJsUtils.compile('<p></p>');
+
+ var config = {templates: {'generic-empty-diff': emptyDiffTemplate}};
+ var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
+ var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
+ assert.equal('<p>Rodrigo Fernandes</p>', result);
+ });
});
});
diff --git a/test/line-by-line-tests.js b/test/line-by-line-tests.js
index 1cd92073..8869b3df 100644
--- a/test/line-by-line-tests.js
+++ b/test/line-by-line-tests.js
@@ -14,7 +14,7 @@ describe('LineByLinePrinter', function() {
' File without changes\n' +
' </div>\n' +
' </td>\n' +
- '</tr>\n';
+ '</tr>';
assert.equal(expected, fileHtml);
});
@@ -422,7 +422,6 @@ describe('LineByLinePrinter', function() {
' </div>\n' +
' </td>\n' +
'</tr>\n' +
- '\n' +
' </tbody>\n' +
' </table>\n' +
' </div>\n' +
diff --git a/test/side-by-side-printer-tests.js b/test/side-by-side-printer-tests.js
index 76625f8e..771daaa5 100644
--- a/test/side-by-side-printer-tests.js
+++ b/test/side-by-side-printer-tests.js
@@ -14,7 +14,7 @@ describe('SideBySidePrinter', function() {
' File without changes\n' +
' </div>\n' +
' </td>\n' +
- '</tr>\n';
+ '</tr>';
assert.equal(expectedRight, fileHtml.right);
assert.equal(expectedLeft, fileHtml.left);
@@ -324,7 +324,6 @@ describe('SideBySidePrinter', function() {
' </div>\n' +
' </td>\n' +
'</tr>\n' +
- '\n' +
' </tbody>\n' +
' </table>\n' +
' </div>\n' +
From f3cadb96677d0eb82fc2752dc3ffbf35ca9b5bdb Mon Sep 17 00:00:00 2001
From: Rodrigo Fernandes <rtfrodrigo@gmail.com>
Date: Sat, 15 Oct 2016 13:21:22 +0100
Subject: [PATCH 2/2] Allow uncompiled templates
---
README.md | 3 +++
src/hoganjs-utils.js | 7 +++++++
test/hogan-cache-tests.js | 24 +++++++++++++++++++++++-
3 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 132c8a28..46909f25 100644
--- a/README.md
+++ b/README.md
@@ -98,6 +98,9 @@ The HTML output accepts a Javascript object with configuration. Possible options
- `synchronisedScroll`: scroll both panes in side-by-side mode: `true` or `false`, default is `false`
- `matchWordsThreshold`: similarity threshold for word matching, default is 0.25
- `matchingMaxComparisons`: perform at most this much comparisons for line matching a block of changes, default is `2500`
+ - `templates`: object with previously compiled templates to replace parts of the html
+ - `rawTemplates`: object with raw not compiled templates to replace parts of the html
+ > For more information regarding the possible templates look into [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates)
## Diff2HtmlUI Helper
diff --git a/src/hoganjs-utils.js b/src/hoganjs-utils.js
index 0dda08d7..b2e9c275 100644
--- a/src/hoganjs-utils.js
+++ b/src/hoganjs-utils.js
@@ -17,6 +17,13 @@
function HoganJsUtils(configuration) {
this.config = configuration || {};
extraTemplates = this.config.templates || {};
+
+ var rawTemplates = this.config.rawTemplates || {};
+ for (var templateName in rawTemplates) {
+ if (rawTemplates.hasOwnProperty(templateName)) {
+ if (!extraTemplates[templateName]) extraTemplates[templateName] = this.compile(rawTemplates[templateName]);
+ }
+ }
}
HoganJsUtils.prototype.render = function(namespace, view, params) {
diff --git a/test/hogan-cache-tests.js b/test/hogan-cache-tests.js
index 3bb754ac..a34839c0 100644
--- a/test/hogan-cache-tests.js
+++ b/test/hogan-cache-tests.js
@@ -36,7 +36,7 @@ describe('HoganJsUtils', function() {
assert.equal(null, result);
});
- it('should allow templates to be overridden', function() {
+ it('should allow templates to be overridden with compiled templates', function() {
var emptyDiffTemplate = HoganJsUtils.compile('<p></p>');
var config = {templates: {'generic-empty-diff': emptyDiffTemplate}};
@@ -44,5 +44,27 @@ describe('HoganJsUtils', function() {
var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
assert.equal('<p>Rodrigo Fernandes</p>', result);
});
+
+ it('should allow templates to be overridden with uncompiled templates', function() {
+ var emptyDiffTemplate = '<p></p>';
+
+ var config = {rawTemplates: {'generic-empty-diff': emptyDiffTemplate}};
+ var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
+ var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
+ assert.equal('<p>Rodrigo Fernandes</p>', result);
+ });
+
+ it('should allow templates to be overridden giving priority to compiled templates', function() {
+ var emptyDiffTemplate = HoganJsUtils.compile('<p></p>');
+ var emptyDiffTemplateUncompiled = '<p>Not used!</p>';
+
+ var config = {
+ templates: {'generic-empty-diff': emptyDiffTemplate},
+ rawTemplates: {'generic-empty-diff': emptyDiffTemplateUncompiled}
+ };
+ var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
+ var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
+ assert.equal('<p>Rodrigo Fernandes</p>', result);
+ });
});
});
Files changed (14)
show
scripts/hulk.js
CHANGED
@@ -173,11 +173,11 @@ function namespace(name) {
|
|
173
173
|
// write a template foreach file that matches template extension
|
174
174
|
templates = extractFiles(options.argv.remain)
|
175
175
|
.map(function(file) {
|
176
|
-
var openedFile = fs.readFileSync(file, 'utf-8');
|
176
|
+
var openedFile = fs.readFileSync(file, 'utf-8').trim();
|
177
177
|
var name;
|
178
178
|
if (!openedFile) return;
|
179
179
|
name = namespace(path.basename(file).replace(/\..*$/, ''));
|
180
|
-
openedFile = removeByteOrderMark(openedFile
|
180
|
+
openedFile = removeByteOrderMark(openedFile);
|
181
181
|
openedFile = wrap(file, name, openedFile);
|
182
182
|
if (!options.outputdir) return openedFile;
|
183
183
|
fs.writeFileSync(path.join(options.outputdir, name + '.js')
|
src/diff2html.js
CHANGED
@@ -7,7 +7,6 @@
|
|
7
7
|
(function() {
|
8
8
|
var diffParser = require('./diff-parser.js').DiffParser;
|
9
|
-
var fileLister = require('./file-list-printer.js').FileListPrinter;
|
10
9
|
var htmlPrinter = require('./html-printer.js').HtmlPrinter;
|
11
10
|
function Diff2Html() {
|
@@ -43,7 +42,7 @@
|
|
43
42
|
var fileList = '';
|
44
43
|
if (configOrEmpty.showFiles === true) {
|
45
|
-
fileList =
|
44
|
+
fileList = htmlPrinter.generateFileListSummary(diffJson, configOrEmpty);
|
46
45
|
}
|
47
46
|
var diffOutput = '';
|
src/file-list-printer.js
CHANGED
@@ -8,11 +8,16 @@
|
|
8
8
|
(function() {
|
9
9
|
var printerUtils = require('./printer-utils.js').PrinterUtils;
|
10
|
-
var hoganUtils
|
10
|
+
var hoganUtils;
|
11
|
+
|
11
12
|
var baseTemplatesPath = 'file-summary';
|
12
13
|
var iconsBaseTemplatesPath = 'icon';
|
13
|
-
function FileListPrinter() {
|
14
|
+
function FileListPrinter(config) {
|
15
|
+
this.config = config;
|
16
|
+
|
17
|
+
var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
|
18
|
+
hoganUtils = new HoganJsUtils(config);
|
14
19
|
}
|
15
20
|
FileListPrinter.prototype.generateFileList = function(diffFiles) {
|
@@ -38,5 +43,5 @@
|
|
38
43
|
});
|
39
44
|
};
|
40
|
-
module.exports.FileListPrinter =
|
45
|
+
module.exports.FileListPrinter = FileListPrinter;
|
41
46
|
})();
|
src/hoganjs-utils.js
CHANGED
@@ -8,18 +8,19 @@
|
|
8
8
|
(function() {
|
9
9
|
var fs = require('fs');
|
10
10
|
var path = require('path');
|
11
|
-
|
12
11
|
var hogan = require('hogan.js');
|
13
12
|
var hoganTemplates = require('./templates/diff2html-templates.js');
|
14
|
-
var
|
13
|
+
var extraTemplates;
|
15
|
-
function HoganJsUtils() {
|
14
|
+
function HoganJsUtils(configuration) {
|
15
|
+
this.config = configuration || {};
|
16
|
+
extraTemplates = this.config.templates || {};
|
16
17
|
}
|
17
|
-
HoganJsUtils.prototype.render = function(namespace, view, params
|
18
|
+
HoganJsUtils.prototype.render = function(namespace, view, params) {
|
18
|
-
var template = this.template(namespace, view
|
19
|
+
var template = this.template(namespace, view);
|
19
20
|
if (template) {
|
20
21
|
return template.render(params);
|
21
22
|
}
|
@@ -27,17 +28,16 @@
|
|
27
28
|
return null;
|
28
29
|
};
|
29
|
-
HoganJsUtils.prototype.template = function(namespace, view
|
30
|
+
HoganJsUtils.prototype.template = function(namespace, view) {
|
30
|
-
var config = configuration || {};
|
31
31
|
var templateKey = this._templateKey(namespace, view);
|
32
|
-
return this._getTemplate(templateKey
|
32
|
+
return this._getTemplate(templateKey);
|
33
33
|
};
|
34
|
-
HoganJsUtils.prototype._getTemplate = function(templateKey
|
34
|
+
HoganJsUtils.prototype._getTemplate = function(templateKey) {
|
35
35
|
var template;
|
36
|
-
if (!config.noCache) {
|
36
|
+
if (!this.config.noCache) {
|
37
37
|
template = this._readFromCache(templateKey);
|
38
38
|
}
|
@@ -53,6 +53,7 @@
|
|
53
53
|
try {
|
54
54
|
if (fs.readFileSync) {
|
55
|
+
var templatesPath = path.resolve(__dirname, 'templates');
|
55
56
|
var templatePath = path.join(templatesPath, templateKey);
|
56
57
|
var templateContent = fs.readFileSync(templatePath + '.mustache', 'utf8');
|
57
58
|
template = hogan.compile(templateContent);
|
@@ -66,12 +67,16 @@
|
|
66
67
|
};
|
67
68
|
HoganJsUtils.prototype._readFromCache = function(templateKey) {
|
68
|
-
return hoganTemplates[templateKey];
|
69
|
+
return extraTemplates[templateKey] || hoganTemplates[templateKey];
|
69
70
|
};
|
70
71
|
HoganJsUtils.prototype._templateKey = function(namespace, view) {
|
71
72
|
return namespace + '-' + view;
|
72
73
|
};
|
74
|
+
HoganJsUtils.prototype.compile = function(templateStr) {
|
75
|
+
return hogan.compile(templateStr);
|
76
|
+
};
|
77
|
+
|
73
|
-
module.exports.HoganJsUtils =
|
78
|
+
module.exports.HoganJsUtils = HoganJsUtils;
|
74
79
|
})();
|
src/html-printer.js
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
(function() {
|
9
9
|
var LineByLinePrinter = require('./line-by-line-printer.js').LineByLinePrinter;
|
10
10
|
var SideBySidePrinter = require('./side-by-side-printer.js').SideBySidePrinter;
|
11
|
+
var FileListPrinter = require('./file-list-printer.js').FileListPrinter;
|
11
12
|
function HtmlPrinter() {
|
12
13
|
}
|
@@ -22,5 +23,10 @@
|
|
22
23
|
return sideBySidePrinter.generateSideBySideJsonHtml(diffFiles);
|
23
24
|
};
|
25
|
+
HtmlPrinter.prototype.generateFileListSummary = function(diffJson, config) {
|
26
|
+
var fileListPrinter = new FileListPrinter(config);
|
27
|
+
return fileListPrinter.generateFileList(diffJson);
|
28
|
+
};
|
29
|
+
|
24
30
|
module.exports.HtmlPrinter = new HtmlPrinter();
|
25
31
|
})();
|
src/line-by-line-printer.js
CHANGED
@@ -11,7 +11,8 @@
|
|
11
11
|
var utils = require('./utils.js').Utils;
|
12
12
|
var Rematch = require('./rematch.js').Rematch;
|
13
|
-
var hoganUtils
|
13
|
+
var hoganUtils;
|
14
|
+
|
14
15
|
var genericTemplatesPath = 'generic';
|
15
16
|
var baseTemplatesPath = 'line-by-line';
|
16
17
|
var iconsBaseTemplatesPath = 'icon';
|
@@ -19,6 +20,9 @@
|
|
19
20
|
function LineByLinePrinter(config) {
|
20
21
|
this.config = config;
|
22
|
+
|
23
|
+
var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
|
24
|
+
hoganUtils = new HoganJsUtils(config);
|
21
25
|
}
|
22
26
|
LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) {
|
src/side-by-side-printer.js
CHANGED
@@ -11,7 +11,8 @@
|
|
11
11
|
var utils = require('./utils.js').Utils;
|
12
12
|
var Rematch = require('./rematch.js').Rematch;
|
13
|
-
var hoganUtils
|
13
|
+
var hoganUtils;
|
14
|
+
|
14
15
|
var genericTemplatesPath = 'generic';
|
15
16
|
var baseTemplatesPath = 'side-by-side';
|
16
17
|
var iconsBaseTemplatesPath = 'icon';
|
@@ -26,6 +27,9 @@
|
|
26
27
|
function SideBySidePrinter(config) {
|
27
28
|
this.config = config;
|
29
|
+
|
30
|
+
var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
|
31
|
+
hoganUtils = new HoganJsUtils(config);
|
28
32
|
}
|
29
33
|
SideBySidePrinter.prototype.makeDiffHtml = function(file, diffs) {
|
test/file-list-printer-tests.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
var assert = require('assert');
|
2
|
-
var fileListPrinter = require('../src/file-list-printer.js').FileListPrinter;
|
2
|
+
var fileListPrinter = new (require('../src/file-list-printer.js').FileListPrinter)();
|
3
3
|
describe('FileListPrinter', function() {
|
4
4
|
describe('generateFileList', function() {
|
test/hogan-cache-tests.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
var assert = require('assert');
|
2
|
-
var HoganJsUtils = require('../src/hoganjs-utils.js').HoganJsUtils;
|
2
|
+
var HoganJsUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)();
|
3
3
|
var diffParser = require('../src/diff-parser.js').DiffParser;
|
4
4
|
describe('HoganJsUtils', function() {
|
@@ -21,16 +21,28 @@ describe('HoganJsUtils', function() {
|
|
21
21
|
});
|
22
22
|
assert.equal(emptyDiffHtml, result);
|
23
23
|
});
|
24
|
+
|
24
25
|
it('should render view without cache', function() {
|
25
26
|
var result = HoganJsUtils.render('generic', 'empty-diff', {
|
26
27
|
contentClass: 'd2h-code-line',
|
27
28
|
diffParser: diffParser
|
28
29
|
}, {noCache: true});
|
29
|
-
assert.equal(emptyDiffHtml
|
30
|
+
assert.equal(emptyDiffHtml, result);
|
30
31
|
});
|
32
|
+
|
31
33
|
it('should return null if template is missing', function() {
|
34
|
+
var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)({noCache: true});
|
32
|
-
var result =
|
35
|
+
var result = hoganUtils.render('generic', 'missing-template', {});
|
33
36
|
assert.equal(null, result);
|
34
37
|
});
|
38
|
+
|
39
|
+
it('should allow templates to be overridden', function() {
|
40
|
+
var emptyDiffTemplate = HoganJsUtils.compile('<p></p>');
|
41
|
+
|
42
|
+
var config = {templates: {'generic-empty-diff': emptyDiffTemplate}};
|
43
|
+
var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
|
44
|
+
var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
|
45
|
+
assert.equal('<p>Rodrigo Fernandes</p>', result);
|
46
|
+
});
|
35
47
|
});
|
36
48
|
});
|
test/line-by-line-tests.js
CHANGED
@@ -14,7 +14,7 @@ describe('LineByLinePrinter', function() {
|
|
14
14
|
' File without changes\n' +
|
15
15
|
' </div>\n' +
|
16
16
|
' </td>\n' +
|
17
|
-
'</tr
|
17
|
+
'</tr>';
|
18
18
|
assert.equal(expected, fileHtml);
|
19
19
|
});
|
@@ -422,7 +422,6 @@ describe('LineByLinePrinter', function() {
|
|
422
422
|
' </div>\n' +
|
423
423
|
' </td>\n' +
|
424
424
|
'</tr>\n' +
|
425
|
-
'\n' +
|
426
425
|
' </tbody>\n' +
|
427
426
|
' </table>\n' +
|
428
427
|
' </div>\n' +
|
test/side-by-side-printer-tests.js
CHANGED
@@ -14,7 +14,7 @@ describe('SideBySidePrinter', function() {
|
|
14
14
|
' File without changes\n' +
|
15
15
|
' </div>\n' +
|
16
16
|
' </td>\n' +
|
17
|
-
'</tr
|
17
|
+
'</tr>';
|
18
18
|
assert.equal(expectedRight, fileHtml.right);
|
19
19
|
assert.equal(expectedLeft, fileHtml.left);
|
@@ -324,7 +324,6 @@ describe('SideBySidePrinter', function() {
|
|
324
324
|
' </div>\n' +
|
325
325
|
' </td>\n' +
|
326
326
|
'</tr>\n' +
|
327
|
-
'\n' +
|
328
327
|
' </tbody>\n' +
|
329
328
|
' </table>\n' +
|
330
329
|
' </div>\n' +
|
331
|
-
--
|
332
330
|
README.md | 3 +++
|
333
331
|
src/hoganjs-utils.js | 7 +++++++
|
334
332
|
test/hogan-cache-tests.js | 24 +++++++++++++++++++++++-
|
335
333
|
3 files changed, 33 insertions(+), 1 deletion(-)
|
README.md
CHANGED
@@ -98,6 +98,9 @@ The HTML output accepts a Javascript object with configuration. Possible options
|
|
98
98
|
- `synchronisedScroll`: scroll both panes in side-by-side mode: `true` or `false`, default is `false`
|
99
99
|
- `matchWordsThreshold`: similarity threshold for word matching, default is 0.25
|
100
100
|
- `matchingMaxComparisons`: perform at most this much comparisons for line matching a block of changes, default is `2500`
|
101
|
+
- `templates`: object with previously compiled templates to replace parts of the html
|
102
|
+
- `rawTemplates`: object with raw not compiled templates to replace parts of the html
|
103
|
+
> For more information regarding the possible templates look into [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates)
|
101
104
|
## Diff2HtmlUI Helper
|
src/hoganjs-utils.js
CHANGED
@@ -17,6 +17,13 @@
|
|
17
17
|
function HoganJsUtils(configuration) {
|
18
18
|
this.config = configuration || {};
|
19
19
|
extraTemplates = this.config.templates || {};
|
20
|
+
|
21
|
+
var rawTemplates = this.config.rawTemplates || {};
|
22
|
+
for (var templateName in rawTemplates) {
|
23
|
+
if (rawTemplates.hasOwnProperty(templateName)) {
|
24
|
+
if (!extraTemplates[templateName]) extraTemplates[templateName] = this.compile(rawTemplates[templateName]);
|
25
|
+
}
|
26
|
+
}
|
20
27
|
}
|
21
28
|
HoganJsUtils.prototype.render = function(namespace, view, params) {
|
test/hogan-cache-tests.js
CHANGED
@@ -36,7 +36,7 @@ describe('HoganJsUtils', function() {
|
|
36
36
|
assert.equal(null, result);
|
37
37
|
});
|
38
|
-
it('should allow templates to be overridden', function() {
|
38
|
+
it('should allow templates to be overridden with compiled templates', function() {
|
39
39
|
var emptyDiffTemplate = HoganJsUtils.compile('<p></p>');
|
40
40
|
var config = {templates: {'generic-empty-diff': emptyDiffTemplate}};
|
@@ -44,5 +44,27 @@ describe('HoganJsUtils', function() {
|
|
44
44
|
var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
|
45
45
|
assert.equal('<p>Rodrigo Fernandes</p>', result);
|
46
46
|
});
|
47
|
+
|
48
|
+
it('should allow templates to be overridden with uncompiled templates', function() {
|
49
|
+
var emptyDiffTemplate = '<p></p>';
|
50
|
+
|
51
|
+
var config = {rawTemplates: {'generic-empty-diff': emptyDiffTemplate}};
|
52
|
+
var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
|
53
|
+
var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
|
54
|
+
assert.equal('<p>Rodrigo Fernandes</p>', result);
|
55
|
+
});
|
56
|
+
|
57
|
+
it('should allow templates to be overridden giving priority to compiled templates', function() {
|
58
|
+
var emptyDiffTemplate = HoganJsUtils.compile('<p></p>');
|
59
|
+
var emptyDiffTemplateUncompiled = '<p>Not used!</p>';
|
60
|
+
|
61
|
+
var config = {
|
62
|
+
templates: {'generic-empty-diff': emptyDiffTemplate},
|
63
|
+
rawTemplates: {'generic-empty-diff': emptyDiffTemplateUncompiled}
|
64
|
+
};
|
65
|
+
var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)(config);
|
66
|
+
var result = hoganUtils.render('generic', 'empty-diff', {myName: 'Rodrigo Fernandes'});
|
67
|
+
assert.equal('<p>Rodrigo Fernandes</p>', result);
|
68
|
+
});
|
47
69
|
});
|
48
70
|
});
|
Enjoy Reading This Article?
Here are some more articles you might like to read next: