lib/crawlable/sitemap.rb in crawlable-0.0.1.7 vs lib/crawlable/sitemap.rb in crawlable-0.0.1.8

- old
+ new

@@ -6,20 +6,30 @@ def define!(*args, &block) self.instance = self.new(*args, &block) end + def path + self.instance ? self.instance.path : "" + end + + def find(path, directory) + if path =~ /#{Regexp.escape(self.path)}/i + return File.join(directory, self.path) + end + end + def parse!(path) path ||= File.join(::Rails.root, 'config/sitemap.rb') eval(IO.read(path)) end def write(to, compress = false) self.instance.write(to, compress) end - def process(from, to, compress = false, &block) + def process!(from, to, compress = false, &block) parse!(from) write(to, compress) end def inspect @@ -35,38 +45,51 @@ self.instance = nil end end - attr_accessor :links, :host, :ping, :yahoo_app_id + attr_accessor :links, :sitemap_host, :ping, :yahoo_app_id, :path, :stylesheet def initialize(*args, &block) - self.host = args.shift - raise "Please define a host: 'Sitemap 'http://my-site.com' do ..." if self.host.blank? + self.sitemap_host = args.shift options = args.extract_options! options.each do |k, v| self.send(k, v) if self.respond_to?(k) end instance_eval(&block) + + raise "Please define a host: 'Sitemap 'http://my-site.com' do ..." if self.sitemap_host.blank? + end + def path(value = nil) + @path = value if value + @path ||= "/sitemap.xml" + @path + end + def yahoo_app_id(string = nil) @yahoo_app_id = string unless string.nil? @yahoo_app_id end def links @links ||= [] end - def host(*args) - @host = args unless args.empty? - @host + def sitemap_host(*args) + @sitemap_host = args unless args.empty? + @sitemap_host end + def stylesheet(value = nil) + @stylesheet = value if value + @stylesheet + end + def ping(*args) @ping = args unless args.empty? @ping end @@ -74,16 +97,16 @@ @sitemap_path = string || "public/sitemap.xml" end def link(path, *args, &block) options = args.extract_options! - options.assert_valid_keys(:priority, :changes, :updated_at, :host) + options.assert_valid_keys(:priority, :changes, :updated_at, :sitemap_host) options.reverse_merge!( :priority => 0.5, :changes => 'monthly', :updated_at => Time.now, - :host => self.host + :host => self.sitemap_host ) result = { :host => options[:host], :path => path, @@ -144,11 +167,20 @@ end end end end end - builder.to_xml + xml = builder.to_xml + + # can't add processing instructions with nokogiri + xml.gsub!("<?xml version=\"1.0\"?>") do |head| + result = head + result << "\n" + result << "<?xml-stylesheet type=\"text/xsl\" href=\"#{stylesheet}\"?>" + end if stylesheet + + xml end def write(path, compress) to = path if compress @@ -186,10 +218,10 @@ def clear @links = nil end def inspect - "<Sitemap @host='#{host.to_s}' @sitemap_path='#{sitemap_path.to_s}' @ping='#{ping.inspect}' @links='#{links.inspect}'/>" + "<Sitemap @sitemap_host='#{sitemap_host.to_s}' @sitemap_path='#{sitemap_path.to_s}' @ping='#{ping.inspect}' @links='#{links.inspect}'/>" end end end