Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/search.rb
1677 views
1
# frozen_string_literal: true
2
3
require 'json'
4
require 'liquid'
5
require './_plugins/colour-tags'
6
require './_plugins/jekyll-topic-filter'
7
8
module Jekyll
9
module Tags
10
11
# Class to support exporting search data as JSON
12
class DumpSearchDataTag < Liquid::Tag
13
def initialize(tag_name, text, tokens)
14
super
15
@text = text.strip
16
end
17
18
def getlist(tutorial, attr)
19
tutorial[attr] || []
20
end
21
22
##
23
# (PRODUCTION ONLY) Export a large JSON blob with records like
24
#
25
# [{type:Tutorial, topic: ..., title: ..., contributors: [..], tags: [<a href="...">tag</a>...], url: ...}
26
#
27
# Example
28
# {{ dump_search_view testing }}
29
def render(context)
30
if Jekyll.env != 'production'
31
Jekyll.logger.info '[GTN/Search] Skipping search generation in development'
32
return
33
end
34
Jekyll.logger.info '[GTN/Search]'
35
36
site = context.registers[:site]
37
topics = Gtn::TopicFilter.list_topics_h(site)
38
39
results = {}
40
topics.each do |k, topic|
41
tutorials = site.data['cache_topic_filter'][k]
42
tutorials.each do |tutorial|
43
results[tutorial['url']] = {
44
'type' => 'Tutorial',
45
'topic' => topic['title'],
46
'title' => tutorial['title'],
47
'contributors' => getlist(tutorial, 'contributors').map do |c|
48
site.data['contributors'].fetch(c, {}).fetch('name', c)
49
end.join(', '),
50
'tags' => getlist(tutorial, 'tags').map do |tag|
51
href = "#{site.baseurl}/search?query=#{tag}"
52
title = "Show all tutorials tagged #{tag}"
53
style = Gtn::HashedColours.colour_tag tag
54
%(<a class="label label-default" title="#{title}" href="#{href}" style="#{style}">#{tag}</a>)
55
end,
56
'url' => site.baseurl + tutorial['url'],
57
}
58
end
59
end
60
61
faqs = site.pages.select { |p| p.data['layout'] == 'faq' }
62
faqs.each do |resource|
63
results[resource['url']] = {
64
'type' => 'FAQ',
65
'topic' => 'FAQ',
66
'title' => resource['title'],
67
'contributors' => getlist(resource.data, 'contributors').map do |c|
68
site.data['contributors'].fetch(c, {}).fetch('name', c)
69
end.join(', '),
70
'tags' => [],
71
'url' => site.baseurl + resource['url'],
72
}
73
end
74
75
JSON.pretty_generate(results)
76
end
77
end
78
end
79
end
80
81
Liquid::Template.register_tag('dump_search_view', Jekyll::Tags::DumpSearchDataTag)
82
83