Skip to content

Commit

Permalink
Forked Nanoc::Helpers::XMLSitemap to fix the public paths for static …
Browse files Browse the repository at this point in the history
…items. See issue #50
  • Loading branch information
cdchapman committed Nov 8, 2016
1 parent 95e4ea3 commit a566028
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 6 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -330,6 +332,7 @@ PLATFORMS
DEPENDENCIES
activesupport
adsf
builder
chunky_png
compass
ghi
Expand Down
48 changes: 48 additions & 0 deletions lib/helpers/xml_sitemap.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a566028

Please sign in to comment.