为 Octopress 创建页面重定向 Rake 任务

/

Octopress 缺乏页面重定向创建功能,自己动手创建了一个页面重定向 Rake 任务。

#  usage rake new_aff['src','dest']
desc "Begin a new aff in #{source_dir}/recommends"
task :new_aff, :src, :dest do |t, args|
  if args.src
    src = args.src
  else
    src = get_stdin("Enter a src for your aff: ")
  end
  if args.dest
    dest = args.dest
  else
    dest = get_stdin("Enter a dest for your aff: ")
  end
  raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
  mkdir_p "#{source_dir}/recommends/#{src.to_url}"
  filename = "#{source_dir}/recommends/#{src.to_url}/index.html"
  if File.exist?(filename)
    abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
  end
  puts "Creating new aff: #{filename}"
  open(filename, 'w') do |aff|
    aff.puts "---"
    aff.puts "layout: null"
    aff.puts "sitemap: false"
    aff.puts "---"
    aff.puts ""
    aff.puts "<!DOCTYPE html><html><head><script type=\"text/javascript\">window.location.replace(\"#{dest.gsub(/&/,'&amp;')}\");</script></head><body></body></html>"
  end
end

使用很简单,举例说明:

rake new_aff['The Best Spinner','http://paydotcom.net/r/95330/LiYunPeng/27055188/']

执行上述 Rake 任务将生成 source/recommends/the-best-spinner/index.html,在浏览器中键入 http://www.example.com/recommends/the-best-spinner/ 将重定向到 http://paydotcom.net/r/95330/LiYunPeng/27055188/

Comments