forked from kroxylicious/kroxylicious.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirector.rb
More file actions
139 lines (121 loc) · 4.08 KB
/
Copy pathredirector.rb
File metadata and controls
139 lines (121 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module SamplePlugin
class RedirectPageGenerator < Jekyll::Generator
safe true
def generate(site)
config = site.data['redirects']
latest_release = Version.parse(site.data['kroxylicious']['latestRelease'])
releases = known_releases(site)
config.each { |redirect_config|
Jekyll.logger.info "generating redirects for #{redirect_config[0]}"
redirect_config[1]['mappings'].each do |mapping|
to_version = Version.parse(mapping['toVersion'] ||= latest_release)
from_version = Version.parse(mapping['fromVersion'] ||= latest_release)
versions = releases.select { |rel| rel.between?(from_version, to_version) }
versions.each { |version|
mapping['version'] = version
site.pages << RedirectPage.new(site, redirect_config[0], redirect_config[1], mapping)
if version == latest_release
mapping['landing_version'] = "latest"
site.pages << RedirectPage.new(site, redirect_config[0], redirect_config[1], mapping)
end
}
end
Jekyll.logger.info "Generated redirects #{redirect_config[0]}"
}
end
private
def known_releases(site)
releases = []
site.data['release'].sort.each do |release|
releases << Version.parse(release[0])
end
releases.sort_by { |version| [version.major, version.minor, version.patch] }
end
end
# Subclass of `Jekyll::Page` with custom method definitions.
class RedirectPage < Jekyll::Page
def initialize(site, group, redirect_config, mapping)
@site = site # the current site instance.
@base = site.source # path to the source directory.
@dir = "/redirect/#{group}/#{mapping['landing_version'] || mapping['version']}/" # the directory the page will reside in.
# All pages have the same filename, so define attributes straight away.
@basename = mapping['name'] # filename without the extension.
@ext = '.html' # the extension.
@name = basename + ext # basically @basename + @ext.
@layout = 'redirect.html'
delay = redirect_config['delay'] ||= 1
@data = {
'target' => "#{redirect_config['baseUrl']}#{mapping['version']}#{mapping['path']}",
'layout' => 'redirect',
'delay' => "#{delay}",
}
Jekyll.logger.info "generated redirect from #{@dir}#{@basename} to #{data['target']}"
end
# Placeholders that are used in constructing page URL.
def url_placeholders
{
:path => @dir,
:category => @dir,
:basename => basename,
:output_ext => output_ext,
}
end
end
class Version
include Comparable
# @return [Integer]
attr_reader :major
# @return [Integer]
attr_reader :minor
# @return [Integer]
attr_reader :patch
def self.parse(version_string)
if version_string.is_a? Version
return version_string
end
if version_string.include? '_'
parts = version_string.split('_').map { |x| x.to_i }
elsif version_string.include? '.'
parts = version_string.split('.').map { |x| x.to_i }
else
raise "Invalid version string: #{version_string}"
end
instance = allocate
instance.send(:initialize, parts[0], parts[1], parts[2])
instance
end
# @param [Integer] major
# @param [Integer] minor
# @param [Integer] patch
def initialize(
major,
minor,
patch
)
@major = major.to_i
@minor = minor.to_i
@patch = patch.to_i
end
# @return [String]
def to_s
"#{major}.#{minor}.#{patch}"
end
def ==(other)
to_s == other.to_s
end
# See section #11 of https://semver.org/spec/v2.0.0.html
# @return [Integer, nil] Returns -1, 0, or 1. or Nil if other is unsortable
def <=>(other)
if other.is_a? Version
[major, minor, patch] <=> [other.major, other.minor, other.patch]
else
nil
end
end
def between?(min, max)
if (min.is_a? Version) && (max.is_a? Version)
self >= min && self <= max
end
end
end
end