Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/gtn/contributors.rb
1677 views
1
# frozen_string_literal: true
2
3
require 'jekyll'
4
require 'time'
5
6
module Gtn
7
# Parse the git repo to get some facts
8
module Contributors
9
@HAS_WARNED_ON = []
10
11
##
12
# Returns contributors, regardless of whether they are 'contributor' or 'contributions' style
13
# Params:
14
# +data+:: +Hash+ of the YAML frontmatter from a material
15
# Returns:
16
# +Array+ of contributor IDs
17
def self.get_contributors(data)
18
if data.key?('contributors')
19
data['contributors']
20
elsif data.key?('contributions')
21
data['contributions'].values.flatten
22
else
23
[]
24
end
25
end
26
27
##
28
# Returns authors, only entities that are primary authors
29
# Params:
30
# +data+:: +Hash+ of the YAML frontmatter from a material
31
# Returns:
32
# +Array+ of contributor IDs
33
def self.get_authors(data)
34
if data.key?('contributors')
35
data['contributors'] || []
36
elsif data.key?('contributions')
37
data['contributions']['authorship'] || []
38
else
39
[]
40
end
41
end
42
43
##
44
# Returns event organisers
45
# Params:
46
# +data+:: +Hash+ of the YAML frontmatter from a material
47
# Returns:
48
# +Array+ of contributor IDs
49
def self.get_organisers(data)
50
if data.key?('contributions') && data['contributions'].key?('organisers')
51
data['contributions']['organisers']
52
else
53
[]
54
end
55
end
56
57
##
58
# Returns event instructors
59
# Params:
60
# +data+:: +Hash+ of the YAML frontmatter from a material
61
# Returns:
62
# +Array+ of contributor IDs
63
def self.get_instructors(data)
64
if data.key?('contributions') && data['contributions'].key?('instructors')
65
data['contributions']['instructors']
66
else
67
[]
68
end
69
end
70
71
##
72
# Get the non-author contributors of a material.
73
# Params:
74
# +data+:: +Hash+ of the YAML frontmatter from a material
75
# Returns:
76
# +Array+ of contributor IDs
77
def self.get_non_authors(material)
78
if material.key?('contributors')
79
[]
80
elsif material.key?('contributions')
81
material['contributions']
82
.reject { |k| k == 'funding' }
83
.reject { |k| k == 'authorship' }
84
.values.flatten.uniq
85
else
86
[]
87
end
88
end
89
90
##
91
# Get the funders (organisations) of a material.
92
# Params:
93
# +data+:: +Hash+ of the YAML frontmatter from a material
94
# Returns:
95
# +Array+ of contributor IDs
96
def self.get_funders(site, data)
97
self.get_all_funding(site, data).reject{ |f| site.data['grants'].key?(f) }
98
end
99
100
##
101
# Get the funders (grants) of a material.
102
# Params:
103
# +site+:: +Jekyll::Site+ object
104
# +data+:: +Hash+ of the YAML frontmatter from a material
105
# Returns:
106
# +Array+ of grant IDs
107
def self.get_grants(site, data)
108
self.get_all_funding(site, data).select{ |f| site.data['grants'].key?(f) }
109
end
110
111
##
112
# Get the funders (grants + organisations) of a material.
113
# Params:
114
# +site+:: +Jekyll::Site+ object
115
# +data+:: +Hash+ of the YAML frontmatter from a material
116
# Returns:
117
# +Array+ of funder IDs mixing grants + organisations
118
def self.get_all_funding(site, data)
119
if data.key?('contributions') && data['contributions'].key?('funding')
120
# The ones specifically in the Grants table
121
data['contributions']['funding']
122
else
123
[]
124
end
125
end
126
127
# Convenience method to allow us to handle nil sites, and load directly
128
# from disk ourselves.
129
def self._load_file(site, category)
130
if site.nil?
131
Jekyll.logger.warn "[GTN/Contributor] Loading #{category} from disk, this access could be improved"
132
File.open("_data/#{category}.yml", 'r') { |f| YAML.safe_load(f) }
133
else
134
site.data[category]
135
end
136
end
137
138
##
139
# Map a contributor ID to their information and type
140
# Params:
141
# +site+:: +Jekyll::Site+ object
142
# +c+:: +String+ of contributor ID
143
# Returns:
144
# +Hash+ of contributor information
145
# +String+ type of contributor (e.g. 'contributor', 'organisation', 'grant')
146
def self.fetch(site, c, warn: false)
147
if _load_file(site, 'contributors').key?(c)
148
return ['contributor', site.data['contributors'][c]]
149
elsif _load_file(site, 'organisations').key?(c)
150
return ['organisation', site.data['organisations'][c]]
151
elsif _load_file(site, 'grants').key?(c)
152
return ['grant', site.data['grants'][c]]
153
else
154
if ! warn
155
if ! @HAS_WARNED_ON.include?(c)
156
Jekyll.logger.warn "Contributor #{c} not found"
157
@HAS_WARNED_ON.push(c)
158
end
159
end
160
end
161
162
['contributor', { 'name' => c }]
163
end
164
165
##
166
# Map a contributor ID to their information and type
167
# Params:
168
# +site+:: +Jekyll::Site+ object
169
# +c+:: +String+ of contributor ID
170
# Returns:
171
# +Hash+ of contributor information
172
def self.fetch_contributor(site, c)
173
fetch(site, c)[1]
174
end
175
176
##
177
# Map a contributor ID to their information and type
178
# Params:
179
# +site+:: +Jekyll::Site+ object
180
# +c+:: +String+ of contributor ID
181
# Returns:
182
# +String+ of contributor name
183
def self.fetch_name(site, c, warn: false)
184
fetch(site, c, warn: warn)[1].fetch('name', c)
185
end
186
187
##
188
# List ALL contributors
189
# Params:
190
# +site+:: +Jekyll::Site+ object
191
# Returns:
192
# +Hash+ of contributors, grants, organisations merged together
193
def self.list(site)
194
site.data['contributors']
195
.merge(site.data['grants'])
196
.merge(site.data['organisations'])
197
.reject { |c| c['halloffame'] == 'no' }
198
end
199
200
##
201
# Check if a specific contributor is a person or not
202
# Params:
203
# +c+:: +String+ of contributor ID
204
# Returns:
205
# +Boolean+ of whether the contributor is a contributor or not
206
def self.person?(site, c)
207
site.data['contributors'].key?(c)
208
end
209
210
##
211
# Check if a specific contributor is a grant or not
212
# Params:
213
# +c+:: +String+ of contributor ID
214
# Returns:
215
# +Boolean+ of whether the contributor is a grant or not
216
def self.grant?(site, c)
217
site.data['grants'].key?(c)
218
end
219
220
##
221
# Obtain the contributor's funding URL
222
# Params:
223
# +c+:: +String+ of contributor ID
224
# Returns:
225
# +Boolean+ of whether the contributor is a funder or not
226
def self.fetch_funding_url(contributor)
227
return contributor['funding_id'] if !contributor.key?('funding_database')
228
229
case contributor['funding_database']
230
when 'cordis'
231
"https://cordis.europa.eu/project/id/#{contributor['funding_id']}"
232
when 'erasmusplus'
233
"https://erasmus-plus.ec.europa.eu/projects/search/details/#{contributor['funding_id']}"
234
when 'ukri'
235
"https://gtr.ukri.org/projects?ref=#{contributor['funding_id']}"
236
when 'highergov'
237
"https://www.highergov.com/contract/#{contributor['funding_id']}/"
238
when 'dfg'
239
"https://gepris-extern.dfg.de/gepris/projekt/#{contributor['funding_id']}?language=en"
240
else
241
Jekyll.logger.error "Unknown funding system #{contributor['funding_database']}. Please let us know so we can add support for it!"
242
'ERROR'
243
end
244
end
245
end
246
end
247
248