Skip to content

Commit

Permalink
Handle redirections in _config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
saivann committed Feb 19, 2014
1 parent 14e25c5 commit 58ef9c6
Show file tree
Hide file tree
Showing 28 changed files with 152 additions and 244 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Installing dependencies on older Ubuntu and Debian distributions

### Import translations

**Update translations**: You can overwrite each language files in _translations by their updated version from Transifex. You should make sure that each .html files (in _layouts, _templates and _redirects) don't serve outdated content for those languages. You should also make sure that no url has been changed by translators.
**Update translations**: You can overwrite each language files in _translations by their updated version from Transifex. You should make sure that each .html files (in _layouts, _templates) don't serve outdated content for those languages. You should also make sure that no url has been changed by translators. If one page has been replaced or moved, a redirection can be added in _config.yml.

**Add a new language**: You can put the language file from Transifex in _translations and add the language in _config.yml in the right display order for the language bar. Make sure to review all pages and check all links.

Expand Down
33 changes: 27 additions & 6 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ langs:
zh_CN: ‪简体中文‬
zh_TW: ‪繁體中文‬

redirects:
/news: /en/version-history
/clients: /en/choose-your-wallet
/about: /en/faq
/de/ueber: /de/faq
/en/about: /en/faq
/es/acerca-de: /es/faq
/fr/a-propos: /fr/faq
/ru/описание: /ru/faq
/ar/بت-كوين-للهواة: /ar/innovation
/de/bitcoin-fuer-enthusiasten: /de/innovation
/en/bitcoin-for-enthusiasts: /en/innovation
/es/bitcoin-para-entusiastas: /es/innovacion
/fa/بیت-کوین-برای-علاقه-مندان: /fa/innovation
/fr/bitcoin-pour-passionnes: /fr/innovation
/it/bitcoin-per-appassionati: /it/innovazione
/nl/bitcoin-voor-liefhebbers: /nl/innovatie
/pl/bitcoin-dla-entuzjastow: /pl/innowacje
/ru/биткоин-энтузиастам: /ru/innovation
/en/bitcoin-for-press: /en/press

aliases:
s_nakamoto: Satoshi Nakamoto
--author=Satoshi Nakamoto: Satoshi Nakamoto
gavinandresen: Gavin Andresen
tcatm: Nils Schneider

safe: false
auto: false
server: false
Expand Down Expand Up @@ -72,9 +99,3 @@ kramdown:
coderay_tab_width: 4
coderay_bold_every: 10
coderay_css: style

aliases:
s_nakamoto: Satoshi Nakamoto
--author=Satoshi Nakamoto: Satoshi Nakamoto
gavinandresen: Gavin Andresen
tcatm: Nils Schneider
25 changes: 1 addition & 24 deletions _layouts/base.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
---
menufor:
- id: bitcoin-for-individuals
- id: bitcoin-for-businesses
- id: bitcoin-for-developers
- id: bitcoin-for-enthusiasts
menu:
- id: how-it-works
- id: vocabulary
- id: resources
- id: community
- id: development
- id: about
---
<!DOCTYPE HTML>
<html lang="{{ page.lang }}">
<head>
Expand Down Expand Up @@ -68,10 +54,7 @@
<li{% if page.id == 'bitcoin-for-businesses' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate bitcoin-for-businesses url %}">{% translate menu-bitcoin-for-businesses layout %}</a></li>
<li{% if page.id == 'bitcoin-for-developers' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate bitcoin-for-developers url %}">{% translate menu-bitcoin-for-developers layout %}</a></li>
{% case page.lang %}
{% when 'ar' or 'fa' %}
<li{% if page.id == 'bitcoin-for-enthusiasts' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate bitcoin-for-enthusiasts url %}">{% translate menu-bitcoin-for-enthusiasts layout %}</a></li>
<li{% if page.id == 'choose-your-wallet' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate choose-your-wallet url %}">{% translate menu-choose-your-wallet layout %}</a></li>
{% when 'pl' or 'zh_TW' %}
{% when 'ar' or 'fa' or 'pl' or 'zh_TW' %}
<li{% if page.id == 'choose-your-wallet' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate choose-your-wallet url %}">{% translate menu-choose-your-wallet layout %}</a></li>
{% else %}
<li{% if page.id == 'getting-started' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate getting-started url %}">{% translate menu-getting-started layout %}</a></li>
Expand All @@ -94,15 +77,9 @@
{% if page.lang == 'en' %}<li{% if page.id == 'press' %} class="active"{% endif %}><a href="/en/press">Press</a></li>{% endif %}
</ul>
</li>
{% case page.lang %}
{% when 'ar' or 'fa' %}
{% else %}
<li{% if page.id == 'innovation' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate innovation url %}">{% translate menu-innovation layout %}</a></li>
{% endcase %}
<li{% if page.id == 'support-bitcoin' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate support-bitcoin url %}">{% translate menu-support-bitcoin layout %}</a></li>
{% case page.lang %}
{% when 'ar' or 'fa' or 'it' or 'nl' or 'pl' or 'tr' or 'zh_CN' or 'zh_TW' %}
<li{% if page.id == 'about' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate about url %}">{% translate menu-about layout %}</a></li>
{% when 'id' %}
{% else %}
<li{% if page.id == 'faq' %} class="active"{% endif %}><a href="/{{ page.lang }}/{% translate faq url %}">{% translate menu-faq layout %}</a></li>
Expand Down
40 changes: 40 additions & 0 deletions _plugins/redirects.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 5 additions & 26 deletions _plugins/sitemap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ def generate(site)
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)
Expand All @@ -42,26 +33,16 @@ def generate(site)
#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
Expand All @@ -71,19 +52,17 @@ def generate(site)
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>'
end
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>'
Expand Down
46 changes: 3 additions & 43 deletions _plugins/templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -21,21 +18,6 @@ def initialize(site, base, lang, srcdir, src, dstdir, dst)
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
Expand All @@ -45,43 +27,21 @@ def generate(site)
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
dst = dst+'.html'
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
Expand Down
11 changes: 0 additions & 11 deletions _redirects/about.yml

This file was deleted.

4 changes: 0 additions & 4 deletions _redirects/bitcoin-for-enthusiasts.yml

This file was deleted.

40 changes: 0 additions & 40 deletions _templates/about.html

This file was deleted.

27 changes: 0 additions & 27 deletions _templates/bitcoin-for-enthusiasts.html

This file was deleted.

Loading

0 comments on commit 58ef9c6

Please sign in to comment.