Skip to content

Commit c2c253d

Browse files
authored
Merge pull request #106 from rtfpessoa/support-overriding-templates
Initial template override support
2 parents d4bab74 + f3cadb9 commit c2c253d

12 files changed

+94
-29
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ The HTML output accepts a Javascript object with configuration. Possible options
9898
- `synchronisedScroll`: scroll both panes in side-by-side mode: `true` or `false`, default is `false`
9999
- `matchWordsThreshold`: similarity threshold for word matching, default is 0.25
100100
- `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)
101104
102105
## Diff2HtmlUI Helper
103106

scripts/hulk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ function namespace(name) {
173173
// write a template foreach file that matches template extension
174174
templates = extractFiles(options.argv.remain)
175175
.map(function(file) {
176-
var openedFile = fs.readFileSync(file, 'utf-8');
176+
var openedFile = fs.readFileSync(file, 'utf-8').trim();
177177
var name;
178178
if (!openedFile) return;
179179
name = namespace(path.basename(file).replace(/\..*$/, ''));
180-
openedFile = removeByteOrderMark(openedFile.trim());
180+
openedFile = removeByteOrderMark(openedFile);
181181
openedFile = wrap(file, name, openedFile);
182182
if (!options.outputdir) return openedFile;
183183
fs.writeFileSync(path.join(options.outputdir, name + '.js')

src/diff2html.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
(function() {
99
var diffParser = require('./diff-parser.js').DiffParser;
10-
var fileLister = require('./file-list-printer.js').FileListPrinter;
1110
var htmlPrinter = require('./html-printer.js').HtmlPrinter;
1211

1312
function Diff2Html() {
@@ -43,7 +42,7 @@
4342

4443
var fileList = '';
4544
if (configOrEmpty.showFiles === true) {
46-
fileList = fileLister.generateFileList(diffJson, configOrEmpty);
45+
fileList = htmlPrinter.generateFileListSummary(diffJson, configOrEmpty);
4746
}
4847

4948
var diffOutput = '';

src/file-list-printer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
(function() {
99
var printerUtils = require('./printer-utils.js').PrinterUtils;
1010

11-
var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
11+
var hoganUtils;
12+
1213
var baseTemplatesPath = 'file-summary';
1314
var iconsBaseTemplatesPath = 'icon';
1415

15-
function FileListPrinter() {
16+
function FileListPrinter(config) {
17+
this.config = config;
18+
19+
var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
20+
hoganUtils = new HoganJsUtils(config);
1621
}
1722

1823
FileListPrinter.prototype.generateFileList = function(diffFiles) {
@@ -38,5 +43,5 @@
3843
});
3944
};
4045

41-
module.exports.FileListPrinter = new FileListPrinter();
46+
module.exports.FileListPrinter = FileListPrinter;
4247
})();

src/hoganjs-utils.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,43 @@
88
(function() {
99
var fs = require('fs');
1010
var path = require('path');
11-
1211
var hogan = require('hogan.js');
1312

1413
var hoganTemplates = require('./templates/diff2html-templates.js');
1514

16-
var templatesPath = path.resolve(__dirname, 'templates');
15+
var extraTemplates;
16+
17+
function HoganJsUtils(configuration) {
18+
this.config = configuration || {};
19+
extraTemplates = this.config.templates || {};
1720

18-
function HoganJsUtils() {
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+
}
1927
}
2028

21-
HoganJsUtils.prototype.render = function(namespace, view, params, configuration) {
22-
var template = this.template(namespace, view, configuration);
29+
HoganJsUtils.prototype.render = function(namespace, view, params) {
30+
var template = this.template(namespace, view);
2331
if (template) {
2432
return template.render(params);
2533
}
2634

2735
return null;
2836
};
2937

30-
HoganJsUtils.prototype.template = function(namespace, view, configuration) {
31-
var config = configuration || {};
38+
HoganJsUtils.prototype.template = function(namespace, view) {
3239
var templateKey = this._templateKey(namespace, view);
3340

34-
return this._getTemplate(templateKey, config);
41+
return this._getTemplate(templateKey);
3542
};
3643

37-
HoganJsUtils.prototype._getTemplate = function(templateKey, config) {
44+
HoganJsUtils.prototype._getTemplate = function(templateKey) {
3845
var template;
3946

40-
if (!config.noCache) {
47+
if (!this.config.noCache) {
4148
template = this._readFromCache(templateKey);
4249
}
4350

@@ -53,6 +60,7 @@
5360

5461
try {
5562
if (fs.readFileSync) {
63+
var templatesPath = path.resolve(__dirname, 'templates');
5664
var templatePath = path.join(templatesPath, templateKey);
5765
var templateContent = fs.readFileSync(templatePath + '.mustache', 'utf8');
5866
template = hogan.compile(templateContent);
@@ -66,12 +74,16 @@
6674
};
6775

6876
HoganJsUtils.prototype._readFromCache = function(templateKey) {
69-
return hoganTemplates[templateKey];
77+
return extraTemplates[templateKey] || hoganTemplates[templateKey];
7078
};
7179

7280
HoganJsUtils.prototype._templateKey = function(namespace, view) {
7381
return namespace + '-' + view;
7482
};
7583

76-
module.exports.HoganJsUtils = new HoganJsUtils();
84+
HoganJsUtils.prototype.compile = function(templateStr) {
85+
return hogan.compile(templateStr);
86+
};
87+
88+
module.exports.HoganJsUtils = HoganJsUtils;
7789
})();

src/html-printer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(function() {
99
var LineByLinePrinter = require('./line-by-line-printer.js').LineByLinePrinter;
1010
var SideBySidePrinter = require('./side-by-side-printer.js').SideBySidePrinter;
11+
var FileListPrinter = require('./file-list-printer.js').FileListPrinter;
1112

1213
function HtmlPrinter() {
1314
}
@@ -22,5 +23,10 @@
2223
return sideBySidePrinter.generateSideBySideJsonHtml(diffFiles);
2324
};
2425

26+
HtmlPrinter.prototype.generateFileListSummary = function(diffJson, config) {
27+
var fileListPrinter = new FileListPrinter(config);
28+
return fileListPrinter.generateFileList(diffJson);
29+
};
30+
2531
module.exports.HtmlPrinter = new HtmlPrinter();
2632
})();

src/line-by-line-printer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
var utils = require('./utils.js').Utils;
1212
var Rematch = require('./rematch.js').Rematch;
1313

14-
var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
14+
var hoganUtils;
15+
1516
var genericTemplatesPath = 'generic';
1617
var baseTemplatesPath = 'line-by-line';
1718
var iconsBaseTemplatesPath = 'icon';
1819
var tagsBaseTemplatesPath = 'tag';
1920

2021
function LineByLinePrinter(config) {
2122
this.config = config;
23+
24+
var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
25+
hoganUtils = new HoganJsUtils(config);
2226
}
2327

2428
LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) {

src/side-by-side-printer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
var utils = require('./utils.js').Utils;
1212
var Rematch = require('./rematch.js').Rematch;
1313

14-
var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils;
14+
var hoganUtils;
15+
1516
var genericTemplatesPath = 'generic';
1617
var baseTemplatesPath = 'side-by-side';
1718
var iconsBaseTemplatesPath = 'icon';
@@ -26,6 +27,9 @@
2627

2728
function SideBySidePrinter(config) {
2829
this.config = config;
30+
31+
var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
32+
hoganUtils = new HoganJsUtils(config);
2933
}
3034

3135
SideBySidePrinter.prototype.makeDiffHtml = function(file, diffs) {

test/file-list-printer-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var assert = require('assert');
22

3-
var fileListPrinter = require('../src/file-list-printer.js').FileListPrinter;
3+
var fileListPrinter = new (require('../src/file-list-printer.js').FileListPrinter)();
44

55
describe('FileListPrinter', function() {
66
describe('generateFileList', function() {

test/hogan-cache-tests.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var assert = require('assert');
22

3-
var HoganJsUtils = require('../src/hoganjs-utils.js').HoganJsUtils;
3+
var HoganJsUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)();
44
var diffParser = require('../src/diff-parser.js').DiffParser;
55

66
describe('HoganJsUtils', function() {
@@ -21,16 +21,50 @@ describe('HoganJsUtils', function() {
2121
});
2222
assert.equal(emptyDiffHtml, result);
2323
});
24+
2425
it('should render view without cache', function() {
2526
var result = HoganJsUtils.render('generic', 'empty-diff', {
2627
contentClass: 'd2h-code-line',
2728
diffParser: diffParser
2829
}, {noCache: true});
29-
assert.equal(emptyDiffHtml + '\n', result);
30+
assert.equal(emptyDiffHtml, result);
3031
});
32+
3133
it('should return null if template is missing', function() {
32-
var result = HoganJsUtils.render('generic', 'missing-template', {}, {noCache: true});
34+
var hoganUtils = new (require('../src/hoganjs-utils.js').HoganJsUtils)({noCache: true});
35+
var result = hoganUtils.render('generic', 'missing-template', {});
3336
assert.equal(null, result);
3437
});
38+
39+
it('should allow templates to be overridden with compiled templates', function() {
40+
var emptyDiffTemplate = HoganJsUtils.compile('<p>{{myName}}</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+
});
47+
48+
it('should allow templates to be overridden with uncompiled templates', function() {
49+
var emptyDiffTemplate = '<p>{{myName}}</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>{{myName}}</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+
});
3569
});
3670
});

0 commit comments

Comments
 (0)