forked from google/WebFundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwfContributors.js
125 lines (115 loc) · 3.93 KB
/
wfContributors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @fileoverview Reads the _contributors.yaml file and uses Handlebars to
* generate the primary contributors file and the individual include files.
* It also takes care of generating individual contributions pages.
*
* @author Pete LePage <petele@google.com>
*/
'use strict';
const path = require('path');
const fs = require('fs-extra');
const jsYaml = require('js-yaml');
const gutil = require('gulp-util');
const wfHelper = require('./wfHelper');
const wfTemplateHelper = require('./wfTemplateHelper');
const CONTRIBUTORS_FILE = './src/data/_contributors.yaml';
const PHOTO_PATH = './src/content/en/images/contributors/';
const TEMPLATE_LIST = './src/templates/contributors/index.md';
const DEST_LIST = './src/content/en/resources/contributors/index.md';
const TEMPLATE_INCLUDE = './src/templates/contributors/include.html';
const DEST_INCLUDE = './src/content/en/_shared/contributors/{{key}}.html';
const TEMPLATE_ARTICLE_LIST = './src/templates/contributors/article-list.md';
const DEST_ARTICLE_LIST = './src/content/en/resources/contributors/{{key}}.md';
const MISSING_AVATAR = 'is missing a photo, using simple avatar instead.';
/**
* Parses & normalizes a markdown file.
*
* @param {string} key The key identifying the user
* @return {string} filename to use
*/
function getPhotoForContributor(key) {
const localImagePath = path.join(PHOTO_PATH, key) + '.jpg';
try {
const stat = fs.statSync(localImagePath);
if (stat.isFile()) {
return key;
}
} catch (ex) {
// do nothing
}
gutil.log(' ', gutil.colors.red(key), MISSING_AVATAR);
return 'no-photo';
}
/**
* Loop through each contributor and render include file for each person.
*
* @param {Array} contributors The list of contributors
*/
function buildIncludes(contributors) {
gutil.log(' ', 'Building include file for each contributor...');
const keys = Object.keys(contributors);
keys.forEach(function(key) {
let contributor = contributors[key];
contributor.id = key;
contributor.photo = getPhotoForContributor(key);
const dest = DEST_INCLUDE.replace('{{key}}', key);
wfTemplateHelper.renderTemplate(TEMPLATE_INCLUDE, contributor, dest);
});
gutil.log(' ', 'Built', gutil.colors.magenta(keys.length + ' files'));
}
/**
* Build the index file for all contributors.
*
* @param {Array} contributors The list of contributors.
*/
function buildIndex(contributors) {
gutil.log(' ', 'Building index file of all contributors...');
const context = {contributors: contributors};
wfTemplateHelper.renderTemplate(TEMPLATE_LIST, context, DEST_LIST);
}
/**
* Loop through each contributor and render individaul contribution index.
*
* @param {Array} contributors The list of contributors
*/
function buildIndividualPages(contributors) {
gutil.log(' ', 'Building individual pages of all contributors...');
const files = wfHelper.getFileList(global.WF.src.content, ['**/*.md']);
const filesByAuthor = wfHelper.splitByAuthor(files);
const keys = Object.keys(contributors);
keys.forEach(function(key) {
if (!(key in filesByAuthor)) {
return;
}
const contributor = contributors[key];
filesByAuthor[key].sort(wfHelper.publishedComparator);
const context = {
id: key,
contributor: contributor,
articles: filesByAuthor[key],
};
const dest = DEST_ARTICLE_LIST.replace('{{key}}', key);
wfTemplateHelper.renderTemplate(TEMPLATE_ARTICLE_LIST, context, dest);
});
}
/**
* Reads the contributors file and parses it from YAML to JSON.
*
* @return {Array} The list of contributors
*/
function getContributors() {
gutil.log(' ', 'Reading contributors.yaml file...');
const yamlDoc = fs.readFileSync(CONTRIBUTORS_FILE, 'utf8');
return jsYaml.safeLoad(yamlDoc);
}
/**
* Build all of the necessary contributors files.
*
*/
function buildAll() {
const contributors = getContributors();
buildIncludes(contributors);
buildIndex(contributors);
buildIndividualPages(contributors);
}
exports.build = buildAll;