Skip to content

Instantly share code, notes, and snippets.

@kastner
Created December 8, 2008 03:11
Show Gist options
  • Select an option

  • Save kastner/33329 to your computer and use it in GitHub Desktop.

Select an option

Save kastner/33329 to your computer and use it in GitHub Desktop.
URL_MAPPINGS = {
"/" => "index.html",
"subscribe" => "subscribe.html",
"edition/baseball" => "edition/baseball.html",
"edition/basketball" => "edition/basketball.html",
"edition/football" => "edition/football.html",
"edition/hockey" => "edition/hockey.html"
}
desc "Make a static version of the site"
task :static do
require 'fileutils'
require 'open-uri'
static_dir = "static-#{Time.now.strftime("%Y-%m-%d")}"
begin
FileUtils.mkdir(static_dir)
rescue Errno::EEXIST
FileUtils.remove_dir(static_dir, true) && FileUtils.mkdir(static_dir)
end
FileUtils.cp_r(Dir.glob("public/*"), "#{static_dir}/")
URL_MAPPINGS.each_pair do |server_url, static_path|
static_path = "#{static_dir}/#{static_path}"
FileUtils.mkdir_p(File.dirname(static_path))
File.open(static_path, "w") do |f|
puts "Fetching #{server_url} => #{static_path}"
contents = open("http://localhost:4567/#{server_url}").read
URL_MAPPINGS.each_pair do |old, replacement|
contents.gsub!(%r|(['"])/#{old}(["'])|, "#{$1}/#{replacement}#{$2}")
end
f.print contents
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment