Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add literal urls as an output option

  • Loading branch information...
commit 86024112c7bb85bdf3563e97f78ad679d9851d56 1 parent 48c8d8d
Evan Arnold earnold authored
1  README.md
View
@@ -30,6 +30,7 @@ In your sitemap config file, paths can be indexed as follows:
Sitemap::Generator.instance.load :host => "mywebsite.com" do
path :root, :priority => 1
path :faq, :priority => 0.5, :change_frequency => "weekly"
+ literal "/my_blog" #helpful for vanity urls layering search results
resources :activities, :params => { :format => "html" }
resources :articles, :objects => proc { Article.published }
end
17 lib/sitemap/generator.rb
View
@@ -31,6 +31,12 @@ def initialize
# ...
# end
#
+ # Literal paths can be added as follows:
+ #
+ # Sitemap::Generator.instance.load :host => "mywebsite.com" do
+ # literal "/some_fancy_url"
+ # end
+ #
# Simple paths can be added as follows:
#
# Sitemap::Generator.instance.load :host => "mywebsite.com" do
@@ -57,6 +63,17 @@ def load(options = {}, &block)
end
self.routes = block
end
+
+ # Adds the literal url (for consistency, starting with a "/" as in "/my_url")
+ # accepts similar options to path and resources
+ def literal(target_url, options = {})
+ search = Sitemap.configuration.search.clone.merge!(options.select { |k, v| SEARCH_ATTRIBUTES.keys.include?(k) })
+ output_host = options[:host] || host
+ self.store << {
+ :url =>"http://#{output_host}#{target_url}",
+ :search => search
+ }
+ end
# Adds the specified url or object (such as an ActiveRecord model instance).
# In either case the data is being looked up in the current application routes.
2  lib/views/fragment.xml.builder
View
@@ -3,7 +3,7 @@ xml.urlset :xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9" do
store.entries.each do |entry|
xml.url do
- xml.loc polymorphic_url(entry[:object], entry[:params])
+ xml.loc entry[:url] ? entry[:url] : polymorphic_url(entry[:object], entry[:params])
entry[:search].each do |type, value|
next if !value || value.blank?
xml.tag! SEARCH_ATTRIBUTES[type], value.to_s
15 spec/generator_spec.rb
View
@@ -19,6 +19,21 @@
doc.errors.length.must_equal 0
doc.root.name.must_equal "urlset"
end
+
+ it "should create entries based on literals" do
+ urls = ["http://someplace.com/target_url", "http://someplace.com/another_url"]
+ Sitemap::Generator.instance.load(:host => "someplace.com") do
+ literal "/target_url"
+ literal "/another_url"
+ end
+ Sitemap::Generator.instance.build!
+ doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
+ elements = doc.xpath "//url/loc"
+ elements.length.must_equal urls.length
+ elements.each_with_index do |element, i|
+ element.text.must_equal urls[i]
+ end
+ end
it "should create entries based on the route paths" do
urls = ["http://someplace.com/", "http://someplace.com/questions"]
Please sign in to comment.
Something went wrong with that request. Please try again.