Module: Nanoc::Helpers::XMLSitemap

Defined in:
lib/nanoc/helpers/xml_sitemap.rb

Overview

Contains functionality for building XML sitemaps that will be crawled by search engines. See the Sitemaps protocol site for details.

Instance Method Summary collapse

Instance Method Details

#xml_sitemap(params = {}) ⇒ String

Builds an XML sitemap and returns it.

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

  • changefreq — The estimated change frequency as defined by the Sitemaps protocol

  • priority — 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 — 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”.

Examples:

Excluding binary items from the sitemap


<%= xml_sitemap :items => @items.reject{ |i| i[:is_hidden] || i.binary? } %>

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :items (Array)

    A list of items to include in the sitemap

  • :rep_select (Proc)

    A proc to filter reps through. If the proc returns true, the rep will be included; otherwise, it will not.

Returns:

  • (String)

    The XML sitemap



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
66
67
68
69
70
71
72
73
74
# File 'lib/nanoc/helpers/xml_sitemap.rb', line 38

def xml_sitemap(params = {})
  require 'builder'

  # Extract parameters
  items       = params.fetch(:items) { @items.reject { |i| i[:is_hidden] } }
  select_proc = params.fetch(:rep_select, nil)

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

  # Check for required attributes
  if @config[:base_url].nil?
    raise 'The Nanoc::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.sitemaps.org/schemas/sitemap/0.9') do
    # Add item
    items.sort_by(&:identifier).each do |item|
      reps = item.reps.reject { |r| r.raw_path.nil? }
      reps.reject! { |r| !select_proc[r] } if select_proc
      reps.sort_by { |r| r.name.to_s }.each do |rep|
        xml.url do
          xml.loc @config[:base_url] + rep.path
          xml.lastmod item[:mtime].__nanoc_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