mirror of
https://github.com/seigler/dash-docs
synced 2025-07-27 01:36:13 +00:00
Handle redirections in _config.yml
This commit is contained in:
parent
14e25c5a1f
commit
58ef9c6c56
28 changed files with 152 additions and 244 deletions
40
_plugins/redirects.rb
Normal file
40
_plugins/redirects.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
#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)
|
||||
#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
|
|
@ -21,15 +21,6 @@ module Jekyll
|
|||
lang=file.split('.')[0]
|
||||
locs[lang] = YAML.load_file('_translations/'+file)[lang]
|
||||
end
|
||||
#Load redirections
|
||||
redirects = {}
|
||||
rredirects = {}
|
||||
Dir.foreach('_redirects') do |file|
|
||||
next if file == '.' or file == '..'
|
||||
id = file.split('.')[0]
|
||||
redirects[id] = YAML.load_file("_redirects/" + file)
|
||||
rredirects[redirects[id]['dst']] = id
|
||||
end
|
||||
#Create destination directory if does not exists
|
||||
if !File.directory?(site.dest)
|
||||
Dir.mkdir(site.dest)
|
||||
|
@ -42,26 +33,16 @@ module Jekyll
|
|||
#Add translated pages with their alternative in each languages
|
||||
locs['en']['url'].each do |id,value|
|
||||
locs.each do |lang,value|
|
||||
#Don't add a page if their url is not translated or if they are a redirection
|
||||
#Don't add a page if their url is not translated
|
||||
next if locs[lang]['url'][id].nil? or locs[lang]['url'][id] == ''
|
||||
next if redirects.has_key?(id) and ( !redirects[id].has_key?('except') or !redirects[id]['except'].has_key?(lang) )
|
||||
sitemap.puts '<url>'
|
||||
sitemap.puts ' <loc>https://bitcoin.org/'+lang+'/'+CGI::escape(locs[lang]['url'][id])+'</loc>'
|
||||
locs.each do |altlang,value|
|
||||
altid = id
|
||||
#If there is a redirection from this page, use the destination as alternate url
|
||||
if redirects.has_key?(id) and ( !redirects[id].has_key?('except') or !redirects[id]['except'].has_key?(altlang) )
|
||||
altid = redirects[id]['dst']
|
||||
end
|
||||
#If there is a disabled redirection to this page, point to the source as alternate url
|
||||
if rredirects.has_key?(id) and redirects[rredirects[id]].has_key?('except') and redirects[rredirects[id]]['except'].has_key?(altlang)
|
||||
altid = rredirects[id]
|
||||
end
|
||||
next if locs[altlang]['url'][altid].nil? or locs[altlang]['url'][altid] == '' or altlang == lang
|
||||
next if locs[altlang]['url'][id].nil? or locs[altlang]['url'][id] == '' or altlang == lang
|
||||
sitemap.puts ' <xhtml:link'
|
||||
sitemap.puts ' rel="alternate"'
|
||||
sitemap.puts ' hreflang="'+altlang+'"'
|
||||
sitemap.puts ' href="https://bitcoin.org/'+altlang+'/'+CGI::escape(locs[altlang]['url'][altid])+'" />'
|
||||
sitemap.puts ' href="https://bitcoin.org/'+altlang+'/'+CGI::escape(locs[altlang]['url'][id])+'" />'
|
||||
end
|
||||
sitemap.puts '</url>'
|
||||
end
|
||||
|
@ -71,9 +52,7 @@ module Jekyll
|
|||
if /^[a-z]{2}(_[A-Z]{2})?$/.match(file1) and File.directory?(file1)
|
||||
Dir.foreach(file1) do |file2|
|
||||
next if !/\.html$/.match(file2)
|
||||
#Ignore static redirect pages
|
||||
data = File.read(file1+'/'+file2)
|
||||
next if !data.index('window.location.href=').nil? or !data.index('redirect:').nil?
|
||||
sitemap.puts '<url>'
|
||||
sitemap.puts ' <loc>https://bitcoin.org/'+file1+'/'+file2.gsub('.html','')+'</loc>'
|
||||
sitemap.puts '</url>'
|
||||
|
@ -81,9 +60,9 @@ module Jekyll
|
|||
end
|
||||
next if !/\.html$/.match(file1)
|
||||
next if file1 == 'index.html'
|
||||
#Ignore static redirect pages and google webmaster tools
|
||||
#Ignore google webmaster tools
|
||||
data = File.read(file1)
|
||||
next if !data.index('window.location.href=').nil? or !data.index('redirect:').nil? or !data.index('google-site-verification:').nil?
|
||||
next if !data.index('google-site-verification:').nil?
|
||||
sitemap.puts '<url>'
|
||||
sitemap.puts ' <loc>https://bitcoin.org/'+file1.gsub('.html','')+'</loc>'
|
||||
sitemap.puts '</url>'
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
#_templates. The final file name of each page is defined in
|
||||
#the url section of each translations in _translations.
|
||||
|
||||
#If a page is defined in _redirects, this plugin will
|
||||
#generate a redirection instead of using the template.
|
||||
|
||||
require 'yaml'
|
||||
require 'cgi'
|
||||
|
||||
|
@ -21,21 +18,6 @@ module Jekyll
|
|||
self.data['lang'] = lang
|
||||
end
|
||||
end
|
||||
|
||||
class PageRedirect < Page
|
||||
def initialize(site, base, lang, srcdir, src, dstdir, dst, red)
|
||||
@site = site
|
||||
@base = base
|
||||
@dir = '/'+dstdir
|
||||
@name = dst
|
||||
self.process(dst)
|
||||
self.read_yaml(File.join(base, srcdir), src)
|
||||
self.data['lang'] = lang
|
||||
self.data['redirect'] = red
|
||||
self.data['layout'] = 'redirect'
|
||||
end
|
||||
end
|
||||
|
||||
class TranslatePageGenerator < Generator
|
||||
def generate(site)
|
||||
#load translations files
|
||||
|
@ -45,19 +27,14 @@ module Jekyll
|
|||
lang = file.split('.')[0]
|
||||
locs[lang] = YAML.load_file("_translations/"+file)[lang]
|
||||
end
|
||||
#Load redirections files
|
||||
redirects = {}
|
||||
Dir.foreach('_redirects') do |file|
|
||||
next if file == '.' or file == '..'
|
||||
id = file.split('.')[0]
|
||||
redirects[id] = YAML.load_file("_redirects/" + file)
|
||||
end
|
||||
#Generate each translated page based on templates
|
||||
if !File.directory?(site.dest)
|
||||
Dir.mkdir(site.dest)
|
||||
end
|
||||
locs.each do |lang,value|
|
||||
Dir.foreach('_templates') do |file|
|
||||
next if file == '.' or file == '..'
|
||||
id = file.split('.')[0]
|
||||
next if redirects.has_key?(id) and ( !redirects[id].has_key?('except') or !redirects[id]['except'].has_key?(lang) )
|
||||
dst = locs[lang]['url'][id]
|
||||
next if dst.nil? or dst == ''
|
||||
src = file
|
||||
|
@ -65,23 +42,6 @@ module Jekyll
|
|||
site.pages << TranslatePage.new(site, site.source, lang, '_templates', src, lang, dst)
|
||||
end
|
||||
site.pages << TranslatePage.new(site, site.source, lang, '_templates', 'index.html', lang, 'index.html')
|
||||
#Generate each redirection page based on _redirects.yml
|
||||
if !File.directory?(site.dest)
|
||||
Dir.mkdir(site.dest)
|
||||
end
|
||||
redirects.each do |id,redirect|
|
||||
next if redirect.has_key?('except') and redirect['except'].has_key?(lang)
|
||||
src = redirect['dst']
|
||||
src = src + '.html'
|
||||
dst = locs[lang]['url'][id]
|
||||
next if dst.nil? or dst == ''
|
||||
dst = dst + '.html'
|
||||
red = redirect['dst']
|
||||
red = locs[lang]['url'][red]
|
||||
next if red.nil? or red == ''
|
||||
red = '/' + lang + '/' + CGI::escape(red)
|
||||
site.pages << PageRedirect.new(site, site.source, lang, '_templates', src, lang, dst, red)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue