2016-03-01 22:30:14 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash/fp');
|
2016-03-06 09:40:19 +00:00
|
|
|
var formatBadge = require('./formatBadge');
|
2016-03-01 22:30:14 +00:00
|
|
|
var formatContributor = require('./formatContributor');
|
2016-03-21 22:53:02 +00:00
|
|
|
var injectContentBetween = require('../markdown').injectContentBetween;
|
2016-03-06 16:53:11 +00:00
|
|
|
|
2016-03-20 19:19:19 +00:00
|
|
|
function injectBetweenTags(tag, newContent) {
|
|
|
|
return function (previousContent) {
|
|
|
|
var tagToLookFor = '<!-- ALL-CONTRIBUTORS-' + tag + ':';
|
|
|
|
var closingTag = '-->';
|
|
|
|
var startOfOpeningTagIndex = previousContent.indexOf(tagToLookFor + 'START');
|
|
|
|
var endOfOpeningTagIndex = previousContent.indexOf(closingTag, startOfOpeningTagIndex);
|
|
|
|
var startOfClosingTagIndex = previousContent.indexOf(tagToLookFor + 'END', endOfOpeningTagIndex);
|
|
|
|
if (startOfOpeningTagIndex === -1 || endOfOpeningTagIndex === -1 || startOfClosingTagIndex === -1) {
|
|
|
|
return previousContent;
|
|
|
|
}
|
|
|
|
return previousContent.slice(0, endOfOpeningTagIndex + closingTag.length) +
|
|
|
|
newContent +
|
|
|
|
previousContent.slice(startOfClosingTagIndex);
|
|
|
|
};
|
|
|
|
}
|
2016-03-01 22:30:14 +00:00
|
|
|
|
|
|
|
function formatLine(contributors) {
|
|
|
|
return '| ' + contributors.join(' | ') + ' |';
|
|
|
|
}
|
|
|
|
|
2016-03-06 16:53:11 +00:00
|
|
|
function createColumnLine(options, contributors) {
|
|
|
|
var nbColumns = Math.min(options.contributorsPerLine, contributors.length);
|
|
|
|
return _.repeat(nbColumns, '| :---: ') + '|';
|
|
|
|
}
|
|
|
|
|
2016-03-01 22:30:14 +00:00
|
|
|
function generateContributorsList(options, contributors) {
|
|
|
|
return _.flow(
|
2016-03-06 15:49:46 +00:00
|
|
|
_.map(function formatEveryContributor(contributor) {
|
2016-03-01 22:30:14 +00:00
|
|
|
return formatContributor(options, contributor);
|
|
|
|
}),
|
|
|
|
_.chunk(options.contributorsPerLine),
|
|
|
|
_.map(formatLine),
|
2016-03-06 16:53:11 +00:00
|
|
|
function insertColumns(lines) {
|
|
|
|
var columnLine = createColumnLine(options, contributors);
|
|
|
|
return injectContentBetween(lines, columnLine, 1, 1);
|
|
|
|
},
|
|
|
|
_.join('\n')
|
2016-03-01 22:30:14 +00:00
|
|
|
)(contributors);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function generate(options, contributors, fileContent) {
|
2016-03-06 09:40:19 +00:00
|
|
|
var contributorsList = generateContributorsList(options, contributors);
|
|
|
|
var badge = formatBadge(options, contributors);
|
2016-03-01 22:30:14 +00:00
|
|
|
return _.flow(
|
2016-03-20 19:19:19 +00:00
|
|
|
injectBetweenTags('LIST', '\n' + contributorsList + '\n'),
|
2016-03-06 15:49:46 +00:00
|
|
|
injectBetweenTags('BADGE', badge)
|
2016-03-06 09:40:19 +00:00
|
|
|
)(fileContent);
|
2016-03-01 22:30:14 +00:00
|
|
|
};
|