Permalink
Browse files

Escape URLs in sitemaps (fixes #1052)

  • Loading branch information...
ddfreyne committed Jan 9, 2017
1 parent a617f0d commit 4d8008e8281031a4da6907e41a3483288470f4de
Showing with 29 additions and 1 deletion.
  1. +1 −0 lib/nanoc.rb
  2. +1 −1 lib/nanoc/helpers/xml_sitemap.rb
  3. +27 −0 test/helpers/test_xml_sitemap.rb
View
@@ -39,6 +39,7 @@ def self.on_windows?
require 'thread'
require 'time'
require 'yaml'
require 'uri'
require 'English'
# Load Nanoc
@@ -30,7 +30,7 @@ def xml_sitemap(params = {})
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.loc 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?
@@ -186,6 +186,33 @@ def test_sorted
end
end
def test_url_escape
if_have 'builder', 'nokogiri' do
# Create items
@items = Nanoc::Int::IdentifiableCollection.new({})
item = Nanoc::ItemWithRepsView.new(Nanoc::Int::Item.new('some content 1', {}, '/george/'), @view_context)
@items << item
create_item_rep(item.unwrap, :default, '/cool projects/проверка')
# Create sitemap item
@item = Nanoc::ItemWithRepsView.new(Nanoc::Int::Item.new('sitemap content', {}, '/sitemap/'), @view_context)
# Create site
@config = Nanoc::ConfigView.new({ base_url: 'http://example.com' }, nil)
# Build sitemap
res = xml_sitemap(items: @items)
# Check
doc = Nokogiri::XML(res)
urlsets = doc.css('> urlset')
assert_equal 1, urlsets.size
urls = urlsets.css('> url')
assert_equal 1, urls.size
assert_equal 'http://example.com/cool%20projects/%D0%BF%D1%80%D0%BE%D0%B2%D0%B5%D1%80%D0%BA%D0%B0', urls[0].css('> loc').inner_text
end
end
protected
def create_item_rep(item, name, path)

0 comments on commit 4d8008e

Please sign in to comment.