Skip to content

Commit

Permalink
Merge pull request #106 from rtfpessoa/support-overriding-templates
Browse files Browse the repository at this point in the history
Initial template override support
  • Loading branch information
rtfpessoa committed Oct 15, 2016
2 parents d4bab74 + f3cadb9 commit c2c253d
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions scripts/hulk.js
Expand Up @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions src/diff2html.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -43,7 +42,7 @@

var fileList = '';
if (configOrEmpty.showFiles === true) {
fileList = fileLister.generateFileList(diffJson, configOrEmpty);
fileList = htmlPrinter.generateFileListSummary(diffJson, configOrEmpty);
}

var diffOutput = '';
Expand Down
11 changes: 8 additions & 3 deletions src/file-list-printer.js
Expand Up @@ -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) {
Expand All @@ -38,5 +43,5 @@
});
};

module.exports.FileListPrinter = new FileListPrinter();
module.exports.FileListPrinter = FileListPrinter;
})();
36 changes: 24 additions & 12 deletions src/hoganjs-utils.js
Expand Up @@ -8,36 +8,43 @@
(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(configuration) {
this.config = configuration || {};
extraTemplates = this.config.templates || {};

function HoganJsUtils() {
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, 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);
}

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);
}

Expand All @@ -53,6 +60,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);
Expand All @@ -66,12 +74,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;
})();
6 changes: 6 additions & 0 deletions src/html-printer.js
Expand Up @@ -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() {
}
Expand All @@ -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();
})();
6 changes: 5 additions & 1 deletion src/line-by-line-printer.js
Expand Up @@ -11,14 +11,18 @@
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';
var tagsBaseTemplatesPath = 'tag';

function LineByLinePrinter(config) {
this.config = config;

var HoganJsUtils = require('./hoganjs-utils.js').HoganJsUtils;
hoganUtils = new HoganJsUtils(config);
}

LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) {
Expand Down
6 changes: 5 additions & 1 deletion src/side-by-side-printer.js
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion 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() {
Expand Down
40 changes: 37 additions & 3 deletions 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() {
Expand All @@ -21,16 +21,50 @@ 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 with compiled templates', function() {
var emptyDiffTemplate = HoganJsUtils.compile('<p>{{myName}}</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);
});

it('should allow templates to be overridden with uncompiled templates', function() {
var emptyDiffTemplate = '<p>{{myName}}</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>{{myName}}</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);
});
});
});
3 changes: 1 addition & 2 deletions test/line-by-line-tests.js
Expand Up @@ -14,7 +14,7 @@ describe('LineByLinePrinter', function() {
' File without changes\n' +
' </div>\n' +
' </td>\n' +
'</tr>\n';
'</tr>';

assert.equal(expected, fileHtml);
});
Expand Down Expand Up @@ -422,7 +422,6 @@ describe('LineByLinePrinter', function() {
' </div>\n' +
' </td>\n' +
'</tr>\n' +
'\n' +
' </tbody>\n' +
' </table>\n' +
' </div>\n' +
Expand Down
3 changes: 1 addition & 2 deletions test/side-by-side-printer-tests.js
Expand Up @@ -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);
Expand Down Expand Up @@ -324,7 +324,6 @@ describe('SideBySidePrinter', function() {
' </div>\n' +
' </td>\n' +
'</tr>\n' +
'\n' +
' </tbody>\n' +
' </table>\n' +
' </div>\n' +
Expand Down

0 comments on commit c2c253d

Please sign in to comment.