Module: Nanoc3::Helpers::XMLSitemap

Nanoc3::Helpers::XMLSitemap contains functionality for building XML sitemaps that will be crawled by search engines. See the Sitemaps protocol web site, http://www.sitemaps.org, for details.

To activate this helper, include it, like this:

  include Nanoc3::Helpers::XMLSitemap

Public Visibility

Public Instance Method Summary

#xml_sitemap

Returns the XML sitemap as a string.

Public Instance Method Details

xml_sitemap

public xml_sitemap

Returns the XML sitemap as a string.

The following attributes can optionally be set on items to change the behaviour of the sitemap:

  • ‘changefreq’, containing the estimated change frequency as defined by the Sitemaps protocol.
  • ‘priority’, containing the item’s priority, ranging from 0.0 to 1.0, as defined by the Sitemaps protocol.

The sitemap will also include dates on which the items were updated. These are generated automatically; the way this happens depends on the used data source (the filesystem data source checks the file mtimes, for instance).

The site configuration will need to have the following attributes:

  • ‘base_url’, containing the URL to the site, without trailing slash. For example, if the site is at "http://example.com/", the base_url would be "http://example.com".
[View source]


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nanoc3/helpers/xml_sitemap.rb', line 35

def xml_sitemap
  require 'builder'

  # Create builder
  buffer = ''
  xml = Builder::XmlMarkup.new(:target => buffer, :indent => 2)

  # Check for required attributes
  if @site.config[:base_url].nil?
    raise RuntimeError.new("The Nanoc3::Helpers::XMLSitemap helper requires the site configuration to specify the base URL for the site.")
  end

  # Build sitemap
  xml.instruct!
  xml.urlset(:xmlns => 'http://www.google.com/schemas/sitemap/0.84') do
    # Add item
    @items.reject { |i| i[:is_hidden] }.each do |item|
      item.reps.reject { |r| r.raw_path.nil? }.each do |rep|
        xml.url do
          xml.loc         @site.config[:base_url] + rep.path
          xml.lastmod     item.mtime.to_iso8601_date unless item.mtime.nil?
          xml.changefreq  item[:changefreq] unless item[:changefreq].nil?
          xml.priority    item[:priority] unless item[:priority].nil?
        end
      end
    end
  end

  # Return sitemap
  buffer
end
Generated on Sunday, August 09 2009 at 01:43:13 PM by YARD 0.2.3.2 (ruby-1.8.7).