Skip to content

Commit 5cdd234

Browse files
committed
plugins/redirects: add redirect for old /developer.pebble.com/ subpaths
Signed-off-by: Ruby Iris Juric <[email protected]>
1 parent cb72418 commit 5cdd234

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

plugins/generator_redirects.rb

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,60 @@
1313
# limitations under the License.
1414

1515
module Jekyll
16-
1716
class GeneratorRedirects < Generator
18-
1917
priority :lowest
2018

21-
def initialize(config)
22-
end
19+
def initialize(config) end
2320

2421
def generate(site)
2522
@site = site
2623
site.data['redirects'].each do |redirect|
27-
if is_infinite_redirect?(redirect[0], redirect[1])
28-
Jekyll.logger.warn "Redirect Warning:", "Skipping redirect of #{redirect[0]} to #{redirect[1]}"
24+
if infinite_redirect?(redirect[0], redirect[1])
25+
Jekyll.logger.warn 'Redirect Warning:', "Skipping redirect of #{redirect[0]} to #{redirect[1]}"
2926
next
3027
end
31-
@site.pages << RedirectPage.new(@site, @site.source, File.dirname(redirect[0]), File.basename(redirect[0]), redirect[1])
28+
@site.pages <<
29+
RedirectPage.new(@site, @site.source, File.dirname(redirect[0]), File.basename(redirect[0]), redirect[1])
30+
end
31+
32+
# Generate redirects for the old URLs under the /developer.pebble.com/ subpath to new, subpath-less location
33+
site.pages.each do |page|
34+
next if page.url.start_with?('/developer.pebble.com/')
35+
next if page.url.start_with?('/assets/')
36+
37+
if page.url.end_with?('/')
38+
@site.pages <<
39+
RedirectPage.new(@site, @site.source, "/developer.pebble.com#{page.url}", 'index.html', page.url)
40+
else
41+
@site.pages <<
42+
RedirectPage.new(@site, @site.source, File.dirname("/developer.pebble.com#{page.url}"), File.basename(page.url), page.url)
43+
end
3244
end
3345
end
3446

3547
private
3648

3749
# Returns true if the redirect pair (from, to) will cause an infinite
3850
# redirect.
39-
def is_infinite_redirect?(from, to)
51+
def infinite_redirect?(from, to)
4052
return true if from == to
41-
return true if File.basename(from) == 'index.html' && File.dirname(from) == File.dirname(to + 'index.html')
53+
54+
return true if File.basename(from) == 'index.html' && File.dirname(from) == File.dirname("#{to}index.html")
55+
4256
false
4357
end
44-
4558
end
4659

4760
class RedirectPage < Page
48-
4961
def initialize(site, base, dir, name, redirect_to)
5062
@site = site
5163
@base = base
5264
@dir = dir
5365
@name = name.empty? ? 'index.html' : name
5466

55-
self.process(@name)
56-
self.read_yaml(File.join(base, '_layouts', 'utils'), 'redirect_permanent.html')
57-
self.data['redirect_to'] = redirect_to
67+
process(@name)
68+
read_yaml(File.join(base, '_layouts', 'utils'), 'redirect_permanent.html')
69+
data['redirect_to'] = redirect_to
5870
end
59-
6071
end
61-
62-
end
72+
end

0 commit comments

Comments
 (0)