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,23 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# require "sitemap_generator/core_ext/big_decimal/conversions"
|
|
2
|
-
require
|
|
4
|
+
require 'sitemap_generator/utilities'
|
|
3
5
|
|
|
4
6
|
module SitemapGenerator
|
|
5
7
|
# = SitemapGenerator Number Helpers
|
|
6
|
-
module Helpers
|
|
7
|
-
|
|
8
|
+
module Helpers # :nodoc:
|
|
8
9
|
# Provides methods for converting numbers into formatted strings.
|
|
9
10
|
# Methods are provided for precision, positional notation and file size
|
|
10
11
|
# and pretty printing.
|
|
11
12
|
#
|
|
12
13
|
# Most methods expect a +number+ argument, and will return it
|
|
13
14
|
# unchanged if can't be converted into a valid number.
|
|
14
|
-
module NumberHelper
|
|
15
|
-
|
|
15
|
+
module NumberHelper # rubocop:disable Metrics/ModuleLength
|
|
16
16
|
# Raised when argument +number+ param given to the helpers is invalid and
|
|
17
17
|
# the option :raise is set to +true+.
|
|
18
18
|
class InvalidNumberError < StandardError
|
|
19
19
|
attr_accessor :number
|
|
20
|
+
|
|
20
21
|
def initialize(number)
|
|
22
|
+
super(number.to_s)
|
|
21
23
|
@number = number
|
|
22
24
|
end
|
|
23
25
|
end
|
|
@@ -38,25 +40,23 @@ module SitemapGenerator
|
|
|
38
40
|
# number_with_delimiter(12345678.05, :locale => :fr) # => 12 345 678,05
|
|
39
41
|
# number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
|
|
40
42
|
# # => 98 765 432,98
|
|
41
|
-
def number_with_delimiter(number, options = {})
|
|
43
|
+
def number_with_delimiter(number, options = {}) # rubocop:disable Metrics/MethodLength
|
|
42
44
|
SitemapGenerator::Utilities.symbolize_keys!(options)
|
|
43
45
|
|
|
44
46
|
begin
|
|
45
47
|
Float(number)
|
|
46
48
|
rescue ArgumentError, TypeError
|
|
47
|
-
if options[:raise]
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return number
|
|
51
|
-
end
|
|
49
|
+
raise InvalidNumberError, number if options[:raise]
|
|
50
|
+
|
|
51
|
+
return number
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
defaults = {
|
|
55
|
-
:
|
|
56
|
-
:
|
|
57
|
-
:
|
|
58
|
-
:
|
|
59
|
-
:
|
|
55
|
+
separator: '.',
|
|
56
|
+
delimiter: ',',
|
|
57
|
+
precision: 3,
|
|
58
|
+
significant: false,
|
|
59
|
+
strip_insignificant_zeros: false
|
|
60
60
|
}
|
|
61
61
|
options = SitemapGenerator::Utilities.reverse_merge(options, defaults)
|
|
62
62
|
|
|
@@ -72,10 +72,12 @@ module SitemapGenerator
|
|
|
72
72
|
# ==== Options
|
|
73
73
|
# * <tt>:locale</tt> - Sets the locale to be used for formatting (defaults to current locale).
|
|
74
74
|
# * <tt>:precision</tt> - Sets the precision of the number (defaults to 3).
|
|
75
|
-
# * <tt>:significant</tt> - If +true+, precision will be the # of significant_digits.
|
|
75
|
+
# * <tt>:significant</tt> - If +true+, precision will be the # of significant_digits.
|
|
76
|
+
# If +false+, the # of fractional digits (defaults to +false+)
|
|
76
77
|
# * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults to ".").
|
|
77
78
|
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
|
|
78
|
-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator
|
|
79
|
+
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator
|
|
80
|
+
# (defaults to +false+)
|
|
79
81
|
#
|
|
80
82
|
# ==== Examples
|
|
81
83
|
# number_with_precision(111.2345) # => 111.235
|
|
@@ -91,62 +93,68 @@ module SitemapGenerator
|
|
|
91
93
|
# number_with_precision(389.32314, :precision => 4, :significant => true) # => 389.3
|
|
92
94
|
# number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
|
|
93
95
|
# # => 1.111,23
|
|
94
|
-
def number_with_precision(number, options = {})
|
|
96
|
+
def number_with_precision(number, options = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
95
97
|
SitemapGenerator::Utilities.symbolize_keys!(options)
|
|
96
98
|
|
|
97
|
-
|
|
98
|
-
Float(number)
|
|
99
|
+
begin
|
|
100
|
+
number = Float(number)
|
|
99
101
|
rescue ArgumentError, TypeError
|
|
100
|
-
if options[:raise]
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return number
|
|
104
|
-
end
|
|
102
|
+
raise InvalidNumberError, number if options[:raise]
|
|
103
|
+
|
|
104
|
+
return number
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
defaults = {
|
|
108
|
-
:
|
|
109
|
-
:
|
|
110
|
-
:
|
|
111
|
-
:
|
|
112
|
-
:
|
|
108
|
+
separator: '.',
|
|
109
|
+
delimiter: ',',
|
|
110
|
+
precision: 3,
|
|
111
|
+
significant: false,
|
|
112
|
+
strip_insignificant_zeros: false
|
|
113
113
|
}
|
|
114
114
|
precision_defaults = {
|
|
115
|
-
:
|
|
115
|
+
delimiter: ''
|
|
116
116
|
}
|
|
117
117
|
defaults = defaults.merge(precision_defaults)
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
# Allow the user to unset default values: e.g. significant: false
|
|
120
|
+
options = SitemapGenerator::Utilities.reverse_merge(options, defaults)
|
|
120
121
|
precision = options.delete :precision
|
|
121
122
|
significant = options.delete :significant
|
|
122
123
|
strip_insignificant_zeros = options.delete :strip_insignificant_zeros
|
|
123
124
|
|
|
124
|
-
if significant
|
|
125
|
-
if number
|
|
126
|
-
digits
|
|
125
|
+
if significant && precision.positive?
|
|
126
|
+
if number.zero?
|
|
127
|
+
digits = 1
|
|
128
|
+
rounded_number = 0
|
|
127
129
|
else
|
|
128
130
|
digits = (Math.log10(number.abs) + 1).floor
|
|
129
|
-
|
|
131
|
+
step = SitemapGenerator::BigDecimal.new((10**(digits - precision)).to_f.to_s)
|
|
132
|
+
rounded_number = (SitemapGenerator::BigDecimal.new(number.to_s) / step)
|
|
133
|
+
.round.to_f * (10**(digits - precision))
|
|
130
134
|
digits = (Math.log10(rounded_number.abs) + 1).floor # After rounding, the number of digits may have changed
|
|
131
135
|
end
|
|
132
|
-
precision
|
|
133
|
-
precision =
|
|
136
|
+
precision -= digits
|
|
137
|
+
precision = 0 unless precision.positive? # don't let it be negative
|
|
134
138
|
else
|
|
135
|
-
rounded_number = SitemapGenerator::Utilities.round(SitemapGenerator::BigDecimal.new(number.to_s),
|
|
139
|
+
rounded_number = SitemapGenerator::Utilities.round(SitemapGenerator::BigDecimal.new(number.to_s),
|
|
140
|
+
precision).to_f
|
|
136
141
|
end
|
|
137
|
-
formatted_number = number_with_delimiter("%01.#{precision}f"
|
|
142
|
+
formatted_number = number_with_delimiter(format("%01.#{precision}f", rounded_number), options)
|
|
138
143
|
if strip_insignificant_zeros
|
|
139
144
|
escaped_separator = Regexp.escape(options[:separator])
|
|
140
145
|
formatted_number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
|
|
141
146
|
else
|
|
142
147
|
formatted_number
|
|
143
148
|
end
|
|
144
|
-
|
|
145
149
|
end
|
|
146
150
|
|
|
147
|
-
STORAGE_UNITS = [
|
|
148
|
-
DECIMAL_UNITS = {
|
|
149
|
-
|
|
151
|
+
STORAGE_UNITS = %i[byte kb mb gb tb].freeze
|
|
152
|
+
DECIMAL_UNITS = {
|
|
153
|
+
0 => :unit, 1 => :ten, 2 => :hundred, 3 => :thousand,
|
|
154
|
+
6 => :million, 9 => :billion, 12 => :trillion, 15 => :quadrillion,
|
|
155
|
+
-1 => :deci, -2 => :centi, -3 => :mili, -6 => :micro,
|
|
156
|
+
-9 => :nano, -12 => :pico, -15 => :femto
|
|
157
|
+
}.freeze
|
|
150
158
|
|
|
151
159
|
# Formats the bytes in +number+ into a more understandable representation
|
|
152
160
|
# (e.g., giving it 1500 yields 1.5 KB). This method is useful for
|
|
@@ -158,10 +166,12 @@ module SitemapGenerator
|
|
|
158
166
|
# ==== Options
|
|
159
167
|
# * <tt>:locale</tt> - Sets the locale to be used for formatting (defaults to current locale).
|
|
160
168
|
# * <tt>:precision</tt> - Sets the precision of the number (defaults to 3).
|
|
161
|
-
# * <tt>:significant</tt> - If +true+, precision will be the # of significant_digits.
|
|
169
|
+
# * <tt>:significant</tt> - If +true+, precision will be the # of significant_digits.
|
|
170
|
+
# If +false+, the # of fractional digits (defaults to +true+)
|
|
162
171
|
# * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults to ".").
|
|
163
172
|
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
|
|
164
|
-
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator
|
|
173
|
+
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator
|
|
174
|
+
# (defaults to +true+)
|
|
165
175
|
# ==== Examples
|
|
166
176
|
# number_to_human_size(123) # => 123 Bytes
|
|
167
177
|
# number_to_human_size(1234) # => 1.21 KB
|
|
@@ -177,59 +187,57 @@ module SitemapGenerator
|
|
|
177
187
|
# <tt>:strip_insignificant_zeros</tt> to +false+ to change that):
|
|
178
188
|
# number_to_human_size(1234567890123, :precision => 5) # => "1.1229 TB"
|
|
179
189
|
# number_to_human_size(524288000, :precision=>5) # => "500 MB"
|
|
180
|
-
def number_to_human_size(number, options = {})
|
|
190
|
+
def number_to_human_size(number, options = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
181
191
|
SitemapGenerator::Utilities.symbolize_keys!(options)
|
|
182
192
|
|
|
183
|
-
|
|
184
|
-
Float(number)
|
|
193
|
+
begin
|
|
194
|
+
number = Float(number)
|
|
185
195
|
rescue ArgumentError, TypeError
|
|
186
|
-
if options[:raise]
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
return number
|
|
190
|
-
end
|
|
196
|
+
raise InvalidNumberError, number if options[:raise]
|
|
197
|
+
|
|
198
|
+
return number
|
|
191
199
|
end
|
|
192
200
|
|
|
193
201
|
defaults = {
|
|
194
|
-
:
|
|
195
|
-
:
|
|
196
|
-
:
|
|
197
|
-
:
|
|
198
|
-
:
|
|
202
|
+
separator: '.',
|
|
203
|
+
delimiter: ',',
|
|
204
|
+
precision: 3,
|
|
205
|
+
significant: false,
|
|
206
|
+
strip_insignificant_zeros: false
|
|
199
207
|
}
|
|
200
208
|
human = {
|
|
201
|
-
:
|
|
202
|
-
:
|
|
203
|
-
:
|
|
204
|
-
:
|
|
209
|
+
delimiter: '',
|
|
210
|
+
precision: 3,
|
|
211
|
+
significant: true,
|
|
212
|
+
strip_insignificant_zeros: true
|
|
205
213
|
}
|
|
206
214
|
defaults = defaults.merge(human)
|
|
207
215
|
options = SitemapGenerator::Utilities.reverse_merge(options, defaults)
|
|
208
|
-
#for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
|
|
209
|
-
options[:strip_insignificant_zeros] = true
|
|
216
|
+
# for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
|
|
217
|
+
options[:strip_insignificant_zeros] = true unless options.key?(:strip_insignificant_zeros)
|
|
210
218
|
|
|
211
|
-
storage_units_format =
|
|
219
|
+
storage_units_format = '%n %u'
|
|
212
220
|
|
|
213
221
|
if number.to_i < 1024
|
|
214
|
-
unit = number.to_i > 1 || number.to_i
|
|
215
|
-
storage_units_format.gsub(
|
|
222
|
+
unit = number.to_i > 1 || number.to_i.zero? ? 'Bytes' : 'Byte'
|
|
223
|
+
storage_units_format.gsub('%n', number.to_i.to_s).gsub('%u', unit)
|
|
216
224
|
else
|
|
217
225
|
max_exp = STORAGE_UNITS.size - 1
|
|
218
226
|
exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024
|
|
219
227
|
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
|
|
220
|
-
number /= 1024
|
|
228
|
+
number /= 1024**exponent
|
|
221
229
|
|
|
222
230
|
unit_key = STORAGE_UNITS[exponent]
|
|
223
231
|
units = {
|
|
224
|
-
:
|
|
225
|
-
:
|
|
226
|
-
:
|
|
227
|
-
:
|
|
228
|
-
:
|
|
232
|
+
byte: 'Bytes',
|
|
233
|
+
kb: 'KB',
|
|
234
|
+
mb: 'MB',
|
|
235
|
+
gb: 'GB',
|
|
236
|
+
tb: 'TB'
|
|
229
237
|
}
|
|
230
238
|
unit = units[unit_key]
|
|
231
239
|
formatted_number = number_with_precision(number, options)
|
|
232
|
-
storage_units_format.gsub(
|
|
240
|
+
storage_units_format.gsub('%n', formatted_number).gsub('%u', unit)
|
|
233
241
|
end
|
|
234
242
|
end
|
|
235
243
|
end
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'sitemap_generator'
|
|
2
4
|
|
|
3
5
|
module SitemapGenerator
|
|
4
|
-
|
|
5
6
|
# Provide a class for evaluating blocks, making the URL helpers from the framework
|
|
6
7
|
# and API methods available to it.
|
|
7
8
|
class Interpreter
|
|
8
|
-
|
|
9
9
|
if SitemapGenerator.app.is_at_least_rails3?
|
|
10
|
-
|
|
11
|
-
include ::Rails.application.routes.url_helpers
|
|
12
|
-
end
|
|
10
|
+
include ::Rails.application.routes.url_helpers unless ::Rails.application.nil?
|
|
13
11
|
elsif SitemapGenerator.app.is_rails?
|
|
14
12
|
require 'action_controller'
|
|
15
13
|
include ActionController::UrlWriter
|
|
@@ -22,11 +20,17 @@ module SitemapGenerator
|
|
|
22
20
|
# * <tt>link_set</tt> - a LinkSet instance to use. Default is SitemapGenerator::Sitemap.
|
|
23
21
|
#
|
|
24
22
|
# All other options are passed to the LinkSet by setting them using accessor methods.
|
|
25
|
-
def initialize(opts={}, &block)
|
|
26
|
-
opts = SitemapGenerator::Utilities.reverse_merge(opts, :
|
|
23
|
+
def initialize(opts = {}, &block)
|
|
24
|
+
opts = SitemapGenerator::Utilities.reverse_merge(opts, link_set: SitemapGenerator::Sitemap)
|
|
27
25
|
@linkset = opts.delete :link_set
|
|
28
26
|
@linkset.send(:set_options, opts)
|
|
29
|
-
eval(&block) if block_given?
|
|
27
|
+
eval(&block) if block_given? # rubocop:disable Security/Eval
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def default_url_options
|
|
31
|
+
return {} unless defined?(ActionController::Base)
|
|
32
|
+
|
|
33
|
+
ActionController::Base.default_url_options || {}
|
|
30
34
|
end
|
|
31
35
|
|
|
32
36
|
def add(*args)
|
|
@@ -53,13 +57,13 @@ module SitemapGenerator
|
|
|
53
57
|
|
|
54
58
|
# Evaluate the block in the interpreter. Pass :yield_sitemap => true to
|
|
55
59
|
# yield the Interpreter instance to the block...for old-style calling.
|
|
56
|
-
def eval(opts={}, &block)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
def eval(opts = {}, &block)
|
|
61
|
+
return unless block_given?
|
|
62
|
+
|
|
63
|
+
if opts[:yield_sitemap]
|
|
64
|
+
yield @linkset
|
|
65
|
+
else
|
|
66
|
+
instance_eval(&block)
|
|
63
67
|
end
|
|
64
68
|
end
|
|
65
69
|
|
|
@@ -70,11 +74,11 @@ module SitemapGenerator
|
|
|
70
74
|
# * <tt>:config_file</tt> - full path to the config file to evaluate.
|
|
71
75
|
# Default is config/sitemap.rb in your application's root directory.
|
|
72
76
|
# All other options are passed to +new+.
|
|
73
|
-
def self.run(opts={}
|
|
77
|
+
def self.run(opts = {})
|
|
74
78
|
opts = opts.dup
|
|
75
79
|
config_file = opts.delete(:config_file)
|
|
76
|
-
config_file ||= SitemapGenerator.app.root + 'config/sitemap.rb'
|
|
77
|
-
interpreter =
|
|
80
|
+
config_file ||= SitemapGenerator.app.root + 'config/sitemap.rb' # rubocop:disable Style/StringConcatenation
|
|
81
|
+
interpreter = new(opts)
|
|
78
82
|
interpreter.instance_eval(File.read(config_file), config_file.to_s)
|
|
79
83
|
interpreter
|
|
80
84
|
end
|