Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/gtn/hooks.rb
1677 views
1
require './_plugins/jekyll-topic-filter'
2
require 'jekyll'
3
4
5
module Gtn
6
# Parse the git repo to get some facts
7
module Hooks
8
9
##
10
# Generate the by-tool pages
11
# Params:
12
# +site+:: Jekyll site object
13
def self.by_tool(site)
14
Jekyll.logger.debug "[GTN/Hooks/by_tool] Started"
15
init_count = site.pages.size
16
start_time = Time.now
17
18
tools = Gtn::TopicFilter.list_materials_by_tool(site)
19
tools.reject!{|tool, _| tool.include?('{{')}
20
21
tools.each do |tool, tutorials|
22
# tool: e.g. `saskia-hiltemann/krona_text/krona-text`
23
24
ordered_tool_ids = tutorials['tool_id']
25
.map{|x|
26
if x[0] == x[1]
27
# TODO: collect versions of builtins.
28
[x[0], '0.0.0'] # Fake version for local only tools
29
else
30
x
31
end
32
}
33
.reject{|x| x[0] == x[1]}
34
.map{|x| [x[0], x[1], Gem::Version.new(fix_version(x[1]))]}
35
.sort_by{|x| x[2]}
36
37
# Redirect from the older, shorter IDs that have more potential for conflicts.
38
if tool.include?('/')
39
previous_id = tool.split('/')[0] + '/' + tool.split('/')[2]
40
else
41
previous_id = tool # No change
42
end
43
44
page2 = Jekyll::PageWithoutAFile.new(site, '', 'by-tool/', "#{tool.gsub('%20', ' ')}.html")
45
page2.content = nil
46
page2.data['layout'] = 'by_tool'
47
page2.data['short_tool'] = tool
48
page2.data['observed_tool_ids'] = ordered_tool_ids.map{|x| x[0..1]}.reverse
49
page2.data['tutorial_list'] = tutorials['tutorials']
50
page2.data['latest_tool_id'] = ordered_tool_ids.map{|x| x[0]}.last
51
# page2.data['redirect_from'] = ["/by-tool/#{previous_id.gsub('%20', ' ')}"]
52
site.pages << page2
53
54
# TODO: For whatever reason the redirect_from does NOT work, even this
55
# early in the hooks, so we're just going to write the file and call it
56
# a day. Someone should fix this someday. My apologies for leaving it like this.
57
if previous_id != tool
58
page2 = Jekyll::PageWithoutAFile.new(site, '', 'by-tool/', "#{previous_id}.html")
59
page2.content = nil
60
page2.data['layout'] = 'by_tool'
61
page2.data['short_tool'] = tool
62
page2.data['observed_tool_ids'] = ordered_tool_ids.map{|x| x[0..1]}.reverse
63
page2.data['tutorial_list'] = tutorials['tutorials']
64
page2.data['latest_tool_id'] = ordered_tool_ids.map{|x| x[0]}.last
65
site.pages << page2
66
end
67
68
end
69
Jekyll.logger.info "[GTN/Hooks/by_tool] #{site.pages.size - init_count} pages added in #{Time.now - start_time}s"
70
end
71
end
72
end
73
74