all-contributors-cli/lib/generate/format-contributor.js

39 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-03-01 22:30:14 +00:00
'use strict';
var _ = require('lodash/fp');
2016-05-05 14:27:26 +00:00
var formatContributionType = require('./format-contribution-type');
2016-03-01 22:30:14 +00:00
var avatarTemplate = _.template('<img src="<%= contributor.avatar_url %>" width="<%= options.imageSize %>px;"/>');
var avatarBlockTemplate = _.template('[<%= avatar %><br /><sub><b><%= name %></b></sub>](<%= contributor.profile %>)');
2016-03-01 22:30:14 +00:00
var contributorTemplate = _.template('<%= avatarBlock %><br /><%= contributions %>');
var defaultImageSize = 100;
2016-03-01 22:30:14 +00:00
function defaultTemplate(templateData) {
var avatar = avatarTemplate(templateData);
var avatarBlock = avatarBlockTemplate(_.assign({
name: escapeName(templateData.contributor.name),
avatar: avatar
}, templateData));
2016-03-06 23:20:24 +00:00
return contributorTemplate(_.assign({avatarBlock: avatarBlock}, templateData));
2016-03-01 22:30:14 +00:00
}
function escapeName(name) {
return name.replace(new RegExp('\\|', 'g'), '&#124;');
}
2016-03-01 22:30:14 +00:00
module.exports = function formatContributor(options, contributor) {
var formatter = _.partial(formatContributionType, [options, contributor]);
var contributions = contributor.contributions
.map(formatter)
.join(' ');
2016-03-01 22:30:14 +00:00
var templateData = {
contributions: contributions,
contributor: contributor,
options: _.assign({imageSize: defaultImageSize}, options)
2016-03-01 22:30:14 +00:00
};
var customTemplate = options.contributorTemplate && _.template(options.contributorTemplate);
2016-03-01 22:30:14 +00:00
return (customTemplate || defaultTemplate)(templateData);
};