Skip to content

Instantly share code, notes, and snippets.

@DylanFM
Created March 23, 2009 03:41
Show Gist options
  • Select an option

  • Save DylanFM/83416 to your computer and use it in GitHub Desktop.

Select an option

Save DylanFM/83416 to your computer and use it in GitHub Desktop.
Sitemap.xml rake task
namespace :sitemap do
desc "Builds the sitemap XML file"
task :build => :merb_env do
domain = 'jeanmarie.local'
pages = ['','about','products','contact','sitemap']
Category.all.each do |category|
pages << "products/#{category.slug}"
end
Photo.all.each do |photo|
pages << "products/#{photo.category.slug}/photos/#{photo.id}"
end
# Build URLs
urls = pages.inject([]) { |urls,page| urls << "http://#{domain}/#{page}" }
# Build XML for sitemap
builder = Nokogiri::XML::Builder.new {
urlset(:xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9') {
urls.each do |uri|
url {
loc uri
}
end
}
}
# Save XML sitemap
File.open("#{Merb.root}/public/sitemap.xml",'w') do |f|
f.puts builder.to_xml
end
puts "Recreated sitemap.xml. #{urls.size} URL(s) saved."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment