forked from bitcoin-dot-org/Bitcoin.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirects.rb
50 lines (42 loc) · 1.24 KB
/
redirects.rb
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
# This file is licensed under the MIT License (MIT) available on
# http://opensource.org/licenses/MIT.
#redirects.rb generates all redirection pages
#from _config.yml .
require 'yaml'
require 'cgi'
module Jekyll
class PageRedirect < Page
def initialize(site, base, srcdir, src, dst)
@site = site
@base = base
@dir = srcdir
@name = src
self.process(src)
self.read_yaml(File.join(base, '/'), 'index.html')
self.data['lang'] = 'en'
self.data['redirect'] = dst
self.data['layout'] = 'redirect'
end
end
class RedirectPageGenerator < Generator
def generate(site)
#Do nothing if plugin is disabled
if !ENV['ENABLED_PLUGINS'].nil? and ENV['ENABLED_PLUGINS'].index('redirects').nil?
print 'Redirects disabled' + "\n"
return
end
#Load redirections
redirects = YAML.load_file("_config.yml")['redirects']
#Generate each redirection page
if !File.directory?(site.dest)
Dir.mkdir(site.dest)
end
redirects.each do |src,dst|
srcar = src.split('/')
src = srcar.pop + '.html'
srcdir = srcar.join('/')
site.pages << PageRedirect.new(site, site.source, srcdir, src, dst)
end
end
end
end