Created
March 23, 2009 03:41
-
-
Save DylanFM/83416 to your computer and use it in GitHub Desktop.
Sitemap.xml rake task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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