Permalink
Browse files

first commit, can generate sitemap.xml from sitemap.rb

  • Loading branch information...
0 parents commit 40a35df8384bd97734ec54c7a5661ce87c269cc6 @flyerhzm committed Aug 8, 2009
Showing with 194 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +35 −0 README
  3. +23 −0 Rakefile
  4. +1 −0 init.rb
  5. +1 −0 install.rb
  6. +91 −0 lib/sitemap.rb
  7. +11 −0 tasks/sitemap_tasks.rake
  8. +8 −0 test/sitemap_test.rb
  9. +3 −0 test/test_helper.rb
  10. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Richard Huang(flyerhzm@gmail.com)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 README
@@ -0,0 +1,35 @@
+Sitemap
+=======
+
+This plugin will generate a sitemap.xml from sitemap.rb whose format likes routes.rb
+
+
+Install
+=======
+
+1. install dependency gem builder
+ sudo gem install builder
+
+2. install sitemap plugin
+ script/plugin install git://github.com/flyerhzm/sitemap.git
+
+
+Example
+=======
+
+1. Define config/sitemap.rb
+
+ Sitemap::Routes.host = 'http://www.sitemap.com'
+
+ Sitemap::Routes.draw do |map|
+ # The format is the same as config/routes.rb.
+ # Just parse resources now, TODO others
+ map.resources :posts, :collection => {:all => :get}, :member => {:display => :get}, :has_many => :comments
+ end
+
+2. run rake sitemap:generate, the sitemap.xml will be generated under public directory
+
+
+
+
+Copyright (c) 2009 Richard Huang (flyerhzm@gmail.com), released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the sitemap plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the sitemap plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Sitemap'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+# Include hook code here
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,91 @@
+require 'active_support'
+
+module Sitemap
+ class Routes
+ class << self
+ include ActiveSupport::Inflector
+ end
+
+ def self.draw
+ yield Mapper.new(self)
+ end
+
+ def self.named_routes
+ {}
+ end
+
+ def self.host=(host)
+ @@host = host
+ end
+
+ def self.add_route(path, options)
+ @@routes ||= []
+ @@routes << {:path => path, :options => options}
+ end
+
+ def self.generate(sitemap_result_file)
+ @@paths = []
+ @@routes.each do |route|
+ parse_path(route[:path], '', nil)
+ end
+
+ File.open(sitemap_result_file, 'w') do |file|
+ xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
+ xml.instruct!
+ xml.urlset(:xmlns => "http://www.sitemaps.org/schema/sitemap/0.9") do
+ @@paths.each do |path|
+ xml.url do
+ xml.loc (@@host + path)
+ end
+ end
+ end
+ end
+ end
+
+ def self.parse_path(path, prefix, parent)
+ items = path.split('/')
+ if items[2].nil?
+ @@paths << prefix + path
+ elsif items[2].start_with?(':')
+ objects = parent.nil? ? Object.const_get(items[1].singularize.camelize).all : parent.send(items[1])
+ objects.each do |obj|
+ if items.size > 3
+ parse_path('/' + items[3..-1].join('/'), "#{prefix}/#{items[1]}/#{obj.to_param}", obj)
+ else
+ @@paths << "#{prefix}/#{items[1]}/#{obj.to_param}"
+ end
+ end
+ return nil
+ else
+ if items.size > 2
+ parse_path('/' + items[2..-1].join('/'), "#{prefix}/#{items[1]}", nil)
+ else
+ @@paths << prefix + path
+ end
+ end
+ end
+ end
+
+ class Mapper
+ include ActionController::Resources
+
+ def initialize(set)
+ @set = set
+ end
+
+ def connect(path, options = {})
+ add_route(path, options)
+ end
+
+ def named_route(name, path, options = {})
+ path.gsub!('.:format', '')
+ if options[:conditions][:method] == :get and !['new', 'create', 'edit', 'update', 'destroy'].include?(options[:action].to_s)
+ add_route(path, options)
+ end
+ end
+
+ def add_route(path, options = {})
+ Sitemap::Routes.add_route(path, options)
+ end
+ end
+end
@@ -0,0 +1,11 @@
+require 'builder'
+
+namespace :sitemap do
+ desc "generate sitemap"
+ task :generate => :environment do
+ sitemap_configure_file = File.join(RAILS_ROOT, 'config/sitemap.rb')
+ sitemap_result_file = File.join(RAILS_ROOT, 'public/sitemap.xml')
+ load(sitemap_configure_file)
+ Sitemap::Routes.generate(sitemap_result_file)
+ end
+end
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class SitemapTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit 40a35df

Please sign in to comment.