sitemap_generator 6.3.0 → 7.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +107 -61
- data/README.md +176 -171
- data/VERSION +1 -1
- data/lib/capistrano/sitemap_generator.rb +2 -0
- data/lib/sitemap_generator/adapters/active_storage_adapter.rb +31 -0
- data/lib/sitemap_generator/adapters/aws_sdk_adapter.rb +29 -13
- data/lib/sitemap_generator/adapters/file_adapter.rb +12 -13
- data/lib/sitemap_generator/adapters/fog_adapter.rb +9 -7
- data/lib/sitemap_generator/adapters/google_storage_adapter.rb +8 -5
- data/lib/sitemap_generator/adapters/s3_adapter.rb +23 -18
- data/lib/sitemap_generator/adapters/wave_adapter.rb +8 -6
- data/lib/sitemap_generator/application.rb +9 -3
- data/lib/sitemap_generator/builder/sitemap_file.rb +41 -31
- data/lib/sitemap_generator/builder/sitemap_index_file.rb +31 -23
- data/lib/sitemap_generator/builder/sitemap_index_url.rb +10 -9
- data/lib/sitemap_generator/builder/sitemap_url.rb +130 -80
- data/lib/sitemap_generator/builder.rb +7 -3
- data/lib/sitemap_generator/core_ext/big_decimal.rb +40 -33
- data/lib/sitemap_generator/core_ext/numeric.rb +52 -46
- data/lib/sitemap_generator/core_ext.rb +2 -0
- data/lib/sitemap_generator/helpers/number_helper.rb +84 -76
- data/lib/sitemap_generator/interpreter.rb +22 -18
- data/lib/sitemap_generator/link_set.rb +114 -113
- data/lib/sitemap_generator/railtie.rb +4 -1
- data/lib/sitemap_generator/simple_namer.rb +8 -6
- data/lib/sitemap_generator/sitemap_location.rb +52 -40
- data/lib/sitemap_generator/tasks.rb +16 -26
- data/lib/sitemap_generator/templates.rb +11 -9
- data/lib/sitemap_generator/utilities.rb +46 -24
- data/lib/sitemap_generator.rb +42 -27
- data/lib/tasks/sitemap_generator_tasks.rake +2 -0
- data/rails/install.rb +2 -0
- data/rails/uninstall.rb +2 -0
- data/templates/sitemap.rb +3 -1
- metadata +7 -131
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'builder'
|
|
2
4
|
|
|
3
5
|
# A LinkSet provisions a bunch of links to sitemap files. It also writes the index file
|
|
4
6
|
# which lists all the sitemap files written.
|
|
5
7
|
module SitemapGenerator
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
# Top-level configuration and orchestration object for a sitemap generation run.
|
|
9
|
+
# Owns the adapter, host, paths, and namer; manages the lifecycle of sitemap files
|
|
10
|
+
# from creation through finalization. See +SitemapGenerator::Sitemap+ for the public API.
|
|
11
|
+
class LinkSet # rubocop:disable Metrics/ClassLength
|
|
12
|
+
REQUIRES_FINALIZATION_OPTS = %i[filename sitemaps_path sitemaps_host namer].freeze
|
|
13
|
+
NEW_LOCATION_OPTS = %i[filename sitemaps_path namer].freeze
|
|
9
14
|
|
|
10
15
|
attr_reader :default_host, :sitemaps_path, :filename, :create_index
|
|
11
16
|
attr_accessor :include_root, :include_index, :adapter, :yield_sitemap, :max_sitemap_links
|
|
@@ -30,17 +35,17 @@ module SitemapGenerator
|
|
|
30
35
|
# If you are calling +create+ more than once in your sitemap configuration file,
|
|
31
36
|
# make sure that you set a different +sitemaps_path+ or +filename+ for each call otherwise
|
|
32
37
|
# the sitemaps may be overwritten.
|
|
33
|
-
def create(opts={}, &block)
|
|
38
|
+
def create(opts = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
|
34
39
|
reset!
|
|
35
40
|
set_options(opts)
|
|
36
41
|
if verbose
|
|
37
42
|
start_time = Time.now
|
|
38
43
|
puts "In '#{sitemap_index.location.public_path}':"
|
|
39
44
|
end
|
|
40
|
-
interpreter.eval(:
|
|
41
|
-
finalize!
|
|
45
|
+
interpreter.eval(yield_sitemap: yield_sitemap?, &block)
|
|
46
|
+
finalize! if block_given?
|
|
42
47
|
end_time = Time.now if verbose
|
|
43
|
-
output(sitemap_index.stats_summary(:
|
|
48
|
+
output(sitemap_index.stats_summary(time_taken: end_time - start_time)) if verbose
|
|
44
49
|
self
|
|
45
50
|
end
|
|
46
51
|
|
|
@@ -117,27 +122,26 @@ module SitemapGenerator
|
|
|
117
122
|
#
|
|
118
123
|
# Note: When adding a new option be sure to include it in `options_for_group()` if
|
|
119
124
|
# the option should be inherited by groups.
|
|
120
|
-
def initialize(options={})
|
|
121
|
-
@default_host, @sitemaps_host, @yield_sitemap, @sitemaps_path,
|
|
125
|
+
def initialize(options = {}) # rubocop:disable Metrics/MethodLength
|
|
126
|
+
@default_host, @sitemaps_host, @yield_sitemap, @sitemaps_path,
|
|
127
|
+
@adapter, @verbose, @protect_index, @sitemap_index,
|
|
128
|
+
@added_default_links, @created_group, @sitemap = nil
|
|
122
129
|
|
|
123
130
|
options = SitemapGenerator::Utilities.reverse_merge(options,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
:max_sitemap_links => SitemapGenerator::MAX_SITEMAP_LINKS
|
|
133
|
-
)
|
|
134
|
-
options.each_pair { |k, v| instance_variable_set("@#{k}".to_sym, v) }
|
|
131
|
+
include_root: true,
|
|
132
|
+
include_index: false,
|
|
133
|
+
filename: :sitemap,
|
|
134
|
+
search_engines: {},
|
|
135
|
+
create_index: :auto,
|
|
136
|
+
compress: true,
|
|
137
|
+
max_sitemap_links: SitemapGenerator::MAX_SITEMAP_LINKS)
|
|
138
|
+
options.each_pair { |k, v| instance_variable_set(:"@#{k}", v) }
|
|
135
139
|
|
|
136
140
|
# If an index is passed in, protect it from modification.
|
|
137
141
|
# Sitemaps can be added to the index but nothing else can be changed.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
142
|
+
return unless options[:sitemap_index]
|
|
143
|
+
|
|
144
|
+
@protect_index = true
|
|
141
145
|
end
|
|
142
146
|
|
|
143
147
|
# Add a link to a Sitemap. If a new Sitemap is required, one will be created for
|
|
@@ -146,9 +150,9 @@ module SitemapGenerator
|
|
|
146
150
|
# link - string link e.g. '/merchant', '/article/1' or whatever.
|
|
147
151
|
# options - see README.
|
|
148
152
|
# host - host for the link, defaults to your <tt>default_host</tt>.
|
|
149
|
-
def add(link, options={})
|
|
150
|
-
add_default_links
|
|
151
|
-
sitemap.add(link, SitemapGenerator::Utilities.reverse_merge(options, :
|
|
153
|
+
def add(link, options = {})
|
|
154
|
+
add_default_links unless @added_default_links
|
|
155
|
+
sitemap.add(link, SitemapGenerator::Utilities.reverse_merge(options, host: @default_host))
|
|
152
156
|
rescue SitemapGenerator::SitemapFullError
|
|
153
157
|
finalize_sitemap!
|
|
154
158
|
retry
|
|
@@ -163,8 +167,8 @@ module SitemapGenerator
|
|
|
163
167
|
#
|
|
164
168
|
# The `:host` option defaults to the value of `sitemaps_host` which is the host where your
|
|
165
169
|
# sitemaps reside. If no `sitemaps_host` is set, the `default_host` is used.
|
|
166
|
-
def add_to_index(link, options={})
|
|
167
|
-
sitemap_index.add(link, SitemapGenerator::Utilities.reverse_merge(options, :
|
|
170
|
+
def add_to_index(link, options = {})
|
|
171
|
+
sitemap_index.add(link, SitemapGenerator::Utilities.reverse_merge(options, host: sitemaps_host))
|
|
168
172
|
end
|
|
169
173
|
|
|
170
174
|
# Create a new group of sitemap files.
|
|
@@ -194,15 +198,15 @@ module SitemapGenerator
|
|
|
194
198
|
# Options like <tt>:default_host</tt> can be used and it will only affect the links
|
|
195
199
|
# within the group. Links added outside of the group will revert to the previous
|
|
196
200
|
# +default_host+.
|
|
197
|
-
def group(opts={}, &block)
|
|
201
|
+
def group(opts = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
|
|
198
202
|
@created_group = true
|
|
199
203
|
original_opts = opts.dup
|
|
200
204
|
|
|
201
|
-
if (
|
|
205
|
+
if (REQUIRES_FINALIZATION_OPTS & original_opts.keys).empty?
|
|
202
206
|
# If no new filename or path is specified reuse the default sitemap file.
|
|
203
207
|
# A new location object will be set on it for the duration of the group.
|
|
204
208
|
original_opts[:sitemap] = sitemap
|
|
205
|
-
elsif original_opts.key?(:sitemaps_host) && (
|
|
209
|
+
elsif original_opts.key?(:sitemaps_host) && (NEW_LOCATION_OPTS & original_opts.keys).empty?
|
|
206
210
|
# If no location options are provided we are creating the next sitemap in the
|
|
207
211
|
# current series, so finalize and inherit the namer.
|
|
208
212
|
finalize_sitemap!
|
|
@@ -217,7 +221,7 @@ module SitemapGenerator
|
|
|
217
221
|
@original_location = @sitemap.location.dup
|
|
218
222
|
@sitemap.location.merge!(@group.sitemap_location)
|
|
219
223
|
if block_given?
|
|
220
|
-
@group.interpreter.eval(:
|
|
224
|
+
@group.interpreter.eval(yield_sitemap: @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
|
|
221
225
|
@group.finalize_sitemap!
|
|
222
226
|
@sitemap.location.merge!(@original_location)
|
|
223
227
|
end
|
|
@@ -236,7 +240,7 @@ module SitemapGenerator
|
|
|
236
240
|
@group.send(:create_index=, true, true) if @group.create_index != false
|
|
237
241
|
|
|
238
242
|
if block_given?
|
|
239
|
-
@group.interpreter.eval(:
|
|
243
|
+
@group.interpreter.eval(yield_sitemap: @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
|
|
240
244
|
@group.finalize_sitemap!
|
|
241
245
|
end
|
|
242
246
|
end
|
|
@@ -278,30 +282,26 @@ module SitemapGenerator
|
|
|
278
282
|
# Is equivalent to:
|
|
279
283
|
#
|
|
280
284
|
# SitemapGenerator::Sitemap.ping_search_engines('http://example.com/sitemap.xml.gz', :super_engine => 'http://superengine.com/ping?url=%s')
|
|
281
|
-
def ping_search_engines(*args)
|
|
282
|
-
require 'cgi/session'
|
|
285
|
+
def ping_search_engines(*args) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
|
283
286
|
require 'open-uri'
|
|
284
287
|
require 'timeout'
|
|
288
|
+
require 'uri'
|
|
285
289
|
|
|
286
290
|
engines = args.last.is_a?(Hash) ? args.pop : {}
|
|
287
291
|
unescaped_url = args.shift || sitemap_index_url
|
|
288
|
-
index_url =
|
|
292
|
+
index_url = URI.encode_www_form_component(unescaped_url)
|
|
289
293
|
|
|
290
294
|
output("\n")
|
|
291
295
|
output("Pinging with URL '#{unescaped_url}':")
|
|
292
296
|
search_engines.merge(engines).each do |engine, link|
|
|
293
|
-
link
|
|
297
|
+
link %= index_url
|
|
294
298
|
name = Utilities.titleize(engine.to_s)
|
|
295
299
|
begin
|
|
296
|
-
Timeout
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
else
|
|
300
|
-
open(link) # using Kernel#open became deprecated since Ruby 2.7. See https://bugs.ruby-lang.org/issues/15893
|
|
301
|
-
end
|
|
302
|
-
}
|
|
300
|
+
Timeout.timeout(10) do
|
|
301
|
+
URI.open(link) # rubocop:disable Security/Open
|
|
302
|
+
end
|
|
303
303
|
output(" Successful ping of #{name}")
|
|
304
|
-
rescue
|
|
304
|
+
rescue StandardError => e
|
|
305
305
|
output("Ping failed for #{name}: #{e.inspect} (URL #{link})")
|
|
306
306
|
end
|
|
307
307
|
end
|
|
@@ -366,9 +366,7 @@ module SitemapGenerator
|
|
|
366
366
|
# By default verbose is true. When running rake tasks, pass the <tt>-s</tt>
|
|
367
367
|
# option to rake to turn verbose off.
|
|
368
368
|
def verbose
|
|
369
|
-
if @verbose.nil?
|
|
370
|
-
@verbose = SitemapGenerator.verbose.nil? ? true : SitemapGenerator.verbose
|
|
371
|
-
end
|
|
369
|
+
@verbose = SitemapGenerator.verbose.nil? || SitemapGenerator.verbose if @verbose.nil?
|
|
372
370
|
@verbose
|
|
373
371
|
end
|
|
374
372
|
|
|
@@ -384,27 +382,26 @@ module SitemapGenerator
|
|
|
384
382
|
#
|
|
385
383
|
# If both `filename` and `namer` are passed, set filename first so it
|
|
386
384
|
# doesn't override the latter.
|
|
387
|
-
def set_options(opts={})
|
|
385
|
+
def set_options(opts = {})
|
|
388
386
|
opts = opts.dup
|
|
389
|
-
%w
|
|
390
|
-
if value = opts.delete(key.to_sym)
|
|
391
|
-
send("#{key}=", value)
|
|
387
|
+
%w[filename namer].each do |key|
|
|
388
|
+
if (value = opts.delete(key.to_sym))
|
|
389
|
+
send(:"#{key}=", value)
|
|
392
390
|
end
|
|
393
391
|
end
|
|
394
392
|
opts.each_pair do |key, value|
|
|
395
|
-
send("#{key}=", value)
|
|
393
|
+
send(:"#{key}=", value)
|
|
396
394
|
end
|
|
397
395
|
end
|
|
398
396
|
|
|
399
397
|
# Given +opts+, modify it and return it prepped for creating a new group from this LinkSet.
|
|
400
398
|
# If <tt>:public_path</tt> is present in +opts+ it is removed because groups cannot
|
|
401
399
|
# change the public path.
|
|
402
|
-
def options_for_group(opts)
|
|
400
|
+
def options_for_group(opts) # rubocop:disable Metrics/MethodLength
|
|
403
401
|
opts = SitemapGenerator::Utilities.reverse_merge(opts,
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
)
|
|
402
|
+
include_index: false,
|
|
403
|
+
include_root: false,
|
|
404
|
+
sitemap_index: sitemap_index)
|
|
408
405
|
opts.delete(:public_path)
|
|
409
406
|
|
|
410
407
|
# Reverse merge the current settings.
|
|
@@ -412,22 +409,21 @@ module SitemapGenerator
|
|
|
412
409
|
# This hash could be a problem because it needs to be maintained
|
|
413
410
|
# when new options are added, but can easily be missed. We really could
|
|
414
411
|
# do with a separate SitemapOptions class.
|
|
415
|
-
current_settings = [
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
].
|
|
412
|
+
current_settings = %i[
|
|
413
|
+
include_root
|
|
414
|
+
include_index
|
|
415
|
+
sitemaps_path
|
|
416
|
+
public_path
|
|
417
|
+
sitemaps_host
|
|
418
|
+
verbose
|
|
419
|
+
default_host
|
|
420
|
+
adapter
|
|
421
|
+
create_index
|
|
422
|
+
compress
|
|
423
|
+
max_sitemap_links
|
|
424
|
+
].each_with_object({}) do |key, hash|
|
|
428
425
|
value = instance_variable_get(:"@#{key}")
|
|
429
426
|
hash[key] = value unless value.nil?
|
|
430
|
-
hash
|
|
431
427
|
end
|
|
432
428
|
SitemapGenerator::Utilities.reverse_merge!(opts, current_settings)
|
|
433
429
|
opts
|
|
@@ -437,13 +433,11 @@ module SitemapGenerator
|
|
|
437
433
|
# in an instance variable.
|
|
438
434
|
def add_default_links
|
|
439
435
|
@added_default_links = true
|
|
440
|
-
link_options = { :
|
|
441
|
-
if include_root?
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
add(sitemap_index, link_options)
|
|
446
|
-
end
|
|
436
|
+
link_options = { priority: 1.0, lastmod: SitemapGenerator::Utilities.current_time }
|
|
437
|
+
add('/', link_options) if include_root?
|
|
438
|
+
return unless include_index?
|
|
439
|
+
|
|
440
|
+
add(sitemap_index, link_options)
|
|
447
441
|
end
|
|
448
442
|
|
|
449
443
|
# Finalize a sitemap by including it in the index and outputting a summary line.
|
|
@@ -457,7 +451,8 @@ module SitemapGenerator
|
|
|
457
451
|
# block passed to create() is empty the default links are still included in the
|
|
458
452
|
# sitemap.
|
|
459
453
|
def finalize_sitemap!
|
|
460
|
-
return if sitemap.finalized? || sitemap.empty? && @created_group
|
|
454
|
+
return if sitemap.finalized? || (sitemap.empty? && @created_group)
|
|
455
|
+
|
|
461
456
|
add_default_links if !@added_default_links && !@created_group
|
|
462
457
|
# This will finalize it. We add to the index even if not creating an index because
|
|
463
458
|
# the index keeps track of how many links are in our sitemaps and we need this info
|
|
@@ -470,6 +465,7 @@ module SitemapGenerator
|
|
|
470
465
|
# been finalized.
|
|
471
466
|
def finalize_sitemap_index!
|
|
472
467
|
return if @protect_index || sitemap_index.finalized?
|
|
468
|
+
|
|
473
469
|
sitemap_index.finalize!
|
|
474
470
|
sitemap_index.write
|
|
475
471
|
end
|
|
@@ -477,15 +473,15 @@ module SitemapGenerator
|
|
|
477
473
|
# Return the interpreter linked to this instance.
|
|
478
474
|
def interpreter
|
|
479
475
|
require 'sitemap_generator/interpreter'
|
|
480
|
-
@interpreter ||= SitemapGenerator::Interpreter.new(:
|
|
476
|
+
@interpreter ||= SitemapGenerator::Interpreter.new(link_set: self)
|
|
481
477
|
end
|
|
482
478
|
|
|
483
479
|
# Reset this instance. Keep the same options, but return to the same state
|
|
484
480
|
# as before any sitemaps were created.
|
|
485
481
|
def reset!
|
|
486
|
-
@sitemap_index = nil if @sitemap_index
|
|
487
|
-
@sitemap = nil if @sitemap
|
|
488
|
-
|
|
482
|
+
@sitemap_index = nil if @sitemap_index&.finalized? && !@protect_index
|
|
483
|
+
@sitemap = nil if @sitemap&.finalized?
|
|
484
|
+
namer.reset
|
|
489
485
|
@added_default_links = false
|
|
490
486
|
end
|
|
491
487
|
|
|
@@ -493,12 +489,13 @@ module SitemapGenerator
|
|
|
493
489
|
# evaluated and some info output to STDOUT in a lazy fasion.
|
|
494
490
|
def output(string)
|
|
495
491
|
return unless verbose
|
|
492
|
+
|
|
496
493
|
puts string
|
|
497
494
|
end
|
|
498
495
|
|
|
496
|
+
# Setters that propagate location configuration (host, paths, adapter) to the
|
|
497
|
+
# active sitemap and index files whenever a value changes on the LinkSet.
|
|
499
498
|
module LocationHelpers
|
|
500
|
-
public
|
|
501
|
-
|
|
502
499
|
# Set the host name, including protocol, that will be used by default on each
|
|
503
500
|
# of your sitemap links. You can pass a different host in your options to `add`
|
|
504
501
|
# if you need to change it on a per-link basis.
|
|
@@ -516,16 +513,14 @@ module SitemapGenerator
|
|
|
516
513
|
# Set to nil to use the current directory.
|
|
517
514
|
def public_path=(value)
|
|
518
515
|
@public_path = Pathname.new(SitemapGenerator::Utilities.append_slash(value))
|
|
519
|
-
if @public_path.relative?
|
|
520
|
-
@public_path = SitemapGenerator.app.root + @public_path
|
|
521
|
-
end
|
|
516
|
+
@public_path = SitemapGenerator.app.root + @public_path if @public_path.relative?
|
|
522
517
|
update_location_info(:public_path, @public_path)
|
|
523
|
-
@public_path
|
|
518
|
+
@public_path # rubocop:disable Lint/Void -- return value used by send(:public_path=) in the getter
|
|
524
519
|
end
|
|
525
520
|
|
|
526
521
|
# Return a Pathname with the full path to the public directory
|
|
527
522
|
def public_path
|
|
528
|
-
@public_path ||=
|
|
523
|
+
@public_path ||= send(:public_path=, 'public/')
|
|
529
524
|
end
|
|
530
525
|
|
|
531
526
|
# Set the sitemaps_path. This path gives the location to write sitemaps to
|
|
@@ -576,28 +571,28 @@ module SitemapGenerator
|
|
|
576
571
|
# Return a new +SitemapLocation+ instance with the current options included
|
|
577
572
|
def sitemap_location
|
|
578
573
|
SitemapGenerator::SitemapLocation.new(
|
|
579
|
-
:
|
|
580
|
-
:
|
|
581
|
-
:
|
|
582
|
-
:
|
|
583
|
-
:
|
|
584
|
-
:
|
|
585
|
-
:
|
|
586
|
-
:
|
|
574
|
+
host: sitemaps_host,
|
|
575
|
+
namer: namer,
|
|
576
|
+
public_path: public_path,
|
|
577
|
+
sitemaps_path: @sitemaps_path,
|
|
578
|
+
adapter: @adapter,
|
|
579
|
+
verbose: verbose,
|
|
580
|
+
compress: @compress,
|
|
581
|
+
max_sitemap_links: max_sitemap_links
|
|
587
582
|
)
|
|
588
583
|
end
|
|
589
584
|
|
|
590
585
|
# Return a new +SitemapIndexLocation+ instance with the current options included
|
|
591
586
|
def sitemap_index_location
|
|
592
587
|
SitemapGenerator::SitemapLocation.new(
|
|
593
|
-
:
|
|
594
|
-
:
|
|
595
|
-
:
|
|
596
|
-
:
|
|
597
|
-
:
|
|
598
|
-
:
|
|
599
|
-
:
|
|
600
|
-
:
|
|
588
|
+
host: sitemaps_host,
|
|
589
|
+
namer: namer,
|
|
590
|
+
public_path: public_path,
|
|
591
|
+
sitemaps_path: @sitemaps_path,
|
|
592
|
+
adapter: @adapter,
|
|
593
|
+
verbose: verbose,
|
|
594
|
+
create_index: @create_index,
|
|
595
|
+
compress: @compress
|
|
601
596
|
)
|
|
602
597
|
end
|
|
603
598
|
|
|
@@ -609,12 +604,15 @@ module SitemapGenerator
|
|
|
609
604
|
# are in your sitemap. If `false` an index file is never created.
|
|
610
605
|
# If `:auto` an index file is created only if your sitemap has more than
|
|
611
606
|
# one sitemap file.
|
|
612
|
-
def create_index=(value, force=false)
|
|
607
|
+
def create_index=(value, force = false) # rubocop:disable Style/OptionalBooleanParameter
|
|
613
608
|
@create_index = value
|
|
614
609
|
# Allow overriding the protected status of the index when we are creating a group.
|
|
615
610
|
# Because sometimes we need to force an index in that case. But generally we don't
|
|
616
611
|
# want to allow people to mess with this value if the index is protected.
|
|
617
|
-
|
|
612
|
+
return unless @sitemap_index && ((!@sitemap_index.finalized? && !@protect_index) || force)
|
|
613
|
+
|
|
614
|
+
@sitemap_index.location[:create_index] =
|
|
615
|
+
value
|
|
618
616
|
end
|
|
619
617
|
|
|
620
618
|
# Set the namer to use to generate the sitemap (and index) file names.
|
|
@@ -629,7 +627,7 @@ module SitemapGenerator
|
|
|
629
627
|
# the current sitemap and if there is no sitemap, creates a new one using
|
|
630
628
|
# the current filename.
|
|
631
629
|
def namer
|
|
632
|
-
@namer ||= @sitemap
|
|
630
|
+
@namer ||= @sitemap&.location&.namer || SitemapGenerator::SimpleNamer.new(@filename)
|
|
633
631
|
end
|
|
634
632
|
|
|
635
633
|
# Set the value of the compress setting.
|
|
@@ -658,9 +656,12 @@ module SitemapGenerator
|
|
|
658
656
|
|
|
659
657
|
# Update the given attribute on the current sitemap index and sitemap file location objects.
|
|
660
658
|
# But don't create the index or sitemap files yet if they are not already created.
|
|
661
|
-
def update_location_info(attribute, value, opts={})
|
|
662
|
-
opts = SitemapGenerator::Utilities.reverse_merge(opts, :
|
|
663
|
-
|
|
659
|
+
def update_location_info(attribute, value, opts = {})
|
|
660
|
+
opts = SitemapGenerator::Utilities.reverse_merge(opts, include_index: !@protect_index)
|
|
661
|
+
if opts[:include_index] && @sitemap_index && !@sitemap_index.finalized?
|
|
662
|
+
@sitemap_index.location[attribute] =
|
|
663
|
+
value
|
|
664
|
+
end
|
|
664
665
|
@sitemap.location[attribute] = value if @sitemap && !@sitemap.finalized?
|
|
665
666
|
end
|
|
666
667
|
end
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module SitemapGenerator
|
|
4
|
+
# Loads sitemap_generator rake tasks automatically when the gem is used in a Rails app.
|
|
2
5
|
class Railtie < Rails::Railtie
|
|
3
6
|
rake_tasks do
|
|
4
|
-
load
|
|
7
|
+
load 'tasks/sitemap_generator_tasks.rake'
|
|
5
8
|
end
|
|
6
9
|
end
|
|
7
10
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module SitemapGenerator
|
|
2
4
|
# A class for generating sitemap filenames.
|
|
3
5
|
#
|
|
@@ -26,12 +28,11 @@ module SitemapGenerator
|
|
|
26
28
|
# is used, and subsequent names would be 'sitemap1.xml.gz', 'sitemap2.xml.gz', etc.
|
|
27
29
|
# In these examples the `base` string is assumed to be 'sitemap'.
|
|
28
30
|
class SimpleNamer
|
|
29
|
-
def initialize(base, options={})
|
|
31
|
+
def initialize(base, options = {})
|
|
30
32
|
@options = SitemapGenerator::Utilities.reverse_merge(options,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
)
|
|
33
|
+
zero: nil, # marker for the start of the series
|
|
34
|
+
extension: '.xml.gz',
|
|
35
|
+
start: 1)
|
|
35
36
|
@base = base
|
|
36
37
|
reset
|
|
37
38
|
end
|
|
@@ -63,7 +64,8 @@ module SitemapGenerator
|
|
|
63
64
|
|
|
64
65
|
# Return this instance set to the previous name
|
|
65
66
|
def previous
|
|
66
|
-
raise NameError,
|
|
67
|
+
raise NameError, 'Already at the start of the series' if start?
|
|
68
|
+
|
|
67
69
|
if @count <= @options[:start]
|
|
68
70
|
@count = @options[:zero]
|
|
69
71
|
else
|
|
@@ -1,22 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'sitemap_generator/helpers/number_helper'
|
|
2
4
|
|
|
3
5
|
module SitemapGenerator
|
|
4
6
|
# A class for determining the exact location at which to write sitemap data.
|
|
5
7
|
# Handles reserving filenames from namers, constructing paths and sending
|
|
6
8
|
# data to the adapter to be written out.
|
|
7
|
-
class SitemapLocation < Hash
|
|
9
|
+
class SitemapLocation < Hash # rubocop:disable Metrics/ClassLength
|
|
8
10
|
include SitemapGenerator::Helpers::NumberHelper
|
|
9
11
|
|
|
10
12
|
PATH_OUTPUT_WIDTH = 47 # Character width of the path in the summary lines
|
|
11
13
|
|
|
12
|
-
[
|
|
14
|
+
%i[host adapter].each do |method|
|
|
13
15
|
define_method(method) do
|
|
14
16
|
raise SitemapGenerator::SitemapError, "No value set for #{method}" unless self[method]
|
|
17
|
+
|
|
15
18
|
self[method]
|
|
16
19
|
end
|
|
17
20
|
end
|
|
18
21
|
|
|
19
|
-
[
|
|
22
|
+
%i[public_path sitemaps_path].each do |method|
|
|
20
23
|
define_method(method) do
|
|
21
24
|
Pathname.new(SitemapGenerator::Utilities.append_slash(self[method]))
|
|
22
25
|
end
|
|
@@ -45,36 +48,35 @@ module SitemapGenerator
|
|
|
45
48
|
# stripped from the filename. If `:all_but_first`, only the `.gz` extension of the first
|
|
46
49
|
# filename is stripped off. If `true` the extensions are left unchanged.
|
|
47
50
|
# * <tt>max_sitemap_links</tt> - The maximum number of links to put in each sitemap.
|
|
48
|
-
def initialize(opts={})
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
51
|
+
def initialize(opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
52
|
+
super()
|
|
53
|
+
SitemapGenerator::Utilities.assert_valid_keys(opts, %i[
|
|
54
|
+
adapter
|
|
55
|
+
public_path
|
|
56
|
+
sitemaps_path
|
|
57
|
+
host
|
|
58
|
+
filename
|
|
59
|
+
namer
|
|
60
|
+
verbose
|
|
61
|
+
create_index
|
|
62
|
+
compress
|
|
63
|
+
max_sitemap_links
|
|
64
|
+
])
|
|
61
65
|
opts[:adapter] ||= SitemapGenerator::FileAdapter.new
|
|
62
|
-
opts[:public_path] ||= SitemapGenerator.app.root + 'public/'
|
|
66
|
+
opts[:public_path] ||= SitemapGenerator.app.root + 'public/' # rubocop:disable Style/StringConcatenation
|
|
63
67
|
# This is a bit of a hack to make the SimpleNamer act like the old SitemapNamer.
|
|
64
68
|
# It doesn't really make sense to create a default namer like this because the
|
|
65
69
|
# namer instance should be shared by the location objects of the sitemaps and
|
|
66
70
|
# sitemap index files. However, this greatly eases testing, so I'm leaving it in
|
|
67
71
|
# for now.
|
|
68
|
-
if !opts[:filename] && !opts[:namer]
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
opts[:verbose] = !!opts[:verbose]
|
|
72
|
-
self.merge!(opts)
|
|
72
|
+
opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1) if !opts[:filename] && !opts[:namer]
|
|
73
|
+
opts[:verbose] = !!opts[:verbose] # rubocop:disable Style/DoubleNegation
|
|
74
|
+
merge!(opts)
|
|
73
75
|
end
|
|
74
76
|
|
|
75
77
|
# Return a new Location instance with the given options merged in
|
|
76
|
-
def with(opts={})
|
|
77
|
-
|
|
78
|
+
def with(opts = {})
|
|
79
|
+
merge(opts)
|
|
78
80
|
end
|
|
79
81
|
|
|
80
82
|
# Full path to the directory of the file.
|
|
@@ -98,17 +100,26 @@ module SitemapGenerator
|
|
|
98
100
|
end
|
|
99
101
|
|
|
100
102
|
# Return the size of the file at <tt>path</tt>
|
|
101
|
-
def filesize
|
|
103
|
+
def filesize # rubocop:disable Naming/PredicateMethod
|
|
102
104
|
File.size?(path)
|
|
103
105
|
end
|
|
104
106
|
|
|
107
|
+
def gzip?
|
|
108
|
+
/\.gz$/.match?(filename.to_s)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def content_type
|
|
112
|
+
gzip? ? 'application/x-gzip' : 'application/xml'
|
|
113
|
+
end
|
|
114
|
+
|
|
105
115
|
# Return the filename. Raises an exception if no filename or namer is set.
|
|
106
116
|
# If using a namer once the filename has been retrieved from the namer its
|
|
107
117
|
# value is locked so that it is unaffected by further changes to the namer.
|
|
108
|
-
def filename
|
|
109
|
-
raise SitemapGenerator::SitemapError,
|
|
118
|
+
def filename # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
119
|
+
raise SitemapGenerator::SitemapError, 'No filename or namer set' unless self[:filename] || self[:namer]
|
|
120
|
+
|
|
110
121
|
unless self[:filename]
|
|
111
|
-
|
|
122
|
+
send(:[]=, :filename, +self[:namer].to_s, super: true)
|
|
112
123
|
|
|
113
124
|
# Post-process the filename for our compression settings.
|
|
114
125
|
# Strip the `.gz` from the extension if we aren't compressing this file.
|
|
@@ -116,7 +127,7 @@ module SitemapGenerator
|
|
|
116
127
|
# expected. Ultimately I should force using a namer in all circumstances.
|
|
117
128
|
# Changing the filename here will affect how the FileAdapter writes out the file.
|
|
118
129
|
if self[:compress] == false ||
|
|
119
|
-
(self[:namer]
|
|
130
|
+
(self[:namer]&.start? && self[:compress] == :all_but_first)
|
|
120
131
|
self[:filename].gsub!(/\.gz$/, '')
|
|
121
132
|
end
|
|
122
133
|
end
|
|
@@ -148,8 +159,8 @@ module SitemapGenerator
|
|
|
148
159
|
end
|
|
149
160
|
|
|
150
161
|
# If you set the filename, clear the namer and vice versa.
|
|
151
|
-
def []=(key, value, opts={})
|
|
152
|
-
|
|
162
|
+
def []=(key, value, opts = {})
|
|
163
|
+
unless opts[:super]
|
|
153
164
|
case key
|
|
154
165
|
when :namer
|
|
155
166
|
super(:filename, nil)
|
|
@@ -171,17 +182,18 @@ module SitemapGenerator
|
|
|
171
182
|
def summary(link_count)
|
|
172
183
|
filesize = number_to_human_size(self.filesize)
|
|
173
184
|
width = self.class::PATH_OUTPUT_WIDTH
|
|
174
|
-
path = SitemapGenerator::Utilities.ellipsis(
|
|
175
|
-
"+ #{(
|
|
185
|
+
path = SitemapGenerator::Utilities.ellipsis(path_in_public, width)
|
|
186
|
+
"+ #{format("%-#{width}s", path)} #{format('%10s', link_count)} links / #{format('%10s', filesize)}"
|
|
176
187
|
end
|
|
177
188
|
end
|
|
178
189
|
|
|
190
|
+
# SitemapLocation subclass for the sitemap index file.
|
|
191
|
+
# Defaults the namer to +:sitemap+ (no numeric suffix on the first name) and
|
|
192
|
+
# exposes the +create_index+ option.
|
|
179
193
|
class SitemapIndexLocation < SitemapLocation
|
|
180
|
-
def initialize(opts={})
|
|
181
|
-
if !opts[:filename] && !opts[:namer]
|
|
182
|
-
|
|
183
|
-
end
|
|
184
|
-
super(opts)
|
|
194
|
+
def initialize(opts = {})
|
|
195
|
+
opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap) if !opts[:filename] && !opts[:namer]
|
|
196
|
+
super
|
|
185
197
|
end
|
|
186
198
|
|
|
187
199
|
# Whether to create a sitemap index. Default `:auto`. See <tt>LinkSet::create_index=</tt>
|
|
@@ -197,8 +209,8 @@ module SitemapGenerator
|
|
|
197
209
|
def summary(link_count)
|
|
198
210
|
filesize = number_to_human_size(self.filesize)
|
|
199
211
|
width = self.class::PATH_OUTPUT_WIDTH - 3
|
|
200
|
-
path = SitemapGenerator::Utilities.ellipsis(
|
|
201
|
-
"+ #{(
|
|
212
|
+
path = SitemapGenerator::Utilities.ellipsis(path_in_public, width)
|
|
213
|
+
"+ #{format("%-#{width}s", path)} #{format('%10s', link_count)} sitemaps / #{format('%10s', filesize)}"
|
|
202
214
|
end
|
|
203
215
|
end
|
|
204
216
|
end
|