Sha256: a760bf623942a23e5924263262a3303b84f3cc52e4e9ea28ddd96d6893272123

Contents?: true

Size: 1.48 KB

Versions: 19

Compression:

Stored size: 1.48 KB

Contents

module Nanoc::Helpers
  # @see http://nanoc.ws/doc/reference/helpers/#xmlsitemap
  module XMLSitemap
    # @option params [Array] :items
    # @option params [Proc] :rep_select
    #
    # @return [String]
    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
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
nanoc-4.5.1 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.5.0 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.4.7 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.4.6 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.4.5 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.4.4 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.4.3 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.4.2 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.4.1 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.4.0 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.8 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.7 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.6 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.5 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.4 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.3 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.2 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.1 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.3.0 lib/nanoc/helpers/xml_sitemap.rb