sitemap_generator 6.2.1 → 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 +23 -0
- data/README.md +43 -37
- data/VERSION +1 -1
- data/lib/sitemap_generator/adapters/active_storage_adapter.rb +26 -0
- data/lib/sitemap_generator/adapters/aws_sdk_adapter.rb +14 -8
- 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 +7 -3
- 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 -63
- 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,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module SitemapGenerator
|
|
2
4
|
module Builder
|
|
3
5
|
class SitemapIndexFile < SitemapFile
|
|
@@ -6,12 +8,12 @@ module SitemapGenerator
|
|
|
6
8
|
#
|
|
7
9
|
# * <tt>location</tt> - a SitemapGenerator::SitemapIndexLocation instance or a Hash of options
|
|
8
10
|
# from which a SitemapLocation will be created for you.
|
|
9
|
-
def initialize(opts={})
|
|
11
|
+
def initialize(opts = {})
|
|
10
12
|
@location = opts.is_a?(Hash) ? SitemapGenerator::SitemapIndexLocation.new(opts) : opts
|
|
11
13
|
@link_count = 0
|
|
12
14
|
@sitemaps_link_count = 0
|
|
13
|
-
@xml_content = '' # XML urlset content
|
|
14
|
-
@xml_wrapper_start =
|
|
15
|
+
@xml_content = +'' # XML urlset content
|
|
16
|
+
@xml_wrapper_start = +<<-HTML
|
|
15
17
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
16
18
|
<sitemapindex
|
|
17
19
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
@@ -21,7 +23,7 @@ module SitemapGenerator
|
|
|
21
23
|
>
|
|
22
24
|
HTML
|
|
23
25
|
@xml_wrapper_start.gsub!(/\s+/, ' ').gsub!(/ *> */, '>').strip!
|
|
24
|
-
@xml_wrapper_end
|
|
26
|
+
@xml_wrapper_end = '</sitemapindex>'
|
|
25
27
|
@filesize = SitemapGenerator::Utilities.bytesize(@xml_wrapper_start) + SitemapGenerator::Utilities.bytesize(@xml_wrapper_end)
|
|
26
28
|
@written = false
|
|
27
29
|
@reserved_name = nil # holds the name reserved from the namer
|
|
@@ -45,7 +47,7 @@ module SitemapGenerator
|
|
|
45
47
|
# can assume that the index is required (unless create_index is false of course).
|
|
46
48
|
# This seems like the logical thing to do.
|
|
47
49
|
alias_method :super_add, :add
|
|
48
|
-
def add(link, options={})
|
|
50
|
+
def add(link, options = {})
|
|
49
51
|
if file = link.is_a?(SitemapFile) && link
|
|
50
52
|
@sitemaps_link_count += file.link_count
|
|
51
53
|
file.finalize! unless file.finalized?
|
|
@@ -59,7 +61,7 @@ module SitemapGenerator
|
|
|
59
61
|
# for there to be an index.
|
|
60
62
|
if @link_count == 0
|
|
61
63
|
@first_sitemap = SitemapGenerator::Builder::LinkHolder.new(file, options)
|
|
62
|
-
@link_count += 1
|
|
64
|
+
@link_count += 1 # pretend it's added, but don't add it yet
|
|
63
65
|
else
|
|
64
66
|
# need an index so make sure name is reserved and first sitemap is written out
|
|
65
67
|
reserve_name unless @location.create_index == false
|
|
@@ -94,13 +96,14 @@ module SitemapGenerator
|
|
|
94
96
|
@sitemaps_link_count
|
|
95
97
|
end
|
|
96
98
|
|
|
97
|
-
def stats_summary(opts={})
|
|
99
|
+
def stats_summary(opts = {})
|
|
98
100
|
str = "Sitemap stats: #{number_with_delimiter(@sitemaps_link_count)} links / #{@link_count} sitemaps"
|
|
99
|
-
str +=
|
|
101
|
+
str += ' / %dm%02ds' % opts[:time_taken].divmod(60) if opts[:time_taken]
|
|
100
102
|
end
|
|
101
103
|
|
|
102
104
|
def finalize!
|
|
103
105
|
raise SitemapGenerator::SitemapFinalizedError if finalized?
|
|
106
|
+
|
|
104
107
|
reserve_name if create_index?
|
|
105
108
|
write_first_sitemap
|
|
106
109
|
@frozen = true
|
|
@@ -138,7 +141,7 @@ module SitemapGenerator
|
|
|
138
141
|
if @first_sitemap
|
|
139
142
|
@first_sitemap.link.write unless @first_sitemap.link.written?
|
|
140
143
|
super_add(SitemapGenerator::Builder::SitemapIndexUrl.new(@first_sitemap.link, @first_sitemap.options))
|
|
141
|
-
@link_count -= 1
|
|
144
|
+
@link_count -= 1 # we already counted it, don't count it twice
|
|
142
145
|
# Store the URL because if create_index is false, this is the
|
|
143
146
|
# "index" URL
|
|
144
147
|
@first_sitemap_url = @first_sitemap.link.location.url
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'builder'
|
|
2
4
|
|
|
3
5
|
module SitemapGenerator
|
|
4
6
|
module Builder
|
|
5
7
|
class SitemapIndexUrl < SitemapUrl
|
|
6
8
|
|
|
7
|
-
def initialize(path, options={})
|
|
9
|
+
def initialize(path, options = {})
|
|
8
10
|
if index = path.is_a?(SitemapGenerator::Builder::SitemapIndexFile) && path
|
|
9
|
-
options = SitemapGenerator::Utilities.reverse_merge(options, :
|
|
11
|
+
options = SitemapGenerator::Utilities.reverse_merge(options, host: index.location.host, lastmod: Time.now, priority: 1.0)
|
|
10
12
|
path = index.location.path_in_public
|
|
11
13
|
super(path, options)
|
|
12
14
|
else
|
|
@@ -15,11 +17,11 @@ module SitemapGenerator
|
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
# Return the URL as XML
|
|
18
|
-
def to_xml(builder=nil)
|
|
20
|
+
def to_xml(builder = nil)
|
|
19
21
|
builder = ::Builder::XmlMarkup.new if builder.nil?
|
|
20
22
|
builder.sitemap do
|
|
21
23
|
builder.loc self[:loc]
|
|
22
|
-
builder.lastmod w3c_date(self[:lastmod])
|
|
24
|
+
builder.lastmod w3c_date(self[:lastmod]) if self[:lastmod]
|
|
23
25
|
end
|
|
24
26
|
builder << '' # force to string
|
|
25
27
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'builder'
|
|
2
4
|
require 'uri'
|
|
3
5
|
require 'time'
|
|
@@ -29,13 +31,13 @@ module SitemapGenerator
|
|
|
29
31
|
# * +mobile+
|
|
30
32
|
# * +alternate+/+alternates+
|
|
31
33
|
# * +pagemap+
|
|
32
|
-
def initialize(path, options={})
|
|
34
|
+
def initialize(path, options = {})
|
|
33
35
|
options = SitemapGenerator::Utilities.symbolize_keys(options)
|
|
34
36
|
if sitemap = path.is_a?(SitemapGenerator::Builder::SitemapFile) && path
|
|
35
37
|
SitemapGenerator::Utilities.reverse_merge!(
|
|
36
38
|
options,
|
|
37
|
-
:
|
|
38
|
-
:
|
|
39
|
+
host: sitemap.location.host,
|
|
40
|
+
lastmod: sitemap.lastmod
|
|
39
41
|
)
|
|
40
42
|
path = sitemap.location.path_in_public
|
|
41
43
|
end
|
|
@@ -46,16 +48,16 @@ module SitemapGenerator
|
|
|
46
48
|
)
|
|
47
49
|
SitemapGenerator::Utilities.reverse_merge!(
|
|
48
50
|
options,
|
|
49
|
-
:
|
|
50
|
-
:
|
|
51
|
-
:
|
|
52
|
-
:
|
|
53
|
-
:
|
|
54
|
-
:
|
|
55
|
-
:
|
|
56
|
-
:
|
|
51
|
+
priority: 0.5,
|
|
52
|
+
changefreq: 'weekly',
|
|
53
|
+
lastmod: Time.now,
|
|
54
|
+
images: [],
|
|
55
|
+
news: {},
|
|
56
|
+
videos: [],
|
|
57
|
+
mobile: false,
|
|
58
|
+
alternates: []
|
|
57
59
|
)
|
|
58
|
-
raise
|
|
60
|
+
raise 'Cannot generate a url without a host' unless SitemapGenerator::Utilities.present?(options[:host])
|
|
59
61
|
|
|
60
62
|
if video = options.delete(:video)
|
|
61
63
|
options[:videos] = video.is_a?(Array) ? options[:videos].concat(video) : options[:videos] << video
|
|
@@ -67,23 +69,23 @@ module SitemapGenerator
|
|
|
67
69
|
path = path.to_s.sub(/^\//, '')
|
|
68
70
|
loc = path.empty? ? options[:host] : (options[:host].to_s.sub(/\/$/, '') + '/' + path)
|
|
69
71
|
self.merge!(
|
|
70
|
-
:
|
|
71
|
-
:
|
|
72
|
-
:
|
|
73
|
-
:
|
|
74
|
-
:
|
|
75
|
-
:
|
|
76
|
-
:
|
|
77
|
-
:
|
|
78
|
-
:
|
|
79
|
-
:
|
|
80
|
-
:
|
|
81
|
-
:
|
|
72
|
+
priority: options[:priority],
|
|
73
|
+
changefreq: options[:changefreq],
|
|
74
|
+
lastmod: options[:lastmod],
|
|
75
|
+
expires: options[:expires],
|
|
76
|
+
host: options[:host],
|
|
77
|
+
loc: loc,
|
|
78
|
+
images: prepare_images(options[:images], options[:host]),
|
|
79
|
+
news: prepare_news(options[:news]),
|
|
80
|
+
videos: options[:videos],
|
|
81
|
+
mobile: options[:mobile],
|
|
82
|
+
alternates: options[:alternates],
|
|
83
|
+
pagemap: options[:pagemap]
|
|
82
84
|
)
|
|
83
85
|
end
|
|
84
86
|
|
|
85
87
|
# Return the URL as XML
|
|
86
|
-
def to_xml(builder=nil)
|
|
88
|
+
def to_xml(builder = nil)
|
|
87
89
|
builder = ::Builder::XmlMarkup.new if builder.nil?
|
|
88
90
|
builder.url do
|
|
89
91
|
builder.loc self[:loc]
|
|
@@ -94,8 +96,8 @@ module SitemapGenerator
|
|
|
94
96
|
|
|
95
97
|
unless SitemapGenerator::Utilities.blank?(self[:news])
|
|
96
98
|
news_data = self[:news]
|
|
97
|
-
builder.news:news do
|
|
98
|
-
builder.news:publication do
|
|
99
|
+
builder.news :news do
|
|
100
|
+
builder.news :publication do
|
|
99
101
|
builder.news :name, news_data[:publication_name].to_s if news_data[:publication_name]
|
|
100
102
|
builder.news :language, news_data[:publication_language].to_s if news_data[:publication_language]
|
|
101
103
|
end
|
|
@@ -110,7 +112,7 @@ module SitemapGenerator
|
|
|
110
112
|
end
|
|
111
113
|
|
|
112
114
|
self[:images].each do |image|
|
|
113
|
-
builder.image:image do
|
|
115
|
+
builder.image :image do
|
|
114
116
|
builder.image :loc, image[:loc]
|
|
115
117
|
builder.image :caption, image[:caption].to_s if image[:caption]
|
|
116
118
|
builder.image :geo_location, image[:geo_location].to_s if image[:geo_location]
|
|
@@ -124,25 +126,25 @@ module SitemapGenerator
|
|
|
124
126
|
builder.video :thumbnail_loc, video[:thumbnail_loc].to_s
|
|
125
127
|
builder.video :title, video[:title].to_s
|
|
126
128
|
builder.video :description, video[:description].to_s
|
|
127
|
-
builder.video :content_loc, video[:content_loc].to_s
|
|
129
|
+
builder.video :content_loc, video[:content_loc].to_s if video[:content_loc]
|
|
128
130
|
if video[:player_loc]
|
|
129
|
-
loc_attributes = { :
|
|
131
|
+
loc_attributes = { allow_embed: yes_or_no_with_default(video[:allow_embed], true) }
|
|
130
132
|
loc_attributes[:autoplay] = video[:autoplay].to_s if SitemapGenerator::Utilities.present?(video[:autoplay])
|
|
131
133
|
builder.video :player_loc, video[:player_loc].to_s, loc_attributes
|
|
132
134
|
end
|
|
133
|
-
builder.video :duration, video[:duration].to_s
|
|
134
|
-
builder.video :expiration_date,
|
|
135
|
-
builder.video :rating, format_float(video[:rating])
|
|
136
|
-
builder.video :view_count, video[:view_count].to_s
|
|
135
|
+
builder.video :duration, video[:duration].to_s if video[:duration]
|
|
136
|
+
builder.video :expiration_date, w3c_date(video[:expiration_date]) if video[:expiration_date]
|
|
137
|
+
builder.video :rating, format_float(video[:rating]) if video[:rating]
|
|
138
|
+
builder.video :view_count, video[:view_count].to_s if video[:view_count]
|
|
137
139
|
builder.video :publication_date, w3c_date(video[:publication_date]) if video[:publication_date]
|
|
138
|
-
video[:tags].each {|tag| builder.video :tag, tag.to_s }
|
|
140
|
+
video[:tags].each { |tag| builder.video :tag, tag.to_s } if video[:tags]
|
|
139
141
|
builder.video :tag, video[:tag].to_s if video[:tag]
|
|
140
142
|
builder.video :category, video[:category].to_s if video[:category]
|
|
141
|
-
builder.video :family_friendly,
|
|
142
|
-
builder.video :gallery_loc, video[:gallery_loc].to_s, :
|
|
143
|
+
builder.video :family_friendly, yes_or_no_with_default(video[:family_friendly], true) if video.has_key?(:family_friendly)
|
|
144
|
+
builder.video :gallery_loc, video[:gallery_loc].to_s, title: video[:gallery_title].to_s if video[:gallery_loc]
|
|
143
145
|
builder.video :price, video[:price].to_s, prepare_video_price_attribs(video) if SitemapGenerator::Utilities.present?(video[:price])
|
|
144
146
|
if video[:uploader]
|
|
145
|
-
builder.video :uploader, video[:uploader].to_s, video[:uploader_info] ? { :
|
|
147
|
+
builder.video :uploader, video[:uploader].to_s, video[:uploader_info] ? { info: video[:uploader_info].to_s } : {}
|
|
146
148
|
end
|
|
147
149
|
builder.video :live, yes_or_no_with_default(video[:live], true) if video.has_key?(:live)
|
|
148
150
|
builder.video :requires_subscription, yes_or_no_with_default(video[:requires_subscription], true) if video.has_key?(:requires_subscription)
|
|
@@ -151,7 +153,7 @@ module SitemapGenerator
|
|
|
151
153
|
|
|
152
154
|
self[:alternates].each do |alternate|
|
|
153
155
|
rel = alternate[:nofollow] ? 'alternate nofollow' : 'alternate'
|
|
154
|
-
attributes = { :
|
|
156
|
+
attributes = { rel: rel, href: alternate[:href].to_s }
|
|
155
157
|
attributes[:hreflang] = alternate[:lang].to_s if SitemapGenerator::Utilities.present?(alternate[:lang])
|
|
156
158
|
attributes[:media] = alternate[:media].to_s if SitemapGenerator::Utilities.present?(alternate[:media])
|
|
157
159
|
builder.xhtml :link, attributes
|
|
@@ -164,9 +166,9 @@ module SitemapGenerator
|
|
|
164
166
|
unless SitemapGenerator::Utilities.blank?(self[:pagemap])
|
|
165
167
|
builder.pagemap :PageMap do
|
|
166
168
|
SitemapGenerator::Utilities.as_array(self[:pagemap][:dataobjects]).each do |dataobject|
|
|
167
|
-
builder.pagemap :DataObject, :
|
|
169
|
+
builder.pagemap :DataObject, type: dataobject[:type].to_s, id: dataobject[:id].to_s do
|
|
168
170
|
SitemapGenerator::Utilities.as_array(dataobject[:attributes]).each do |attribute|
|
|
169
|
-
builder.pagemap :Attribute, attribute[:value].to_s, :
|
|
171
|
+
builder.pagemap :Attribute, attribute[:value].to_s, name: attribute[:name].to_s
|
|
170
172
|
end
|
|
171
173
|
end
|
|
172
174
|
end
|
|
@@ -197,12 +199,12 @@ module SitemapGenerator
|
|
|
197
199
|
|
|
198
200
|
# Return an Array of image option Hashes suitable to be parsed by SitemapGenerator::Builder::SitemapFile
|
|
199
201
|
def prepare_images(images, host)
|
|
200
|
-
images.delete_if { |key,value| key[:loc] == nil }
|
|
202
|
+
images.delete_if { |key, value| key[:loc] == nil }
|
|
201
203
|
images.each do |r|
|
|
202
204
|
SitemapGenerator::Utilities.assert_valid_keys(r, :loc, :caption, :geo_location, :title, :license)
|
|
203
205
|
r[:loc] = URI.join(host, r[:loc]).to_s
|
|
204
206
|
end
|
|
205
|
-
images[0..(SitemapGenerator::MAX_SITEMAP_IMAGES-1)]
|
|
207
|
+
images[0..(SitemapGenerator::MAX_SITEMAP_IMAGES - 1)]
|
|
206
208
|
end
|
|
207
209
|
|
|
208
210
|
def w3c_date(date)
|
|
@@ -211,23 +213,24 @@ module SitemapGenerator
|
|
|
211
213
|
elsif date.respond_to?(:iso8601)
|
|
212
214
|
date.iso8601.sub(/Z$/i, '+00:00')
|
|
213
215
|
elsif date.is_a?(Date) && !date.is_a?(DateTime)
|
|
214
|
-
date.strftime(
|
|
216
|
+
date.strftime('%Y-%m-%d')
|
|
215
217
|
else
|
|
216
|
-
zulutime =
|
|
217
|
-
date.
|
|
218
|
-
|
|
219
|
-
date.utc
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
218
|
+
zulutime =
|
|
219
|
+
if date.is_a?(DateTime)
|
|
220
|
+
date.new_offset(0)
|
|
221
|
+
elsif date.respond_to?(:utc)
|
|
222
|
+
date.utc
|
|
223
|
+
elsif date.is_a?(Integer)
|
|
224
|
+
Time.at(date).utc
|
|
225
|
+
else
|
|
226
|
+
nil
|
|
227
|
+
end
|
|
225
228
|
|
|
226
229
|
if zulutime
|
|
227
|
-
zulutime.strftime(
|
|
230
|
+
zulutime.strftime('%Y-%m-%dT%H:%M:%S+00:00')
|
|
228
231
|
else
|
|
229
232
|
zone = date.strftime('%z').insert(-3, ':')
|
|
230
|
-
date.strftime(
|
|
233
|
+
date.strftime('%Y-%m-%dT%H:%M:%S') + zone
|
|
231
234
|
end
|
|
232
235
|
end
|
|
233
236
|
end
|
|
@@ -237,6 +240,7 @@ module SitemapGenerator
|
|
|
237
240
|
def yes_or_no(value)
|
|
238
241
|
if value.is_a?(String)
|
|
239
242
|
raise ArgumentError.new("Unrecognized value for yes/no field: #{value.inspect}") unless value =~ /^(yes|no)$/i
|
|
243
|
+
|
|
240
244
|
value.downcase
|
|
241
245
|
else
|
|
242
246
|
value ? 'yes' : 'no'
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'sitemap_generator/builder/sitemap_file'
|
|
2
4
|
require 'sitemap_generator/builder/sitemap_index_file'
|
|
3
5
|
require 'sitemap_generator/builder/sitemap_url'
|
|
@@ -5,4 +7,4 @@ require 'sitemap_generator/builder/sitemap_index_url'
|
|
|
5
7
|
|
|
6
8
|
module SitemapGenerator::Builder
|
|
7
9
|
LinkHolder = Struct.new(:link, :options)
|
|
8
|
-
end
|
|
10
|
+
end
|
|
@@ -1,9 +1,11 @@
|
|
|
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
|
|
8
|
+
module Helpers # :nodoc:
|
|
7
9
|
|
|
8
10
|
# Provides methods for converting numbers into formatted strings.
|
|
9
11
|
# Methods are provided for precision, positional notation and file size
|
|
@@ -52,11 +54,11 @@ module SitemapGenerator
|
|
|
52
54
|
end
|
|
53
55
|
|
|
54
56
|
defaults = {
|
|
55
|
-
:
|
|
56
|
-
:
|
|
57
|
-
:
|
|
58
|
-
:
|
|
59
|
-
:
|
|
57
|
+
separator: '.',
|
|
58
|
+
delimiter: ',',
|
|
59
|
+
precision: 3,
|
|
60
|
+
significant: false,
|
|
61
|
+
strip_insignificant_zeros: false
|
|
60
62
|
}
|
|
61
63
|
options = SitemapGenerator::Utilities.reverse_merge(options, defaults)
|
|
62
64
|
|
|
@@ -105,18 +107,18 @@ module SitemapGenerator
|
|
|
105
107
|
end
|
|
106
108
|
|
|
107
109
|
defaults = {
|
|
108
|
-
:
|
|
109
|
-
:
|
|
110
|
-
:
|
|
111
|
-
:
|
|
112
|
-
:
|
|
110
|
+
separator: '.',
|
|
111
|
+
delimiter: ',',
|
|
112
|
+
precision: 3,
|
|
113
|
+
significant: false,
|
|
114
|
+
strip_insignificant_zeros: false
|
|
113
115
|
}
|
|
114
116
|
precision_defaults = {
|
|
115
|
-
:
|
|
117
|
+
delimiter: ''
|
|
116
118
|
}
|
|
117
119
|
defaults = defaults.merge(precision_defaults)
|
|
118
120
|
|
|
119
|
-
options = SitemapGenerator::Utilities.reverse_merge(options, defaults)
|
|
121
|
+
options = SitemapGenerator::Utilities.reverse_merge(options, defaults) # Allow the user to unset default values: Eg.: :significant => false
|
|
120
122
|
precision = options.delete :precision
|
|
121
123
|
significant = options.delete :significant
|
|
122
124
|
strip_insignificant_zeros = options.delete :strip_insignificant_zeros
|
|
@@ -126,11 +128,11 @@ module SitemapGenerator
|
|
|
126
128
|
digits, rounded_number = 1, 0
|
|
127
129
|
else
|
|
128
130
|
digits = (Math.log10(number.abs) + 1).floor
|
|
129
|
-
rounded_number = (SitemapGenerator::BigDecimal.new(number.to_s) / SitemapGenerator::BigDecimal.new((10
|
|
131
|
+
rounded_number = (SitemapGenerator::BigDecimal.new(number.to_s) / SitemapGenerator::BigDecimal.new((10**(digits - precision)).to_f.to_s)).round.to_f * 10**(digits - precision)
|
|
130
132
|
digits = (Math.log10(rounded_number.abs) + 1).floor # After rounding, the number of digits may have changed
|
|
131
133
|
end
|
|
132
134
|
precision = precision - digits
|
|
133
|
-
precision = precision > 0 ? precision : 0
|
|
135
|
+
precision = precision > 0 ? precision : 0 # don't let it be negative
|
|
134
136
|
else
|
|
135
137
|
rounded_number = SitemapGenerator::Utilities.round(SitemapGenerator::BigDecimal.new(number.to_s), precision).to_f
|
|
136
138
|
end
|
|
@@ -191,24 +193,24 @@ module SitemapGenerator
|
|
|
191
193
|
end
|
|
192
194
|
|
|
193
195
|
defaults = {
|
|
194
|
-
:
|
|
195
|
-
:
|
|
196
|
-
:
|
|
197
|
-
:
|
|
198
|
-
:
|
|
196
|
+
separator: '.',
|
|
197
|
+
delimiter: ',',
|
|
198
|
+
precision: 3,
|
|
199
|
+
significant: false,
|
|
200
|
+
strip_insignificant_zeros: false
|
|
199
201
|
}
|
|
200
202
|
human = {
|
|
201
|
-
:
|
|
202
|
-
:
|
|
203
|
-
:
|
|
204
|
-
:
|
|
203
|
+
delimiter: '',
|
|
204
|
+
precision: 3,
|
|
205
|
+
significant: true,
|
|
206
|
+
strip_insignificant_zeros: true
|
|
205
207
|
}
|
|
206
208
|
defaults = defaults.merge(human)
|
|
207
209
|
options = SitemapGenerator::Utilities.reverse_merge(options, defaults)
|
|
208
|
-
#for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
|
|
210
|
+
# for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
|
|
209
211
|
options[:strip_insignificant_zeros] = true if not options.key?(:strip_insignificant_zeros)
|
|
210
212
|
|
|
211
|
-
storage_units_format =
|
|
213
|
+
storage_units_format = '%n %u'
|
|
212
214
|
|
|
213
215
|
if number.to_i < 1024
|
|
214
216
|
unit = number.to_i > 1 || number.to_i == 0 ? 'Bytes' : 'Byte'
|
|
@@ -217,15 +219,15 @@ module SitemapGenerator
|
|
|
217
219
|
max_exp = STORAGE_UNITS.size - 1
|
|
218
220
|
exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024
|
|
219
221
|
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
|
|
220
|
-
number /= 1024
|
|
222
|
+
number /= 1024**exponent
|
|
221
223
|
|
|
222
224
|
unit_key = STORAGE_UNITS[exponent]
|
|
223
225
|
units = {
|
|
224
|
-
:
|
|
225
|
-
:
|
|
226
|
-
:
|
|
227
|
-
:
|
|
228
|
-
:
|
|
226
|
+
byte: 'Bytes',
|
|
227
|
+
kb: 'KB',
|
|
228
|
+
mb: 'MB',
|
|
229
|
+
gb: 'GB',
|
|
230
|
+
tb: 'TB'
|
|
229
231
|
}
|
|
230
232
|
unit = units[unit_key]
|
|
231
233
|
formatted_number = number_with_precision(number, options)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'sitemap_generator'
|
|
2
4
|
|
|
3
5
|
module SitemapGenerator
|
|
@@ -22,8 +24,8 @@ module SitemapGenerator
|
|
|
22
24
|
# * <tt>link_set</tt> - a LinkSet instance to use. Default is SitemapGenerator::Sitemap.
|
|
23
25
|
#
|
|
24
26
|
# 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, :
|
|
27
|
+
def initialize(opts = {}, &block)
|
|
28
|
+
opts = SitemapGenerator::Utilities.reverse_merge(opts, link_set: SitemapGenerator::Sitemap)
|
|
27
29
|
@linkset = opts.delete :link_set
|
|
28
30
|
@linkset.send(:set_options, opts)
|
|
29
31
|
eval(&block) if block_given?
|
|
@@ -53,7 +55,7 @@ module SitemapGenerator
|
|
|
53
55
|
|
|
54
56
|
# Evaluate the block in the interpreter. Pass :yield_sitemap => true to
|
|
55
57
|
# yield the Interpreter instance to the block...for old-style calling.
|
|
56
|
-
def eval(opts={}, &block)
|
|
58
|
+
def eval(opts = {}, &block)
|
|
57
59
|
if block_given?
|
|
58
60
|
if opts[:yield_sitemap]
|
|
59
61
|
yield @linkset
|
|
@@ -70,7 +72,7 @@ module SitemapGenerator
|
|
|
70
72
|
# * <tt>:config_file</tt> - full path to the config file to evaluate.
|
|
71
73
|
# Default is config/sitemap.rb in your application's root directory.
|
|
72
74
|
# All other options are passed to +new+.
|
|
73
|
-
def self.run(opts={}, &block)
|
|
75
|
+
def self.run(opts = {}, &block)
|
|
74
76
|
opts = opts.dup
|
|
75
77
|
config_file = opts.delete(:config_file)
|
|
76
78
|
config_file ||= SitemapGenerator.app.root + 'config/sitemap.rb'
|