diff --git a/Gemfile b/Gemfile index df43c352..89bf833e 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,7 @@ gem 'rouge' gem 'haml' gem 'pandoc-ruby' # for benefit reports in ConTeXt gem 'git' # for version history and tagging info +gem 'builder' # for sitemap group :development, optional: true do gem 'guard' diff --git a/Gemfile.lock b/Gemfile.lock index 08c812bb..f1ec9847 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -25,7 +25,7 @@ GIT GIT remote: https://github.com/ruby-rdf/rdf.git - revision: e1ff91841066369b8917dd21be9564452a6ea18a + revision: 4740b4a52bf358656d01d93adc5174d5fe07aec8 branch: develop specs: rdf (2.1.0) @@ -40,7 +40,8 @@ GEM i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) - addressable (2.4.0) + addressable (2.5.0) + public_suffix (~> 2.0, >= 2.0.2) adsf (1.2.1) rack (>= 1.0.0) bcp47 (0.3.3) @@ -191,11 +192,12 @@ GEM pandoc-ruby (2.0.1) parallel (1.9.0) pipe-run (0.3.0) - posix-spawn (0.3.11) + posix-spawn (0.3.12) pry (0.10.4) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) + public_suffix (2.0.4) pygments.rb (0.6.3) posix-spawn (~> 0.3.6) yajl-ruby (~> 1.2.0) @@ -330,6 +332,7 @@ PLATFORMS DEPENDENCIES activesupport adsf + builder chunky_png compass ghi diff --git a/lib/helpers/xml_sitemap.rb b/lib/helpers/xml_sitemap.rb new file mode 100644 index 00000000..86809d3d --- /dev/null +++ b/lib/helpers/xml_sitemap.rb @@ -0,0 +1,48 @@ +module Nanoc::Helpers + # @see http://nanoc.ws/doc/reference/helpers/#xmlsitemap + module XMLSitemap + include LifePreserver::LinkTo + + # @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 path_to(rep, global: true) + 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