Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/social.rb
1677 views
1
require './_plugins/gtn'
2
3
Jekyll::Hooks.register :site, :post_write do |site|
4
# No need to run this except in prod.
5
if Jekyll.env == 'production'
6
# Build our Images!
7
count = 0
8
social_start = Time.now
9
templates = {}
10
meta = {}
11
12
# Pre-existing templates
13
Dir.glob('assets/branding/social/*.svg').each do |file|
14
templates[File.basename(file, '.svg')] = File.read(file)
15
end
16
17
Jekyll.logger.info "[GTN/Socials] Writing social media cards, #{templates.keys} discovered"
18
# svgs = []
19
20
(site.pages + site.posts).each do |page|
21
if page.data['layout'].nil? || page.data['layout'] == ''
22
next
23
end
24
25
if ['faq-page', 'slides-plain', 'workflow-list', 'base', 'event-track', 'embed', 'none', 'page'].include? page.data['layout']
26
next
27
end
28
29
title_override = nil
30
parts = page.path.split('/')
31
if parts.include?('topics')
32
topic_id = parts[parts.index('topics') + 1]
33
topic_name = site.data[topic_id]['title'].upcase
34
else
35
topic_id = nil
36
topic_name = ""
37
end
38
mod_time = Gtn::ModificationTimes.obtain_time(page.path)
39
# Default is usually fine
40
authors = Gtn::Contributors.get_authors(page.data).map { |c| Gtn::Contributors.fetch_name(site, c) }
41
42
# Handle specific layouts.
43
case page.data['layout']
44
when 'event'
45
tpl = templates['event'].dup
46
tpl = tpl.gsub('TOPIC_NAME', 'EVENT')
47
# Event specific
48
authors = Gtn::Contributors.get_organisers(page.data).map { |c| Gtn::Contributors.fetch_name(site, c) }
49
if page.data['mode'] == 'online'
50
tpl = tpl.gsub('In-Person', 'Online')
51
end
52
if page.data['async'] == false
53
tpl = tpl.gsub('Asynchronous', 'Synchronous')
54
end
55
when 'workflow'
56
tpl = templates['workflow'].dup
57
tpl = tpl.gsub('TOPIC_NAME', site.data[page.data['workflow']['topic_id']]['title'].upcase)
58
authors = page.data['workflow']['creators'].map { |c| c['name'] }
59
60
mod_time = page.data['workflow']['modified']
61
when 'faq'
62
tpl = templates['faq'].dup
63
if page.url =~ /faqs\/gtn/
64
tpl = tpl.gsub('TOPIC_NAME', 'GTN FAQ')
65
elsif page.url =~ /faqs\/galaxy/
66
tpl = tpl.gsub('TOPIC_NAME', 'Galaxy FAQ')
67
end
68
when 'recordings'
69
tpl = templates['recording'].dup
70
authors = page.data['material']['ref'].data.fetch('recordings', [])
71
.reverse.map { |r| r.fetch('speakers', []) + r.fetch('captioners',[]) }.flatten.uniq
72
authors = authors.map { |c| Gtn::Contributors.fetch_name(site, c) }
73
74
mod_time = [
75
Gtn::ModificationTimes.obtain_time(page.path),
76
Gtn::ModificationTimes.obtain_time(page.data['material']['ref'].path)
77
].max
78
when 'tutorial_hands_on'
79
tpl = templates['tutorial'].dup
80
when 'tutorial_slides'
81
tpl = templates['slides'].dup
82
when 'introduction_slides'
83
tpl = templates['slides'].dup
84
when 'learning-pathway'
85
tpl = templates['learning-pathway'].dup
86
tpl = tpl.gsub('TOPIC_NAME', 'LP')
87
authors = page.data['editorial_board'].map { |c| Gtn::Contributors.fetch_name(site, c) }
88
when 'topic'
89
tpl = templates['topic'].dup
90
tpl = tpl.gsub('TOPIC_NAME', 'Topic')
91
topic = site.data[page.data['topic_name']]
92
title_override = topic['title']
93
authors = topic.fetch('editorial_board', []).map { |c| Gtn::Contributors.fetch_name(site, c) }
94
95
mod_time = [
96
Gtn::ModificationTimes.obtain_time(page.path),
97
Gtn::ModificationTimes.obtain_time(page.path.gsub('index.md', 'metadata.yaml')),
98
Gtn::ModificationTimes.obtain_time("metadata/#{page.data['topic_name']}.yaml"),
99
].max
100
else
101
Jekyll.logger.debug "[GTN/Socials] Skipping #{page.data['layout']} => #{page.path}"
102
next
103
end
104
105
if ! topic_name.nil?
106
tpl = tpl.gsub('TOPIC_NAME', topic_name)
107
end
108
109
tpl = tpl.gsub(/gxy.io\/GTN:....../, "gxy.io/GTN:#{page.data['short_id']}")
110
111
# Title
112
tpl = tpl
113
.gsub('TITLE2TITLE2', '')
114
.gsub('TITLE3TITLE3', '')
115
.gsub('TITLE4TITLE4', '')
116
.gsub('TITLE1TITLE1', (title_override || page.data['title'].gsub('_', ' ')).gsub('&', '&'))
117
118
# Authors
119
if authors.length > 0
120
tpl = tpl.gsub('AUTHOR1', authors[0])
121
else
122
tpl = tpl.gsub('AUTHOR1', '')
123
end
124
if authors.length > 1
125
tpl = tpl.gsub('AUTHOR2', authors[1])
126
else
127
tpl = tpl.gsub('AUTHOR2', '')
128
end
129
if authors.length > 2
130
tpl = tpl.gsub('AUTHOR3', authors[2])
131
else
132
tpl = tpl.gsub('AUTHOR3', '')
133
end
134
if authors.length > 4
135
tpl = tpl.gsub('AUTHOR4', 'et al.')
136
elsif authors.length > 3
137
tpl = tpl.gsub('AUTHOR4', authors[3])
138
else
139
tpl = tpl.gsub('AUTHOR4', '')
140
end
141
142
if page.url.end_with?('.html')
143
svg_path = page.url.sub('.html', '.svg')
144
else
145
svg_path = File.join(page.url, 'index.svg')
146
end
147
svg = File.join(site.dest, svg_path)
148
if ! File.directory?(File.dirname(svg))
149
FileUtils.mkdir_p(File.dirname(svg))
150
end
151
File.open(svg, 'w') do |f|
152
f.write(tpl)
153
end
154
# svgs << svg
155
156
meta[svg_path] = mod_time.to_i
157
158
# Convert to PNG
159
count += 1
160
end
161
Jekyll.logger.info "[GTN/Socials] Social media cards written in #{Time.now - social_start} seconds for #{count} pages"
162
163
File.open(File.join(site.dest, 'api', 'social-meta.json'), 'w') do |f|
164
f.write(meta.to_json)
165
end
166
167
# This takes on the order of 30 minutes.
168
# png_start = Time.now
169
# svgs.each do |svg|
170
# # I'm not sure about this.
171
# `magick -density 50 #{svg} #{svg.sub('.svg', '.png')}`
172
# end
173
# Jekyll.logger.info "[GTN/Socials] Social media cards convert to png in #{Time.now - png_start} seconds"
174
end
175
end
176
177