Skip to content

Commit b09dc13

Browse files
committed
Generates a page per version.
Signed-off-by: Sam Barker <[email protected]>
1 parent 85421cd commit b09dc13

File tree

3 files changed

+109
-26
lines changed

3 files changed

+109
-26
lines changed

Gemfile.lock

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ GEM
5858
jekyll (>= 3.0.0)
5959
jekyll-feed (0.17.0)
6060
jekyll (>= 3.7, < 5.0)
61-
jekyll-redirect-from (0.16.0)
62-
jekyll (>= 3.3, < 5.0)
6361
jekyll-sass-converter (3.1.0)
6462
sass-embedded (~> 1.75)
6563
jekyll-seo-tag (2.8.0)
@@ -98,6 +96,7 @@ GEM
9896
google-protobuf (~> 4.31)
9997
sass-embedded (1.89.2-x86_64-linux-gnu)
10098
google-protobuf (~> 4.31)
99+
semver-string (1.0.0)
101100
terminal-table (3.0.2)
102101
unicode-display_width (>= 1.1.1, < 3)
103102
unicode-display_width (2.6.0)
@@ -112,10 +111,10 @@ DEPENDENCIES
112111
jekyll (~> 4.4.1)
113112
jekyll-asciidoc (~> 3.0.1)
114113
jekyll-feed (~> 0.12)
115-
jekyll-redirect-from
116114
jekyll-sass-converter (~> 3.1.0)
117115
jekyll-seo-tag (~> 2.8.0)
118116
jekyll-toc (~> 0.19.0)
117+
semver-string (~> 1)
119118

120119
BUNDLED WITH
121120
2.4.22

_data/redirects/errors.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mappings:
66

77
- name: test
88
fromVersion: 0.10.0
9+
toVersion: 0.12.0
910
path: /html/kroxylicious-proxy/#con-configuring-client-connections-proxy
1011

1112

_plugins/redirector.rb

Lines changed: 106 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,131 @@ class RedirectPageGenerator < Jekyll::Generator
44

55
def generate(site)
66
config = site.data['redirects']
7-
config.each { |redirectConfig|
8-
Jekyll.logger.info "generating redoirects for #{redirectConfig[0]}"
9-
redirectConfig[1]['mappings'].each do |mapping|
10-
Jekyll.logger.info "generating redoirect for #{redirectConfig[0]}/#{mapping['name']}"
11-
site.pages << RedirectPage.new(site, redirectConfig[0], redirectConfig[1], mapping)
7+
latest_release = Version.parse(site.data['kroxylicious']['latestRelease'])
8+
releases = known_releases(site)
9+
config.each { |redirect_config|
10+
Jekyll.logger.info "generating redirects for #{redirect_config[0]}"
11+
redirect_config[1]['mappings'].each do |mapping|
12+
to_version = Version.parse(mapping['toVersion'] ||= latest_release)
13+
from_version = Version.parse(mapping['fromVersion'] ||= latest_release)
14+
versions = releases.select { |rel| rel.between?(from_version, to_version) }
15+
versions.each { |version|
16+
mapping['version'] = version
17+
site.pages << RedirectPage.new(site, redirect_config[0], redirect_config[1], mapping)
18+
}
1219
end
13-
Jekyll.logger.info "loaded redirect pages #{redirectConfig[0]}"
20+
Jekyll.logger.info "Generated redirects #{redirect_config[0]}"
1421
}
15-
Jekyll.logger.info "finished generating redirects"
22+
end
23+
24+
private
25+
26+
def known_releases(site)
27+
releases = []
28+
site.data['release'].sort.each do |release|
29+
releases << Version.parse(release[0])
30+
end
31+
releases.sort_by { |version| [version.major, version.minor, version.patch] }
1632
end
1733
end
1834

1935
# Subclass of `Jekyll::Page` with custom method definitions.
2036
class RedirectPage < Jekyll::Page
2137
def initialize(site, group, redirectConfig, mapping)
2238

23-
@site = site # the current site instance.
24-
@base = site.source # path to the source directory.
25-
@dir = "/redirect/#{group}/" # the directory the page will reside in.
39+
@site = site # the current site instance.
40+
@base = site.source # path to the source directory.
41+
@dir = "/redirect/#{group}/#{mapping['version']}/" # the directory the page will reside in.
2642

27-
# All pages have the same filename, so define attributes straight away.
28-
@basename = mapping['name'] # filename without the extension.
29-
@ext = '.html' # the extension.
30-
@name = basename + ext # basically @basename + @ext.
31-
@layout = 'redirect.html'
43+
# All pages have the same filename, so define attributes straight away.
44+
@basename = mapping['name'] # filename without the extension.
45+
@ext = '.html' # the extension.
46+
@name = basename + ext # basically @basename + @ext.
47+
@layout = 'redirect.html'
3248

33-
@data = {
34-
'target' => redirectConfig['baseUrl'] + mapping['fromVersion'] + mapping['path'],
35-
'layout' => 'redirect'
36-
}
49+
@data = {
50+
'target' => "#{redirectConfig['baseUrl']}#{mapping['version']}#{mapping['path']}",
51+
'layout' => 'redirect'
52+
}
3753

38-
Jekyll.logger.info "generated redirect page #{@dir}#{@basename}"
54+
Jekyll.logger.info "generated redirect from #{@dir}#{@basename} to #{data['target']}"
3955
end
4056

4157
# Placeholders that are used in constructing page URL.
4258
def url_placeholders
4359
{
44-
:path => @dir,
45-
:category => @dir,
46-
:basename => basename,
60+
:path => @dir,
61+
:category => @dir,
62+
:basename => basename,
4763
:output_ext => output_ext,
4864
}
4965
end
5066
end
67+
68+
class Version
69+
include Comparable
70+
71+
# @return [Integer]
72+
attr_reader :major
73+
74+
# @return [Integer]
75+
attr_reader :minor
76+
77+
# @return [Integer]
78+
attr_reader :patch
79+
80+
def self.parse(version_string)
81+
if version_string.is_a? Version
82+
return version_string
83+
end
84+
if version_string.include? '_'
85+
parts = version_string.split('_').map { |x| x.to_i }
86+
elsif version_string.include? '.'
87+
parts = version_string.split('.').map { |x| x.to_i }
88+
else
89+
raise "Invalid version string: #{version_string}"
90+
end
91+
instance = allocate
92+
instance.send(:initialize, parts[0], parts[1], parts[2])
93+
instance
94+
end
95+
96+
# @param [Integer] major
97+
# @param [Integer] minor
98+
# @param [Integer] patch
99+
def initialize(
100+
major,
101+
minor,
102+
patch
103+
)
104+
@major = major.to_i
105+
@minor = minor.to_i
106+
@patch = patch.to_i
107+
end
108+
109+
# @return [String]
110+
def to_s
111+
"#{major}.#{minor}.#{patch}"
112+
end
113+
114+
def ==(other)
115+
to_s == other.to_s
116+
end
117+
118+
# See section #11 of https://semver.org/spec/v2.0.0.html
119+
# @return [Integer, nil] Returns -1, 0, or 1. or Nil if other is unsortable
120+
def <=>(other)
121+
if other.is_a? Version
122+
[major, minor, patch] <=> [other.major, other.minor, other.patch]
123+
else
124+
nil
125+
end
126+
end
127+
128+
def between?(min, max)
129+
if (min.is_a? Version) && (max.is_a? Version)
130+
self >= min && self <= max
131+
end
132+
end
133+
end
51134
end

0 commit comments

Comments
 (0)