Skip to content

Commit 2aaae31

Browse files
committed
Initial template override support
1 parent d4bab74 commit 2aaae31

11 files changed

+62
-29
lines changed

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: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,36 @@
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;
1716

18-
function HoganJsUtils() {
17+
function HoganJsUtils(configuration) {
18+
this.config = configuration || {};
19+
extraTemplates = this.config.templates || {};
1920
}
2021

21-
HoganJsUtils.prototype.render = function(namespace, view, params, configuration) {
22-
var template = this.template(namespace, view, configuration);
22+
HoganJsUtils.prototype.render = function(namespace, view, params) {
23+
var template = this.template(namespace, view);
2324
if (template) {
2425
return template.render(params);
2526
}
2627

2728
return null;
2829
};
2930

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

34-
return this._getTemplate(templateKey, config);
34+
return this._getTemplate(templateKey);
3535
};
3636

37-
HoganJsUtils.prototype._getTemplate = function(templateKey, config) {
37+
HoganJsUtils.prototype._getTemplate = function(templateKey) {
3838
var template;
3939

40-
if (!config.noCache) {
40+
if (!this.config.noCache) {
4141
template = this._readFromCache(templateKey);
4242
}
4343

@@ -53,6 +53,7 @@
5353

5454
try {
5555
if (fs.readFileSync) {
56+
var templatesPath = path.resolve(__dirname, 'templates');
5657
var templatePath = path.join(templatesPath, templateKey);
5758
var templateContent = fs.readFileSync(templatePath + '.mustache', 'utf8');
5859
template = hogan.compile(templateContent);
@@ -66,12 +67,16 @@
6667
};
6768

6869
HoganJsUtils.prototype._readFromCache = function(templateKey) {
69-
return hoganTemplates[templateKey];
70+
return extraTemplates[templateKey] || hoganTemplates[templateKey];
7071
};
7172

7273
HoganJsUtils.prototype._templateKey = function(namespace, view) {
7374
return namespace + '-' + view;
7475
};
7576

76-
module.exports.HoganJsUtils = new HoganJsUtils();
77+
HoganJsUtils.prototype.compile = function(templateStr) {
78+
return hogan.compile(templateStr);
79+
};
80+
81+
module.exports.HoganJsUtils = HoganJsUtils;
7782
})();

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: 15 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,28 @@ 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', 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+
});
3547
});
3648
});

test/line-by-line-tests.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('LineByLinePrinter', function() {
1414
' File without changes\n' +
1515
' </div>\n' +
1616
' </td>\n' +
17-
'</tr>\n';
17+
'</tr>';
1818

1919
assert.equal(expected, fileHtml);
2020
});
@@ -422,7 +422,6 @@ describe('LineByLinePrinter', function() {
422422
' </div>\n' +
423423
' </td>\n' +
424424
'</tr>\n' +
425-
'\n' +
426425
' </tbody>\n' +
427426
' </table>\n' +
428427
' </div>\n' +

0 commit comments

Comments
 (0)