Path: blob/main/_plugins/gtn/contributors.rb
1677 views
# frozen_string_literal: true12require 'jekyll'3require 'time'45module Gtn6# Parse the git repo to get some facts7module Contributors8@HAS_WARNED_ON = []910##11# Returns contributors, regardless of whether they are 'contributor' or 'contributions' style12# Params:13# +data+:: +Hash+ of the YAML frontmatter from a material14# Returns:15# +Array+ of contributor IDs16def self.get_contributors(data)17if data.key?('contributors')18data['contributors']19elsif data.key?('contributions')20data['contributions'].values.flatten21else22[]23end24end2526##27# Returns authors, only entities that are primary authors28# Params:29# +data+:: +Hash+ of the YAML frontmatter from a material30# Returns:31# +Array+ of contributor IDs32def self.get_authors(data)33if data.key?('contributors')34data['contributors'] || []35elsif data.key?('contributions')36data['contributions']['authorship'] || []37else38[]39end40end4142##43# Returns event organisers44# Params:45# +data+:: +Hash+ of the YAML frontmatter from a material46# Returns:47# +Array+ of contributor IDs48def self.get_organisers(data)49if data.key?('contributions') && data['contributions'].key?('organisers')50data['contributions']['organisers']51else52[]53end54end5556##57# Returns event instructors58# Params:59# +data+:: +Hash+ of the YAML frontmatter from a material60# Returns:61# +Array+ of contributor IDs62def self.get_instructors(data)63if data.key?('contributions') && data['contributions'].key?('instructors')64data['contributions']['instructors']65else66[]67end68end6970##71# Get the non-author contributors of a material.72# Params:73# +data+:: +Hash+ of the YAML frontmatter from a material74# Returns:75# +Array+ of contributor IDs76def self.get_non_authors(material)77if material.key?('contributors')78[]79elsif material.key?('contributions')80material['contributions']81.reject { |k| k == 'funding' }82.reject { |k| k == 'authorship' }83.values.flatten.uniq84else85[]86end87end8889##90# Get the funders (organisations) of a material.91# Params:92# +data+:: +Hash+ of the YAML frontmatter from a material93# Returns:94# +Array+ of contributor IDs95def self.get_funders(site, data)96self.get_all_funding(site, data).reject{ |f| site.data['grants'].key?(f) }97end9899##100# Get the funders (grants) of a material.101# Params:102# +site+:: +Jekyll::Site+ object103# +data+:: +Hash+ of the YAML frontmatter from a material104# Returns:105# +Array+ of grant IDs106def self.get_grants(site, data)107self.get_all_funding(site, data).select{ |f| site.data['grants'].key?(f) }108end109110##111# Get the funders (grants + organisations) of a material.112# Params:113# +site+:: +Jekyll::Site+ object114# +data+:: +Hash+ of the YAML frontmatter from a material115# Returns:116# +Array+ of funder IDs mixing grants + organisations117def self.get_all_funding(site, data)118if data.key?('contributions') && data['contributions'].key?('funding')119# The ones specifically in the Grants table120data['contributions']['funding']121else122[]123end124end125126# Convenience method to allow us to handle nil sites, and load directly127# from disk ourselves.128def self._load_file(site, category)129if site.nil?130Jekyll.logger.warn "[GTN/Contributor] Loading #{category} from disk, this access could be improved"131File.open("_data/#{category}.yml", 'r') { |f| YAML.safe_load(f) }132else133site.data[category]134end135end136137##138# Map a contributor ID to their information and type139# Params:140# +site+:: +Jekyll::Site+ object141# +c+:: +String+ of contributor ID142# Returns:143# +Hash+ of contributor information144# +String+ type of contributor (e.g. 'contributor', 'organisation', 'grant')145def self.fetch(site, c, warn: false)146if _load_file(site, 'contributors').key?(c)147return ['contributor', site.data['contributors'][c]]148elsif _load_file(site, 'organisations').key?(c)149return ['organisation', site.data['organisations'][c]]150elsif _load_file(site, 'grants').key?(c)151return ['grant', site.data['grants'][c]]152else153if ! warn154if ! @HAS_WARNED_ON.include?(c)155Jekyll.logger.warn "Contributor #{c} not found"156@HAS_WARNED_ON.push(c)157end158end159end160161['contributor', { 'name' => c }]162end163164##165# Map a contributor ID to their information and type166# Params:167# +site+:: +Jekyll::Site+ object168# +c+:: +String+ of contributor ID169# Returns:170# +Hash+ of contributor information171def self.fetch_contributor(site, c)172fetch(site, c)[1]173end174175##176# Map a contributor ID to their information and type177# Params:178# +site+:: +Jekyll::Site+ object179# +c+:: +String+ of contributor ID180# Returns:181# +String+ of contributor name182def self.fetch_name(site, c, warn: false)183fetch(site, c, warn: warn)[1].fetch('name', c)184end185186##187# List ALL contributors188# Params:189# +site+:: +Jekyll::Site+ object190# Returns:191# +Hash+ of contributors, grants, organisations merged together192def self.list(site)193site.data['contributors']194.merge(site.data['grants'])195.merge(site.data['organisations'])196.reject { |c| c['halloffame'] == 'no' }197end198199##200# Check if a specific contributor is a person or not201# Params:202# +c+:: +String+ of contributor ID203# Returns:204# +Boolean+ of whether the contributor is a contributor or not205def self.person?(site, c)206site.data['contributors'].key?(c)207end208209##210# Check if a specific contributor is a grant or not211# Params:212# +c+:: +String+ of contributor ID213# Returns:214# +Boolean+ of whether the contributor is a grant or not215def self.grant?(site, c)216site.data['grants'].key?(c)217end218219##220# Obtain the contributor's funding URL221# Params:222# +c+:: +String+ of contributor ID223# Returns:224# +Boolean+ of whether the contributor is a funder or not225def self.fetch_funding_url(contributor)226return contributor['funding_id'] if !contributor.key?('funding_database')227228case contributor['funding_database']229when 'cordis'230"https://cordis.europa.eu/project/id/#{contributor['funding_id']}"231when 'erasmusplus'232"https://erasmus-plus.ec.europa.eu/projects/search/details/#{contributor['funding_id']}"233when 'ukri'234"https://gtr.ukri.org/projects?ref=#{contributor['funding_id']}"235when 'highergov'236"https://www.highergov.com/contract/#{contributor['funding_id']}/"237when 'dfg'238"https://gepris-extern.dfg.de/gepris/projekt/#{contributor['funding_id']}?language=en"239else240Jekyll.logger.error "Unknown funding system #{contributor['funding_database']}. Please let us know so we can add support for it!"241'ERROR'242end243end244end245end246247248