sitemap_generator 6.3.0 → 7.0.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 +16 -0
- data/README.md +37 -31
- data/VERSION +1 -1
- data/lib/sitemap_generator/adapters/active_storage_adapter.rb +26 -0
- data/lib/sitemap_generator/adapters/aws_sdk_adapter.rb +5 -3
- data/lib/sitemap_generator/adapters/file_adapter.rb +2 -0
- data/lib/sitemap_generator/adapters/fog_adapter.rb +7 -5
- data/lib/sitemap_generator/adapters/google_storage_adapter.rb +3 -1
- data/lib/sitemap_generator/adapters/s3_adapter.rb +10 -6
- data/lib/sitemap_generator/adapters/wave_adapter.rb +3 -1
- data/lib/sitemap_generator/application.rb +4 -0
- data/lib/sitemap_generator/builder/sitemap_file.rb +17 -12
- data/lib/sitemap_generator/builder/sitemap_index_file.rb +12 -9
- data/lib/sitemap_generator/builder/sitemap_index_url.rb +6 -4
- data/lib/sitemap_generator/builder/sitemap_url.rb +59 -55
- data/lib/sitemap_generator/builder.rb +3 -1
- data/lib/sitemap_generator/core_ext/big_decimal.rb +2 -0
- data/lib/sitemap_generator/core_ext/numeric.rb +2 -0
- data/lib/sitemap_generator/core_ext.rb +2 -0
- data/lib/sitemap_generator/helpers/number_helper.rb +35 -33
- data/lib/sitemap_generator/interpreter.rb +6 -4
- data/lib/sitemap_generator/link_set.rb +65 -62
- data/lib/sitemap_generator/railtie.rb +3 -1
- data/lib/sitemap_generator/simple_namer.rb +8 -5
- data/lib/sitemap_generator/sitemap_location.rb +13 -9
- data/lib/sitemap_generator/tasks.rb +10 -8
- data/lib/sitemap_generator/templates.rb +8 -5
- data/lib/sitemap_generator/utilities.rb +12 -9
- data/lib/sitemap_generator.rb +13 -9
- data/templates/sitemap.rb +1 -1
- metadata +6 -130
|
@@ -1,11 +1,13 @@
|
|
|
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
8
|
class LinkSet
|
|
7
|
-
@@requires_finalization_opts = [
|
|
8
|
-
@@new_location_opts = [
|
|
9
|
+
@@requires_finalization_opts = %i[filename sitemaps_path sitemaps_host namer]
|
|
10
|
+
@@new_location_opts = %i[filename sitemaps_path namer]
|
|
9
11
|
|
|
10
12
|
attr_reader :default_host, :sitemaps_path, :filename, :create_index
|
|
11
13
|
attr_accessor :include_root, :include_index, :adapter, :yield_sitemap, :max_sitemap_links
|
|
@@ -30,17 +32,17 @@ module SitemapGenerator
|
|
|
30
32
|
# If you are calling +create+ more than once in your sitemap configuration file,
|
|
31
33
|
# make sure that you set a different +sitemaps_path+ or +filename+ for each call otherwise
|
|
32
34
|
# the sitemaps may be overwritten.
|
|
33
|
-
def create(opts={}, &block)
|
|
35
|
+
def create(opts = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
|
34
36
|
reset!
|
|
35
37
|
set_options(opts)
|
|
36
38
|
if verbose
|
|
37
39
|
start_time = Time.now
|
|
38
40
|
puts "In '#{sitemap_index.location.public_path}':"
|
|
39
41
|
end
|
|
40
|
-
interpreter.eval(:
|
|
41
|
-
finalize!
|
|
42
|
+
interpreter.eval(yield_sitemap: yield_sitemap?, &block)
|
|
43
|
+
finalize! if block_given?
|
|
42
44
|
end_time = Time.now if verbose
|
|
43
|
-
output(sitemap_index.stats_summary(:
|
|
45
|
+
output(sitemap_index.stats_summary(time_taken: end_time - start_time)) if verbose
|
|
44
46
|
self
|
|
45
47
|
end
|
|
46
48
|
|
|
@@ -117,21 +119,19 @@ module SitemapGenerator
|
|
|
117
119
|
#
|
|
118
120
|
# Note: When adding a new option be sure to include it in `options_for_group()` if
|
|
119
121
|
# the option should be inherited by groups.
|
|
120
|
-
def initialize(options={})
|
|
122
|
+
def initialize(options = {})
|
|
121
123
|
@default_host, @sitemaps_host, @yield_sitemap, @sitemaps_path, @adapter, @verbose, @protect_index, @sitemap_index, @added_default_links, @created_group, @sitemap = nil
|
|
122
124
|
|
|
123
125
|
options = SitemapGenerator::Utilities.reverse_merge(options,
|
|
124
|
-
:
|
|
125
|
-
:
|
|
126
|
-
:
|
|
127
|
-
:
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
:
|
|
131
|
-
:compress => true,
|
|
132
|
-
:max_sitemap_links => SitemapGenerator::MAX_SITEMAP_LINKS
|
|
126
|
+
include_root: true,
|
|
127
|
+
include_index: false,
|
|
128
|
+
filename: :sitemap,
|
|
129
|
+
search_engines: {},
|
|
130
|
+
create_index: :auto,
|
|
131
|
+
compress: true,
|
|
132
|
+
max_sitemap_links: SitemapGenerator::MAX_SITEMAP_LINKS
|
|
133
133
|
)
|
|
134
|
-
options.each_pair { |k, v| instance_variable_set("@#{k}"
|
|
134
|
+
options.each_pair { |k, v| instance_variable_set(:"@#{k}", v) }
|
|
135
135
|
|
|
136
136
|
# If an index is passed in, protect it from modification.
|
|
137
137
|
# Sitemaps can be added to the index but nothing else can be changed.
|
|
@@ -146,9 +146,9 @@ module SitemapGenerator
|
|
|
146
146
|
# link - string link e.g. '/merchant', '/article/1' or whatever.
|
|
147
147
|
# options - see README.
|
|
148
148
|
# 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, :
|
|
149
|
+
def add(link, options = {})
|
|
150
|
+
add_default_links unless @added_default_links
|
|
151
|
+
sitemap.add(link, SitemapGenerator::Utilities.reverse_merge(options, host: @default_host))
|
|
152
152
|
rescue SitemapGenerator::SitemapFullError
|
|
153
153
|
finalize_sitemap!
|
|
154
154
|
retry
|
|
@@ -163,8 +163,8 @@ module SitemapGenerator
|
|
|
163
163
|
#
|
|
164
164
|
# The `:host` option defaults to the value of `sitemaps_host` which is the host where your
|
|
165
165
|
# 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, :
|
|
166
|
+
def add_to_index(link, options = {})
|
|
167
|
+
sitemap_index.add(link, SitemapGenerator::Utilities.reverse_merge(options, host: sitemaps_host))
|
|
168
168
|
end
|
|
169
169
|
|
|
170
170
|
# Create a new group of sitemap files.
|
|
@@ -194,7 +194,7 @@ module SitemapGenerator
|
|
|
194
194
|
# Options like <tt>:default_host</tt> can be used and it will only affect the links
|
|
195
195
|
# within the group. Links added outside of the group will revert to the previous
|
|
196
196
|
# +default_host+.
|
|
197
|
-
def group(opts={}, &block)
|
|
197
|
+
def group(opts = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
|
|
198
198
|
@created_group = true
|
|
199
199
|
original_opts = opts.dup
|
|
200
200
|
|
|
@@ -217,7 +217,7 @@ module SitemapGenerator
|
|
|
217
217
|
@original_location = @sitemap.location.dup
|
|
218
218
|
@sitemap.location.merge!(@group.sitemap_location)
|
|
219
219
|
if block_given?
|
|
220
|
-
@group.interpreter.eval(:
|
|
220
|
+
@group.interpreter.eval(yield_sitemap: @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
|
|
221
221
|
@group.finalize_sitemap!
|
|
222
222
|
@sitemap.location.merge!(@original_location)
|
|
223
223
|
end
|
|
@@ -236,7 +236,7 @@ module SitemapGenerator
|
|
|
236
236
|
@group.send(:create_index=, true, true) if @group.create_index != false
|
|
237
237
|
|
|
238
238
|
if block_given?
|
|
239
|
-
@group.interpreter.eval(:
|
|
239
|
+
@group.interpreter.eval(yield_sitemap: @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
|
|
240
240
|
@group.finalize_sitemap!
|
|
241
241
|
end
|
|
242
242
|
end
|
|
@@ -278,28 +278,28 @@ module SitemapGenerator
|
|
|
278
278
|
# Is equivalent to:
|
|
279
279
|
#
|
|
280
280
|
# 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'
|
|
281
|
+
def ping_search_engines(*args) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
|
283
282
|
require 'open-uri'
|
|
284
283
|
require 'timeout'
|
|
284
|
+
require 'uri'
|
|
285
285
|
|
|
286
286
|
engines = args.last.is_a?(Hash) ? args.pop : {}
|
|
287
287
|
unescaped_url = args.shift || sitemap_index_url
|
|
288
|
-
index_url =
|
|
288
|
+
index_url = URI.encode_www_form_component(unescaped_url)
|
|
289
289
|
|
|
290
290
|
output("\n")
|
|
291
291
|
output("Pinging with URL '#{unescaped_url}':")
|
|
292
292
|
search_engines.merge(engines).each do |engine, link|
|
|
293
|
-
link
|
|
293
|
+
link %= index_url
|
|
294
294
|
name = Utilities.titleize(engine.to_s)
|
|
295
295
|
begin
|
|
296
|
-
Timeout
|
|
296
|
+
Timeout.timeout(10) do
|
|
297
297
|
if URI.respond_to?(:open) # Available since Ruby 2.5
|
|
298
298
|
URI.open(link)
|
|
299
299
|
else
|
|
300
300
|
open(link) # using Kernel#open became deprecated since Ruby 2.7. See https://bugs.ruby-lang.org/issues/15893
|
|
301
301
|
end
|
|
302
|
-
|
|
302
|
+
end
|
|
303
303
|
output(" Successful ping of #{name}")
|
|
304
304
|
rescue Timeout::Error, StandardError => e
|
|
305
305
|
output("Ping failed for #{name}: #{e.inspect} (URL #{link})")
|
|
@@ -384,26 +384,26 @@ module SitemapGenerator
|
|
|
384
384
|
#
|
|
385
385
|
# If both `filename` and `namer` are passed, set filename first so it
|
|
386
386
|
# doesn't override the latter.
|
|
387
|
-
def set_options(opts={})
|
|
387
|
+
def set_options(opts = {})
|
|
388
388
|
opts = opts.dup
|
|
389
|
-
%w
|
|
390
|
-
if value = opts.delete(key.to_sym)
|
|
391
|
-
send("#{key}=", value)
|
|
389
|
+
%w[filename namer].each do |key|
|
|
390
|
+
if (value = opts.delete(key.to_sym))
|
|
391
|
+
send(:"#{key}=", value)
|
|
392
392
|
end
|
|
393
393
|
end
|
|
394
394
|
opts.each_pair do |key, value|
|
|
395
|
-
send("#{key}=", value)
|
|
395
|
+
send(:"#{key}=", value)
|
|
396
396
|
end
|
|
397
397
|
end
|
|
398
398
|
|
|
399
399
|
# Given +opts+, modify it and return it prepped for creating a new group from this LinkSet.
|
|
400
400
|
# If <tt>:public_path</tt> is present in +opts+ it is removed because groups cannot
|
|
401
401
|
# change the public path.
|
|
402
|
-
def options_for_group(opts)
|
|
402
|
+
def options_for_group(opts) # rubocop:disable Metrics/MethodLength
|
|
403
403
|
opts = SitemapGenerator::Utilities.reverse_merge(opts,
|
|
404
|
-
:
|
|
405
|
-
:
|
|
406
|
-
:
|
|
404
|
+
include_index: false,
|
|
405
|
+
include_root: false,
|
|
406
|
+
sitemap_index: sitemap_index
|
|
407
407
|
)
|
|
408
408
|
opts.delete(:public_path)
|
|
409
409
|
|
|
@@ -437,7 +437,7 @@ module SitemapGenerator
|
|
|
437
437
|
# in an instance variable.
|
|
438
438
|
def add_default_links
|
|
439
439
|
@added_default_links = true
|
|
440
|
-
link_options = { :
|
|
440
|
+
link_options = { lastmod: Time.now, priority: 1.0 }
|
|
441
441
|
if include_root?
|
|
442
442
|
add('/', link_options)
|
|
443
443
|
end
|
|
@@ -457,7 +457,8 @@ module SitemapGenerator
|
|
|
457
457
|
# block passed to create() is empty the default links are still included in the
|
|
458
458
|
# sitemap.
|
|
459
459
|
def finalize_sitemap!
|
|
460
|
-
return if sitemap.finalized? || sitemap.empty? && @created_group
|
|
460
|
+
return if sitemap.finalized? || (sitemap.empty? && @created_group)
|
|
461
|
+
|
|
461
462
|
add_default_links if !@added_default_links && !@created_group
|
|
462
463
|
# This will finalize it. We add to the index even if not creating an index because
|
|
463
464
|
# the index keeps track of how many links are in our sitemaps and we need this info
|
|
@@ -470,6 +471,7 @@ module SitemapGenerator
|
|
|
470
471
|
# been finalized.
|
|
471
472
|
def finalize_sitemap_index!
|
|
472
473
|
return if @protect_index || sitemap_index.finalized?
|
|
474
|
+
|
|
473
475
|
sitemap_index.finalize!
|
|
474
476
|
sitemap_index.write
|
|
475
477
|
end
|
|
@@ -477,7 +479,7 @@ module SitemapGenerator
|
|
|
477
479
|
# Return the interpreter linked to this instance.
|
|
478
480
|
def interpreter
|
|
479
481
|
require 'sitemap_generator/interpreter'
|
|
480
|
-
@interpreter ||= SitemapGenerator::Interpreter.new(:
|
|
482
|
+
@interpreter ||= SitemapGenerator::Interpreter.new(link_set: self)
|
|
481
483
|
end
|
|
482
484
|
|
|
483
485
|
# Reset this instance. Keep the same options, but return to the same state
|
|
@@ -493,6 +495,7 @@ module SitemapGenerator
|
|
|
493
495
|
# evaluated and some info output to STDOUT in a lazy fasion.
|
|
494
496
|
def output(string)
|
|
495
497
|
return unless verbose
|
|
498
|
+
|
|
496
499
|
puts string
|
|
497
500
|
end
|
|
498
501
|
|
|
@@ -576,28 +579,28 @@ module SitemapGenerator
|
|
|
576
579
|
# Return a new +SitemapLocation+ instance with the current options included
|
|
577
580
|
def sitemap_location
|
|
578
581
|
SitemapGenerator::SitemapLocation.new(
|
|
579
|
-
:
|
|
580
|
-
:
|
|
581
|
-
:
|
|
582
|
-
:
|
|
583
|
-
:
|
|
584
|
-
:
|
|
585
|
-
:
|
|
586
|
-
:
|
|
582
|
+
host: sitemaps_host,
|
|
583
|
+
namer: namer,
|
|
584
|
+
public_path: public_path,
|
|
585
|
+
sitemaps_path: @sitemaps_path,
|
|
586
|
+
adapter: @adapter,
|
|
587
|
+
verbose: verbose,
|
|
588
|
+
compress: @compress,
|
|
589
|
+
max_sitemap_links: max_sitemap_links
|
|
587
590
|
)
|
|
588
591
|
end
|
|
589
592
|
|
|
590
593
|
# Return a new +SitemapIndexLocation+ instance with the current options included
|
|
591
594
|
def sitemap_index_location
|
|
592
595
|
SitemapGenerator::SitemapLocation.new(
|
|
593
|
-
:
|
|
594
|
-
:
|
|
595
|
-
:
|
|
596
|
-
:
|
|
597
|
-
:
|
|
598
|
-
:
|
|
599
|
-
:
|
|
600
|
-
:
|
|
596
|
+
host: sitemaps_host,
|
|
597
|
+
namer: namer,
|
|
598
|
+
public_path: public_path,
|
|
599
|
+
sitemaps_path: @sitemaps_path,
|
|
600
|
+
adapter: @adapter,
|
|
601
|
+
verbose: verbose,
|
|
602
|
+
create_index: @create_index,
|
|
603
|
+
compress: @compress
|
|
601
604
|
)
|
|
602
605
|
end
|
|
603
606
|
|
|
@@ -609,7 +612,7 @@ module SitemapGenerator
|
|
|
609
612
|
# are in your sitemap. If `false` an index file is never created.
|
|
610
613
|
# If `:auto` an index file is created only if your sitemap has more than
|
|
611
614
|
# one sitemap file.
|
|
612
|
-
def create_index=(value, force=false)
|
|
615
|
+
def create_index=(value, force = false)
|
|
613
616
|
@create_index = value
|
|
614
617
|
# Allow overriding the protected status of the index when we are creating a group.
|
|
615
618
|
# Because sometimes we need to force an index in that case. But generally we don't
|
|
@@ -658,8 +661,8 @@ module SitemapGenerator
|
|
|
658
661
|
|
|
659
662
|
# Update the given attribute on the current sitemap index and sitemap file location objects.
|
|
660
663
|
# 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, :
|
|
664
|
+
def update_location_info(attribute, value, opts = {})
|
|
665
|
+
opts = SitemapGenerator::Utilities.reverse_merge(opts, include_index: !@protect_index)
|
|
663
666
|
@sitemap_index.location[attribute] = value if opts[:include_index] && @sitemap_index && !@sitemap_index.finalized?
|
|
664
667
|
@sitemap.location[attribute] = value if @sitemap && !@sitemap.finalized?
|
|
665
668
|
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,11 +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
|
-
:
|
|
33
|
+
zero: nil, # identifies the marker for the start of the series
|
|
34
|
+
extension: '.xml.gz',
|
|
35
|
+
start: 1
|
|
34
36
|
)
|
|
35
37
|
@base = base
|
|
36
38
|
reset
|
|
@@ -63,7 +65,8 @@ module SitemapGenerator
|
|
|
63
65
|
|
|
64
66
|
# Return this instance set to the previous name
|
|
65
67
|
def previous
|
|
66
|
-
raise NameError,
|
|
68
|
+
raise NameError, 'Already at the start of the series' if start?
|
|
69
|
+
|
|
67
70
|
if @count <= @options[:start]
|
|
68
71
|
@count = @options[:zero]
|
|
69
72
|
else
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'sitemap_generator/helpers/number_helper'
|
|
2
4
|
|
|
3
5
|
module SitemapGenerator
|
|
@@ -12,6 +14,7 @@ module SitemapGenerator
|
|
|
12
14
|
[: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
|
|
@@ -45,7 +48,7 @@ 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={})
|
|
51
|
+
def initialize(opts = {})
|
|
49
52
|
SitemapGenerator::Utilities.assert_valid_keys(opts, [
|
|
50
53
|
:adapter,
|
|
51
54
|
:public_path,
|
|
@@ -66,14 +69,14 @@ module SitemapGenerator
|
|
|
66
69
|
# sitemap index files. However, this greatly eases testing, so I'm leaving it in
|
|
67
70
|
# for now.
|
|
68
71
|
if !opts[:filename] && !opts[:namer]
|
|
69
|
-
opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, :
|
|
72
|
+
opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1)
|
|
70
73
|
end
|
|
71
74
|
opts[:verbose] = !!opts[:verbose]
|
|
72
75
|
self.merge!(opts)
|
|
73
76
|
end
|
|
74
77
|
|
|
75
78
|
# Return a new Location instance with the given options merged in
|
|
76
|
-
def with(opts={})
|
|
79
|
+
def with(opts = {})
|
|
77
80
|
self.merge(opts)
|
|
78
81
|
end
|
|
79
82
|
|
|
@@ -106,9 +109,10 @@ module SitemapGenerator
|
|
|
106
109
|
# If using a namer once the filename has been retrieved from the namer its
|
|
107
110
|
# value is locked so that it is unaffected by further changes to the namer.
|
|
108
111
|
def filename
|
|
109
|
-
raise SitemapGenerator::SitemapError,
|
|
112
|
+
raise SitemapGenerator::SitemapError, 'No filename or namer set' unless self[:filename] || self[:namer]
|
|
113
|
+
|
|
110
114
|
unless self[:filename]
|
|
111
|
-
self.send(:[]=, :filename, self[:namer].to_s, :
|
|
115
|
+
self.send(:[]=, :filename, +self[:namer].to_s, super: true)
|
|
112
116
|
|
|
113
117
|
# Post-process the filename for our compression settings.
|
|
114
118
|
# Strip the `.gz` from the extension if we aren't compressing this file.
|
|
@@ -148,7 +152,7 @@ module SitemapGenerator
|
|
|
148
152
|
end
|
|
149
153
|
|
|
150
154
|
# If you set the filename, clear the namer and vice versa.
|
|
151
|
-
def []=(key, value, opts={})
|
|
155
|
+
def []=(key, value, opts = {})
|
|
152
156
|
if !opts[:super]
|
|
153
157
|
case key
|
|
154
158
|
when :namer
|
|
@@ -172,12 +176,12 @@ module SitemapGenerator
|
|
|
172
176
|
filesize = number_to_human_size(self.filesize)
|
|
173
177
|
width = self.class::PATH_OUTPUT_WIDTH
|
|
174
178
|
path = SitemapGenerator::Utilities.ellipsis(self.path_in_public, width)
|
|
175
|
-
"+ #{('%-'+width.to_s+'s') % path} #{'%10s' % link_count} links / #{'%10s' % filesize}"
|
|
179
|
+
"+ #{('%-' + width.to_s + 's') % path} #{'%10s' % link_count} links / #{'%10s' % filesize}"
|
|
176
180
|
end
|
|
177
181
|
end
|
|
178
182
|
|
|
179
183
|
class SitemapIndexLocation < SitemapLocation
|
|
180
|
-
def initialize(opts={})
|
|
184
|
+
def initialize(opts = {})
|
|
181
185
|
if !opts[:filename] && !opts[:namer]
|
|
182
186
|
opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap)
|
|
183
187
|
end
|
|
@@ -198,7 +202,7 @@ module SitemapGenerator
|
|
|
198
202
|
filesize = number_to_human_size(self.filesize)
|
|
199
203
|
width = self.class::PATH_OUTPUT_WIDTH - 3
|
|
200
204
|
path = SitemapGenerator::Utilities.ellipsis(self.path_in_public, width)
|
|
201
|
-
"+ #{('%-'+width.to_s+'s') % path} #{'%10s' % link_count} sitemaps / #{'%10s' % filesize}"
|
|
205
|
+
"+ #{('%-' + width.to_s + 's') % path} #{'%10s' % link_count} sitemaps / #{'%10s' % filesize}"
|
|
202
206
|
end
|
|
203
207
|
end
|
|
204
208
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# require this file to load the tasks
|
|
2
4
|
require 'rake'
|
|
3
5
|
|
|
@@ -28,18 +30,18 @@ namespace :sitemap do
|
|
|
28
30
|
require 'sitemap_generator'
|
|
29
31
|
end
|
|
30
32
|
|
|
31
|
-
desc
|
|
32
|
-
task :
|
|
33
|
+
desc 'Install a default config/sitemap.rb file'
|
|
34
|
+
task install: ['sitemap:require'] do
|
|
33
35
|
SitemapGenerator::Utilities.install_sitemap_rb(verbose)
|
|
34
36
|
end
|
|
35
37
|
|
|
36
|
-
desc
|
|
37
|
-
task :
|
|
38
|
+
desc 'Delete all Sitemap files in public/ directory'
|
|
39
|
+
task clean: ['sitemap:require'] do
|
|
38
40
|
SitemapGenerator::Utilities.clean_files
|
|
39
41
|
end
|
|
40
42
|
|
|
41
|
-
desc
|
|
42
|
-
task :
|
|
43
|
+
desc 'Generate sitemaps and ping search engines.'
|
|
44
|
+
task refresh: ['sitemap:create'] do
|
|
43
45
|
SitemapGenerator::Sitemap.ping_search_engines
|
|
44
46
|
end
|
|
45
47
|
|
|
@@ -47,7 +49,7 @@ namespace :sitemap do
|
|
|
47
49
|
task 'refresh:no_ping' => ['sitemap:create']
|
|
48
50
|
|
|
49
51
|
desc "Generate sitemaps but don't ping search engines. Alias for refresh:no_ping."
|
|
50
|
-
task :
|
|
51
|
-
SitemapGenerator::Interpreter.run(:
|
|
52
|
+
task create: ['sitemap:require_environment'] do
|
|
53
|
+
SitemapGenerator::Interpreter.run(config_file: ENV['CONFIG_FILE'], verbose: verbose)
|
|
52
54
|
end
|
|
53
55
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module SitemapGenerator
|
|
2
4
|
# Provide convenient access to template files. E.g.
|
|
3
5
|
#
|
|
@@ -7,17 +9,18 @@ module SitemapGenerator
|
|
|
7
9
|
# Define an accessor method for each template file.
|
|
8
10
|
class Templates
|
|
9
11
|
FILES = {
|
|
10
|
-
:
|
|
12
|
+
sitemap_sample: 'sitemap.rb',
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
# Dynamically define accessors for each key defined in <tt>FILES</tt>
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
attr_writer(*FILES.keys)
|
|
17
|
+
|
|
18
|
+
FILES.each_key do |name|
|
|
19
|
+
eval(<<-ACCESSOR, binding, __FILE__, __LINE__ + 1)
|
|
17
20
|
define_method(:#{name}) do
|
|
18
21
|
@#{name} ||= read_template(:#{name})
|
|
19
22
|
end
|
|
20
|
-
|
|
23
|
+
ACCESSOR
|
|
21
24
|
end
|
|
22
25
|
|
|
23
26
|
def initialize(root = SitemapGenerator.root)
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module SitemapGenerator
|
|
2
4
|
module Utilities
|
|
3
5
|
extend self
|
|
4
6
|
|
|
5
7
|
# Copy templates/sitemap.rb to config if not there yet.
|
|
6
|
-
def install_sitemap_rb(verbose=false)
|
|
8
|
+
def install_sitemap_rb(verbose = false)
|
|
7
9
|
if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb')
|
|
8
|
-
puts
|
|
10
|
+
puts 'already exists: config/sitemap.rb, file not copied' if verbose
|
|
9
11
|
else
|
|
10
12
|
FileUtils.cp(
|
|
11
13
|
SitemapGenerator.templates.template_path(:sitemap_sample),
|
|
12
14
|
SitemapGenerator.app.root + 'config/sitemap.rb')
|
|
13
|
-
puts
|
|
15
|
+
puts 'created: config/sitemap.rb' if verbose
|
|
14
16
|
end
|
|
15
17
|
end
|
|
16
18
|
|
|
@@ -31,7 +33,7 @@ module SitemapGenerator
|
|
|
31
33
|
# strings for keys but assert symbols as keys, this will fail.
|
|
32
34
|
def assert_valid_keys(hash, *valid_keys)
|
|
33
35
|
unknown_keys = hash.keys - [valid_keys].flatten
|
|
34
|
-
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(
|
|
36
|
+
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(', ')}") unless unknown_keys.empty?
|
|
35
37
|
end
|
|
36
38
|
|
|
37
39
|
# Return a new hash with all keys converted to symbols, as long as
|
|
@@ -69,7 +71,7 @@ module SitemapGenerator
|
|
|
69
71
|
# x.round(2) # => 1.34
|
|
70
72
|
def round(float, precision = nil)
|
|
71
73
|
if precision
|
|
72
|
-
magnitude = 10.0
|
|
74
|
+
magnitude = 10.0**precision
|
|
73
75
|
(float * magnitude).round / magnitude
|
|
74
76
|
else
|
|
75
77
|
float.round
|
|
@@ -98,7 +100,7 @@ module SitemapGenerator
|
|
|
98
100
|
# Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second.
|
|
99
101
|
# Modifies the receiver in place.
|
|
100
102
|
def reverse_merge!(hash, other_hash)
|
|
101
|
-
hash.merge!(
|
|
103
|
+
hash.merge!(other_hash) { |k, o, n| o }
|
|
102
104
|
end
|
|
103
105
|
|
|
104
106
|
# An object is blank if it's false, empty, or a whitespace string.
|
|
@@ -111,7 +113,7 @@ module SitemapGenerator
|
|
|
111
113
|
# ...to:
|
|
112
114
|
#
|
|
113
115
|
# if !address.blank?
|
|
114
|
-
def blank?(object)
|
|
116
|
+
def blank?(object) # rubocop:disable Metrics/MethodLength
|
|
115
117
|
case object
|
|
116
118
|
when NilClass, FalseClass
|
|
117
119
|
true
|
|
@@ -133,13 +135,14 @@ module SitemapGenerator
|
|
|
133
135
|
|
|
134
136
|
# Sets $VERBOSE for the duration of the block and back to its original value afterwards.
|
|
135
137
|
def with_warnings(flag)
|
|
136
|
-
old_verbose, $VERBOSE = $VERBOSE, flag
|
|
138
|
+
old_verbose, $VERBOSE = $VERBOSE, flag # rubocop:disable Style/ParallelAssignment
|
|
137
139
|
yield
|
|
138
140
|
ensure
|
|
139
141
|
$VERBOSE = old_verbose
|
|
140
142
|
end
|
|
141
143
|
|
|
142
144
|
def titleize(string)
|
|
145
|
+
string = string.dup if string.frozen?
|
|
143
146
|
string.gsub!(/_/, ' ')
|
|
144
147
|
string.split(/(\W)/).map(&:capitalize).join
|
|
145
148
|
end
|
|
@@ -156,7 +159,7 @@ module SitemapGenerator
|
|
|
156
159
|
# Returns a string. Expects a string or Pathname object.
|
|
157
160
|
def append_slash(path)
|
|
158
161
|
strpath = path.to_s
|
|
159
|
-
if strpath[-1]
|
|
162
|
+
if !strpath[-1].nil? && strpath[-1].chr != '/'
|
|
160
163
|
strpath + '/'
|
|
161
164
|
else
|
|
162
165
|
strpath
|
data/lib/sitemap_generator.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'sitemap_generator/simple_namer'
|
|
2
4
|
require 'sitemap_generator/builder'
|
|
3
5
|
require 'sitemap_generator/link_set'
|
|
@@ -9,6 +11,7 @@ require 'sitemap_generator/sitemap_location'
|
|
|
9
11
|
module SitemapGenerator
|
|
10
12
|
autoload(:Interpreter, 'sitemap_generator/interpreter')
|
|
11
13
|
autoload(:FileAdapter, 'sitemap_generator/adapters/file_adapter')
|
|
14
|
+
autoload(:ActiveStorageAdapter, 'sitemap_generator/adapters/active_storage_adapter') if defined?(::ActiveStorage)
|
|
12
15
|
autoload(:S3Adapter, 'sitemap_generator/adapters/s3_adapter')
|
|
13
16
|
autoload(:AwsSdkAdapter, 'sitemap_generator/adapters/aws_sdk_adapter')
|
|
14
17
|
autoload(:WaveAdapter, 'sitemap_generator/adapters/wave_adapter')
|
|
@@ -22,7 +25,7 @@ module SitemapGenerator
|
|
|
22
25
|
SitemapFinalizedError = Class.new(SitemapError)
|
|
23
26
|
|
|
24
27
|
Utilities.with_warnings(nil) do
|
|
25
|
-
VERSION = File.read(File.dirname(__FILE__) +
|
|
28
|
+
VERSION = File.read(File.dirname(__FILE__) + '/../VERSION').strip
|
|
26
29
|
MAX_SITEMAP_FILES = 50_000 # max sitemap links per index file
|
|
27
30
|
MAX_SITEMAP_LINKS = 50_000 # max links per sitemap
|
|
28
31
|
MAX_SITEMAP_IMAGES = 1_000 # max images per url
|
|
@@ -62,13 +65,14 @@ module SitemapGenerator
|
|
|
62
65
|
# Global default for the verbose setting.
|
|
63
66
|
def self.verbose
|
|
64
67
|
if @verbose.nil?
|
|
65
|
-
@verbose =
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
@verbose =
|
|
69
|
+
if SitemapGenerator::Utilities.truthy?(ENV['VERBOSE'])
|
|
70
|
+
true
|
|
71
|
+
elsif SitemapGenerator::Utilities.falsy?(ENV['VERBOSE'])
|
|
72
|
+
false
|
|
73
|
+
else
|
|
74
|
+
nil
|
|
75
|
+
end
|
|
72
76
|
else
|
|
73
77
|
@verbose
|
|
74
78
|
end
|
|
@@ -79,7 +83,7 @@ module SitemapGenerator
|
|
|
79
83
|
!!@yield_sitemap
|
|
80
84
|
end
|
|
81
85
|
|
|
82
|
-
self.root = File.expand_path(File.join(File.dirname(__FILE__), '../'))
|
|
86
|
+
self.root = File.expand_path(File.join(File.dirname(__FILE__), '../')) # Root of the install dir, not the Rails app
|
|
83
87
|
self.templates = SitemapGenerator::Templates.new(self.root)
|
|
84
88
|
self.app = SitemapGenerator::Application.new
|
|
85
89
|
end
|
data/templates/sitemap.rb
CHANGED