# License: GNU Affero GPL, http://www.fsf.org/licensing/licenses/agpl-3.0.html # Héctor Pérez Arenas, http://hecpeare.net require 'yaml' require 'cgi' get '/' do redirect "index.html" end get '/*.*' do vars=split_and_prepare(path) list=YAML.load(IO.read('list.yml')) case size=vars.size - 1 when 0 url="http://www.google.com/search?q=" + vars[0] when 1 #e.g. seekr.es/w/free_software -> http://en.wikipedia.org/wiki/Free_software url=list[vars[0]] url.gsub!("$1$",vars[1]) if url else #e.g. seekr.es/w/es/GNU -> http://es.wikipedia.org/wiki/GNU url=list[vars[0] + size.to_s] size.times{|n| url.gsub!("$#{n+1}$",vars[n+1])} if url end redirect url ? url.gsub("%3A",":").gsub("%2F","/") : "/try_again.html" end def replace_if_abb(p) YAML.load(IO.read('abbreviations.yml'))[p] || p end def split_and_prepare(key) #key.split(%2F) # CGI.escape("/") -> %2F last_param_is_url = key.gsub!(/http:.*/,'') ? CGI.escape($&) : nil vars=key.split("/") vars<\"([^\"]*)\"/ path.slice!(0) # remove the first character ("/") path end