Sha256: 3dcaaff6dd45eabd2534a61f8474b4cb6f2b4fd94c55003e1d5be49018eb3b9a

Contents?: true

Size: 1.52 KB

Versions: 41

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Nanoc::Helpers
  # @see https://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.select(&:path)
          reps.select! { |r| select_proc[r] } if select_proc
          reps.sort_by { |r| r.name.to_s }.each do |rep|
            xml.url do
              xml.loc Addressable::URI.escape(@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

41 entries across 41 versions & 1 rubygems

Version Path
nanoc-4.11.23 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.22 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.21 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.20 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.19 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.18 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.17 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.16 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.15 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.14 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.13 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.12 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.11 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.10 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.9 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.8 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.7 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.6 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.5 lib/nanoc/helpers/xml_sitemap.rb
nanoc-4.11.4 lib/nanoc/helpers/xml_sitemap.rb