sitemap_generator 6.3.0 → 7.0.2

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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +20 -0
  3. data/README.md +37 -31
  4. data/VERSION +1 -1
  5. data/lib/sitemap_generator/adapters/active_storage_adapter.rb +26 -0
  6. data/lib/sitemap_generator/adapters/aws_sdk_adapter.rb +24 -9
  7. data/lib/sitemap_generator/adapters/file_adapter.rb +2 -0
  8. data/lib/sitemap_generator/adapters/fog_adapter.rb +7 -5
  9. data/lib/sitemap_generator/adapters/google_storage_adapter.rb +3 -1
  10. data/lib/sitemap_generator/adapters/s3_adapter.rb +10 -6
  11. data/lib/sitemap_generator/adapters/wave_adapter.rb +3 -1
  12. data/lib/sitemap_generator/application.rb +4 -0
  13. data/lib/sitemap_generator/builder/sitemap_file.rb +19 -13
  14. data/lib/sitemap_generator/builder/sitemap_index_file.rb +12 -9
  15. data/lib/sitemap_generator/builder/sitemap_index_url.rb +6 -4
  16. data/lib/sitemap_generator/builder/sitemap_url.rb +59 -55
  17. data/lib/sitemap_generator/builder.rb +3 -1
  18. data/lib/sitemap_generator/core_ext/big_decimal.rb +2 -0
  19. data/lib/sitemap_generator/core_ext/numeric.rb +2 -0
  20. data/lib/sitemap_generator/core_ext.rb +2 -0
  21. data/lib/sitemap_generator/helpers/number_helper.rb +35 -33
  22. data/lib/sitemap_generator/interpreter.rb +6 -4
  23. data/lib/sitemap_generator/link_set.rb +65 -62
  24. data/lib/sitemap_generator/railtie.rb +55 -1
  25. data/lib/sitemap_generator/simple_namer.rb +8 -5
  26. data/lib/sitemap_generator/sitemap_location.rb +13 -9
  27. data/lib/sitemap_generator/tasks.rb +14 -26
  28. data/lib/sitemap_generator/templates.rb +8 -5
  29. data/lib/sitemap_generator/utilities.rb +38 -11
  30. data/lib/sitemap_generator.rb +24 -17
  31. data/templates/sitemap.rb +1 -1
  32. metadata +7 -131
@@ -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 = [:filename, :sitemaps_path, :sitemaps_host, :namer]
8
- @@new_location_opts = [:filename, :sitemaps_path, :namer]
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(:yield_sitemap => yield_sitemap?, &block)
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(:time_taken => end_time - start_time)) if verbose
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
- :include_root => true,
125
- :include_index => false,
126
- :filename => :sitemap,
127
- :search_engines => {
128
- :google => "http://www.google.com/webmasters/tools/ping?sitemap=%s"
129
- },
130
- :create_index => :auto,
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}".to_sym, v) }
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 if !@added_default_links
151
- sitemap.add(link, SitemapGenerator::Utilities.reverse_merge(options, :host => @default_host))
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, :host => sitemaps_host))
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(:yield_sitemap => @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
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(:yield_sitemap => @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
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 = CGI.escape(unescaped_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 = link % index_url
293
+ link %= index_url
294
294
  name = Utilities.titleize(engine.to_s)
295
295
  begin
296
- Timeout::timeout(10) {
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(filename namer).each do |key|
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
- :include_index => false,
405
- :include_root => false,
406
- :sitemap_index => sitemap_index
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 = { :lastmod => Time.now, :priority => 1.0 }
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(:link_set => self)
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
- :host => sitemaps_host,
580
- :namer => namer,
581
- :public_path => public_path,
582
- :sitemaps_path => @sitemaps_path,
583
- :adapter => @adapter,
584
- :verbose => verbose,
585
- :compress => @compress,
586
- :max_sitemap_links => max_sitemap_links
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
- :host => sitemaps_host,
594
- :namer => namer,
595
- :public_path => public_path,
596
- :sitemaps_path => @sitemaps_path,
597
- :adapter => @adapter,
598
- :verbose => verbose,
599
- :create_index => @create_index,
600
- :compress => @compress
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, :include_index => !@protect_index)
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,7 +1,61 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SitemapGenerator
2
4
  class Railtie < Rails::Railtie
5
+ # Top level options object to namespace all settings
6
+ config.sitemap = ActiveSupport::OrderedOptions.new
7
+
3
8
  rake_tasks do
4
- load "tasks/sitemap_generator_tasks.rake"
9
+ load 'tasks/sitemap_generator_tasks.rake'
10
+ end
11
+
12
+ # Recognize existing Rails options as defaults for config.sitemap.*
13
+ initializer 'sitemap_generator.set_configs' do |app|
14
+ # routes.default_url_options takes precedence, falling back to configs
15
+ url_opts = (app.default_url_options || {})
16
+ .with_defaults(config.try(:action_controller).try(:default_url_options) || {})
17
+ .with_defaults(config.try(:action_mailer).try(:default_url_options) || {})
18
+ .with_defaults(config.try(:active_job).try(:default_url_options) || {})
19
+
20
+ config.sitemap.default_host ||= ActionDispatch::Http::URL.full_url_for(url_opts) if url_opts.key?(:host)
21
+
22
+ # Rails defaults action_controller.asset_host and action_mailer.asset_host
23
+ # to the top-level config.asset_host so we get that for free here.
24
+ config.sitemap.sitemaps_host ||= [
25
+ config.try(:action_controller).try(:asset_host),
26
+ config.try(:action_mailer).try(:asset_host)
27
+ ].grep(String).first
28
+
29
+ config.sitemap.compress = config.try(:assets).try(:gzip) if config.sitemap.compress.nil?
30
+
31
+ config.sitemap.public_path ||= app.paths['public'].first
32
+
33
+ # "Compile" config.sitemap options onto the Sitemap class.
34
+ config.after_initialize do
35
+ ActiveSupport.on_load(:sitemap_generator, yield: true) do |sitemap|
36
+ config.sitemap.except(:adapter).each { |k, v| sitemap.public_send("#{k}=", v) }
37
+ end
38
+ end
39
+ end
40
+
41
+ # Allow setting the CONFIG_FILE without relying on env var;
42
+ # (e.g in config/application or environments/*.rb)
43
+ initializer 'sitemap_generator.config_file' do
44
+ if (config_file = config.sitemap.delete(:config_file).presence) && ENV['CONFIG_FILE'].blank?
45
+ ENV['CONFIG_FILE'] = config_file
46
+ end
47
+ end
48
+
49
+ # Allow lazily setting the adapter class without forcing an autoload.
50
+ # (ie. string or symbol name; or Callable (proc/lambda/etc))
51
+ initializer 'sitemap_generator.adapter' do |app|
52
+ config.to_prepare do
53
+ ActiveSupport.on_load(:sitemap_generator) do
54
+ self.adapter = Utilities.find_adapter app.config.sitemap.adapter
55
+ end
56
+ end
5
57
  end
6
58
  end
59
+
60
+ ActiveSupport.run_load_hooks(:sitemap_generator, Sitemap)
7
61
  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
- :zero => nil, # identifies the marker for the start of the series
32
- :extension => '.xml.gz',
33
- :start => 1
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, "Already at the start of the series" if start?
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, :start => 2, :zero => 1)
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, "No filename or namer set" unless self[:filename] || self[:namer]
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, :super => true)
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
 
@@ -8,46 +10,32 @@ namespace :sitemap do
8
10
  # Require sitemap_generator only. When installed as a plugin the require will fail, so in
9
11
  # that case, load the environment first.
10
12
  task :require do
11
- begin
12
- require 'sitemap_generator'
13
- rescue LoadError => e
14
- if defined?(Rails::VERSION)
15
- Rake::Task['sitemap:require_environment'].invoke
16
- else
17
- raise e
18
- end
19
- end
20
- end
21
-
22
- # Require sitemap_generator after loading the Rails environment. We still need the require
23
- # in case we are installed as a gem and are setup to not automatically be required.
24
- task :require_environment do
25
- if defined?(Rails::VERSION)
26
- Rake::Task['environment'].invoke
27
- end
28
13
  require 'sitemap_generator'
29
14
  end
15
+ # In a Rails app, we need to boot Rails.
16
+ # Ensure gem is required in case it wasn't automatically loaded.
17
+ Rake::Task[:require].enhance([:environment]) if defined?(Rails)
30
18
 
31
- desc "Install a default config/sitemap.rb file"
32
- task :install => ['sitemap:require'] do
19
+ desc 'Install a default config/sitemap.rb file'
20
+ task install: :require do
33
21
  SitemapGenerator::Utilities.install_sitemap_rb(verbose)
34
22
  end
35
23
 
36
- desc "Delete all Sitemap files in public/ directory"
37
- task :clean => ['sitemap:require'] do
24
+ desc 'Delete all Sitemap files in public/ directory'
25
+ task clean: :require do
38
26
  SitemapGenerator::Utilities.clean_files
39
27
  end
40
28
 
41
- desc "Generate sitemaps and ping search engines."
42
- task :refresh => ['sitemap:create'] do
29
+ desc 'Generate sitemaps and ping search engines.'
30
+ task refresh: :create do
43
31
  SitemapGenerator::Sitemap.ping_search_engines
44
32
  end
45
33
 
46
34
  desc "Generate sitemaps but don't ping search engines."
47
- task 'refresh:no_ping' => ['sitemap:create']
35
+ task 'refresh:no_ping' => :create
48
36
 
49
37
  desc "Generate sitemaps but don't ping search engines. Alias for refresh:no_ping."
50
- task :create => ['sitemap:require_environment'] do
51
- SitemapGenerator::Interpreter.run(:config_file => ENV["CONFIG_FILE"], :verbose => verbose)
38
+ task create: :require do
39
+ SitemapGenerator::Interpreter.run(config_file: ENV['CONFIG_FILE'], verbose: verbose)
52
40
  end
53
41
  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
- :sitemap_sample => 'sitemap.rb',
12
+ sitemap_sample: 'sitemap.rb',
11
13
  }
12
14
 
13
15
  # Dynamically define accessors for each key defined in <tt>FILES</tt>
14
- attr_accessor(*FILES.keys)
15
- FILES.keys.each do |name|
16
- eval <<-END
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
- END
23
+ ACCESSOR
21
24
  end
22
25
 
23
26
  def initialize(root = SitemapGenerator.root)