Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/sitemap.rb
1677 views
1
# frozen_string_literal: true
2
3
module Jekyll
4
module Generators
5
# Generate a sitemap like Jekyll::Sitemap
6
class SitemapGenerator2 < Generator
7
safe true
8
9
##
10
# Generate a sitemap.xml file
11
# We reimplement the default Jekyll sitemap generator, because we want to
12
# leverage the GTN::ModificationTimes class to obtain the last modification
13
# date of a page, in a more efficient way than the default Jekyll sitemap
14
#
15
# Params:
16
# +site+:: The +Jekyll::Site+ object
17
def generate(site)
18
if Jekyll.env == 'production'
19
_build(site)
20
else
21
Jekyll.logger.info '[GTN/Sitemap] Skipping in development mode'
22
end
23
end
24
25
def _build(site)
26
# We import later in case we don't need to bother importing in the first place.
27
require 'date'
28
require './_plugins/gtn'
29
30
Jekyll.logger.info '[GTN/Sitemap] Generating'
31
result = '<?xml version="1.0" encoding="UTF-8"?>'
32
result += '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' \
33
'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 ' \
34
'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" ' \
35
'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
36
37
subset_pages = site.pages
38
.reject { |t| t.path =~ /ipynb$/ || t.path =~ /api\/ga4gh\/trs\/v2/}
39
.reject { |t| t.data.fetch('layout', 'page') =~ /external/}
40
.reject { |t| t.data.fetch('hands_on', '') == 'external'}
41
42
subset_pages.each do |t|
43
begin
44
d = Gtn::ModificationTimes.obtain_time(t.path)
45
d.format = '%FT%T%:z'
46
formatted_date = d.to_s
47
rescue StandardError
48
d = Time.new
49
formatted_date = d.strftime('%FT%T%:z')
50
end
51
52
result += "<url><loc>#{site.config['url'] + site.config['baseurl'] + t.url}</loc>" \
53
"<lastmod>#{formatted_date}</lastmod></url>"
54
end
55
result += '</urlset>'
56
57
page2 = PageWithoutAFile.new(site, '', '.', 'sitemap.xml')
58
page2.content = result
59
site.pages << page2
60
end
61
end
62
end
63
end
64
65