meta-tags 2.22.2 → 2.23.0
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
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +21 -0
- data/README.md +58 -43
- data/lib/generators/meta_tags/install_generator.rb +5 -0
- data/lib/generators/meta_tags/templates/config/initializers/meta_tags.rb +6 -5
- data/lib/meta_tags/configuration.rb +21 -10
- data/lib/meta_tags/content_tag.rb +3 -4
- data/lib/meta_tags/controller_helper.rb +11 -8
- data/lib/meta_tags/meta_tags_collection.rb +37 -36
- data/lib/meta_tags/railtie.rb +1 -0
- data/lib/meta_tags/renderer.rb +80 -78
- data/lib/meta_tags/tag.rb +10 -8
- data/lib/meta_tags/text_normalizer.rb +52 -39
- data/lib/meta_tags/version.rb +1 -1
- data/lib/meta_tags/view_helper.rb +62 -75
- data/lib/meta_tags.rb +0 -2
- data/sig/{lib/_internal/rails.rbs → _private/rails_support.rbs} +0 -19
- data/sig/lib/meta_tags/controller_helper.rbs +1 -1
- data/sig/lib/meta_tags/meta_tags_collection.rbs +2 -2
- data/sig/lib/meta_tags/rails_interfaces.rbs +15 -0
- data/sig/lib/meta_tags/view_helper.rbs +9 -38
- data/sig/lib/meta_tags.rbs +3 -1
- data.tar.gz.sig +0 -0
- metadata +28 -24
- metadata.gz.sig +0 -0
|
@@ -1,42 +1,38 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module MetaTags
|
|
4
|
-
#
|
|
4
|
+
# This class represents a collection of meta tags. Basically a wrapper around
|
|
5
5
|
# HashWithIndifferentAccess, with some additional helper methods.
|
|
6
6
|
class MetaTagsCollection
|
|
7
7
|
attr_reader :meta_tags
|
|
8
8
|
|
|
9
9
|
# Initializes a new instance of MetaTagsCollection.
|
|
10
|
-
#
|
|
11
10
|
def initialize
|
|
12
11
|
@meta_tags = ActiveSupport::HashWithIndifferentAccess.new
|
|
13
12
|
end
|
|
14
13
|
|
|
15
14
|
# Returns meta tag value by name.
|
|
16
15
|
#
|
|
17
|
-
# @param [String, Symbol]
|
|
16
|
+
# @param name [String, Symbol] meta tag name.
|
|
18
17
|
# @return meta tag value.
|
|
19
|
-
#
|
|
20
18
|
def [](name)
|
|
21
19
|
@meta_tags[name]
|
|
22
20
|
end
|
|
23
21
|
|
|
24
22
|
# Sets meta tag value by name.
|
|
25
23
|
#
|
|
26
|
-
# @param [String, Symbol]
|
|
24
|
+
# @param name [String, Symbol] meta tag name.
|
|
27
25
|
# @param value meta tag value.
|
|
28
26
|
# @return meta tag value.
|
|
29
|
-
#
|
|
30
27
|
def []=(name, value)
|
|
31
28
|
@meta_tags[name] = value
|
|
32
29
|
end
|
|
33
30
|
|
|
34
|
-
# Recursively merges a Hash of meta tag attributes into current list.
|
|
31
|
+
# Recursively merges a Hash of meta tag attributes into the current list.
|
|
35
32
|
#
|
|
36
|
-
# @param [Hash, #to_meta_tags]
|
|
33
|
+
# @param object [Hash, #to_meta_tags] Hash of meta tags (or object responding
|
|
37
34
|
# to #to_meta_tags and returning a hash) to merge into the current list.
|
|
38
35
|
# @return [Hash] result of the merge.
|
|
39
|
-
#
|
|
40
36
|
def update(object = {})
|
|
41
37
|
meta_tags = if object.respond_to?(:to_meta_tags)
|
|
42
38
|
# @type var object: _MetaTagish & Object
|
|
@@ -48,11 +44,10 @@ module MetaTags
|
|
|
48
44
|
@meta_tags.deep_merge! normalize_open_graph(meta_tags)
|
|
49
45
|
end
|
|
50
46
|
|
|
51
|
-
#
|
|
47
|
+
# Temporarily merges defaults with the current meta tags list and yields the block.
|
|
52
48
|
#
|
|
53
|
-
# @param [Hash]
|
|
49
|
+
# @param defaults [Hash] list of default meta tag attributes.
|
|
54
50
|
# @return result of the block call.
|
|
55
|
-
#
|
|
56
51
|
def with_defaults(defaults = {})
|
|
57
52
|
old_meta_tags = @meta_tags
|
|
58
53
|
@meta_tags = normalize_open_graph(defaults).deep_merge!(@meta_tags)
|
|
@@ -63,9 +58,8 @@ module MetaTags
|
|
|
63
58
|
|
|
64
59
|
# Constructs the full title as if it would be rendered in title meta tag.
|
|
65
60
|
#
|
|
66
|
-
# @param [Hash]
|
|
61
|
+
# @param defaults [Hash] list of default meta tag attributes.
|
|
67
62
|
# @return [String] page title.
|
|
68
|
-
#
|
|
69
63
|
def full_title(defaults = {})
|
|
70
64
|
with_defaults(defaults) { extract_full_title }
|
|
71
65
|
end
|
|
@@ -73,30 +67,29 @@ module MetaTags
|
|
|
73
67
|
# Constructs the title without site title (for normalized parameters). When title is empty,
|
|
74
68
|
# use the site title instead.
|
|
75
69
|
#
|
|
70
|
+
# @param defaults [Hash] list of default meta tag attributes.
|
|
76
71
|
# @return [String] page title.
|
|
77
|
-
#
|
|
78
72
|
def page_title(defaults = {})
|
|
73
|
+
had_site = @meta_tags.key?(:site)
|
|
79
74
|
old_site = @meta_tags[:site]
|
|
80
|
-
@meta_tags[:site] = nil
|
|
75
|
+
@meta_tags[:site] = nil if had_site
|
|
81
76
|
full_title = with_defaults(defaults) { extract_full_title }
|
|
82
77
|
full_title.presence || old_site || ""
|
|
83
78
|
ensure
|
|
84
|
-
@meta_tags[:site] = old_site
|
|
79
|
+
@meta_tags[:site] = old_site if had_site
|
|
85
80
|
end
|
|
86
81
|
|
|
87
82
|
# Deletes and returns a meta tag value by name.
|
|
88
83
|
#
|
|
89
|
-
# @param [String, Symbol]
|
|
84
|
+
# @param name [String, Symbol] meta tag name.
|
|
90
85
|
# @return [Object] meta tag value.
|
|
91
|
-
#
|
|
92
86
|
def extract(name)
|
|
93
87
|
@meta_tags.delete(name)
|
|
94
88
|
end
|
|
95
89
|
|
|
96
90
|
# Deletes specified meta tags.
|
|
97
91
|
#
|
|
98
|
-
# @param [Array<String, Symbol>]
|
|
99
|
-
#
|
|
92
|
+
# @param names [Array<String, Symbol>] list of meta tags to delete.
|
|
100
93
|
def delete(*names)
|
|
101
94
|
names.each { |name| @meta_tags.delete(name) }
|
|
102
95
|
end
|
|
@@ -104,7 +97,6 @@ module MetaTags
|
|
|
104
97
|
# Extracts full page title and deletes all related meta tags.
|
|
105
98
|
#
|
|
106
99
|
# @return [String] page title.
|
|
107
|
-
#
|
|
108
100
|
def extract_full_title
|
|
109
101
|
site_title = extract(:site) || ""
|
|
110
102
|
title = extract_title
|
|
@@ -117,7 +109,6 @@ module MetaTags
|
|
|
117
109
|
# Extracts page title as an array of segments without site title and separators.
|
|
118
110
|
#
|
|
119
111
|
# @return [Array<String>] segments of page title.
|
|
120
|
-
#
|
|
121
112
|
def extract_title
|
|
122
113
|
title = extract(:title).presence
|
|
123
114
|
return [] unless title
|
|
@@ -132,7 +123,6 @@ module MetaTags
|
|
|
132
123
|
# Extracts title separator as a string.
|
|
133
124
|
#
|
|
134
125
|
# @return [String] page title separator.
|
|
135
|
-
#
|
|
136
126
|
def extract_separator
|
|
137
127
|
if meta_tags[:separator] == false
|
|
138
128
|
# Special case: if separator is hidden, do not display suffix/prefix
|
|
@@ -149,9 +139,9 @@ module MetaTags
|
|
|
149
139
|
|
|
150
140
|
# Extracts noindex settings as a Hash mapping noindex tag name to value.
|
|
151
141
|
#
|
|
152
|
-
# @return [Hash
|
|
153
|
-
#
|
|
142
|
+
# @return [Hash{String => String}] noindex attributes.
|
|
154
143
|
def extract_robots
|
|
144
|
+
# @type var result: Hash[String, Array[String]]
|
|
155
145
|
result = Hash.new { |h, k| h[k] = [] }
|
|
156
146
|
|
|
157
147
|
[
|
|
@@ -171,9 +161,8 @@ module MetaTags
|
|
|
171
161
|
|
|
172
162
|
# Converts input hash to HashWithIndifferentAccess and renames :open_graph to :og.
|
|
173
163
|
#
|
|
174
|
-
# @param [Hash]
|
|
164
|
+
# @param meta_tags [Hash] list of meta tags.
|
|
175
165
|
# @return [ActiveSupport::HashWithIndifferentAccess] normalized meta tags list.
|
|
176
|
-
#
|
|
177
166
|
def normalize_open_graph(meta_tags)
|
|
178
167
|
meta_tags = meta_tags.with_indifferent_access
|
|
179
168
|
meta_tags[:og] = meta_tags.delete(:open_graph) if meta_tags.key?(:open_graph)
|
|
@@ -181,21 +170,19 @@ module MetaTags
|
|
|
181
170
|
end
|
|
182
171
|
|
|
183
172
|
# Extracts separator segment without deleting it from meta tags list.
|
|
184
|
-
# If the value is false, empty string will be returned.
|
|
173
|
+
# If the value is false, an empty string will be returned.
|
|
185
174
|
#
|
|
186
|
-
# @param [Symbol, String]
|
|
187
|
-
# @param [String] default
|
|
175
|
+
# @param name [Symbol, String] separator segment name.
|
|
176
|
+
# @param default [String] default value.
|
|
188
177
|
# @return [String] separator segment value.
|
|
189
|
-
#
|
|
190
178
|
def extract_separator_section(name, default)
|
|
191
179
|
(meta_tags[name] == false) ? "" : (meta_tags[name] || default)
|
|
192
180
|
end
|
|
193
181
|
|
|
194
|
-
# Extracts robots attribute (noindex, nofollow, etc)
|
|
195
|
-
#
|
|
196
|
-
# @param [String, Symbol] name noindex attribute name.
|
|
197
|
-
# @return [Array<String>] pair of noindex attribute name and value.
|
|
182
|
+
# Extracts a robots attribute name/value pair (noindex, nofollow, etc.).
|
|
198
183
|
#
|
|
184
|
+
# @param name [String, Symbol] noindex attribute name.
|
|
185
|
+
# @return [Array<String>] noindex attribute name and value pair.
|
|
199
186
|
def extract_robots_attribute(name)
|
|
200
187
|
noindex = extract(name)
|
|
201
188
|
noindex_name = (noindex.is_a?(String) || noindex.is_a?(Array)) ? noindex : "robots"
|
|
@@ -204,18 +191,32 @@ module MetaTags
|
|
|
204
191
|
[noindex_name, noindex_value]
|
|
205
192
|
end
|
|
206
193
|
|
|
194
|
+
# Appends resolved robots directives while preserving first-write priority.
|
|
195
|
+
#
|
|
196
|
+
# @param result [Hash{String => Array<String>}] robots directives grouped by tag name.
|
|
197
|
+
# @param attributes [Symbol, Array<Symbol>] robots attributes to resolve.
|
|
198
|
+
# @return [void]
|
|
207
199
|
def calculate_robots_attributes(result, attributes)
|
|
208
200
|
processed = Set.new
|
|
209
201
|
Array(attributes).each do |attribute|
|
|
202
|
+
# @type var attribute: String | Symbol
|
|
210
203
|
names, value = extract_robots_attribute(attribute)
|
|
211
204
|
next unless value
|
|
212
205
|
|
|
213
206
|
Array(names).each do |name|
|
|
207
|
+
# @type var name: String | Symbol
|
|
214
208
|
apply_robots_value(result, name, value, processed)
|
|
215
209
|
end
|
|
216
210
|
end
|
|
217
211
|
end
|
|
218
212
|
|
|
213
|
+
# Records a robots directive unless it has already been set for the same key.
|
|
214
|
+
#
|
|
215
|
+
# @param result [Hash{String => Array<String>}] robots directives grouped by tag name.
|
|
216
|
+
# @param name [String, Symbol] robots tag name.
|
|
217
|
+
# @param value [String] robots directive value.
|
|
218
|
+
# @param processed [Set<String>] names already recorded in this pass.
|
|
219
|
+
# @return [void]
|
|
219
220
|
def apply_robots_value(result, name, value, processed)
|
|
220
221
|
name = name.to_s
|
|
221
222
|
return if processed.include?(name)
|
data/lib/meta_tags/railtie.rb
CHANGED
data/lib/meta_tags/renderer.rb
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module MetaTags
|
|
4
|
-
# This class is used by MetaTags
|
|
4
|
+
# This class is used by the MetaTags gem to render HTML meta tags on a page.
|
|
5
5
|
class Renderer
|
|
6
6
|
attr_reader :meta_tags, :normalized_meta_tags
|
|
7
7
|
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
# @param [MetaTagsCollection] meta_tags meta tags object to render.
|
|
8
|
+
# Initializes a new instance of Renderer.
|
|
11
9
|
#
|
|
10
|
+
# @param meta_tags [MetaTagsCollection] meta tags object to render.
|
|
12
11
|
def initialize(meta_tags)
|
|
13
12
|
@meta_tags = meta_tags
|
|
14
13
|
@normalized_meta_tags = {}
|
|
@@ -16,7 +15,7 @@ module MetaTags
|
|
|
16
15
|
|
|
17
16
|
# Renders meta tags on the page.
|
|
18
17
|
#
|
|
19
|
-
# @param [ActionView::Base]
|
|
18
|
+
# @param view [ActionView::Base] Rails view object.
|
|
20
19
|
def render(view)
|
|
21
20
|
tags = [] # : Array[Tag]
|
|
22
21
|
|
|
@@ -43,8 +42,7 @@ module MetaTags
|
|
|
43
42
|
|
|
44
43
|
# Renders charset tag.
|
|
45
44
|
#
|
|
46
|
-
# @param [Array<Tag>]
|
|
47
|
-
#
|
|
45
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
48
46
|
def render_charset(tags)
|
|
49
47
|
charset = meta_tags.extract(:charset)
|
|
50
48
|
tags << Tag.new(:meta, charset: charset) if charset.present?
|
|
@@ -52,8 +50,7 @@ module MetaTags
|
|
|
52
50
|
|
|
53
51
|
# Renders title tag.
|
|
54
52
|
#
|
|
55
|
-
# @param [Array<Tag>]
|
|
56
|
-
#
|
|
53
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
57
54
|
def render_title(tags)
|
|
58
55
|
normalized_meta_tags[:title] = meta_tags.page_title
|
|
59
56
|
normalized_meta_tags[:site] = meta_tags[:site]
|
|
@@ -68,8 +65,7 @@ module MetaTags
|
|
|
68
65
|
|
|
69
66
|
# Renders icon(s) tag.
|
|
70
67
|
#
|
|
71
|
-
# @param [Array<Tag>]
|
|
72
|
-
#
|
|
68
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
73
69
|
def render_icon(tags)
|
|
74
70
|
icon = meta_tags.extract(:icon)
|
|
75
71
|
return unless icon
|
|
@@ -88,9 +84,9 @@ module MetaTags
|
|
|
88
84
|
# Renders meta tag with normalization (should have a corresponding normalize_
|
|
89
85
|
# method in TextNormalizer).
|
|
90
86
|
#
|
|
91
|
-
# @param [Array<Tag>]
|
|
87
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
88
|
+
# @param name [Symbol] meta tag name to normalize and render.
|
|
92
89
|
# @see TextNormalizer
|
|
93
|
-
#
|
|
94
90
|
def render_with_normalization(tags, name)
|
|
95
91
|
value = TextNormalizer.public_send(:"normalize_#{name}", meta_tags.extract(name))
|
|
96
92
|
normalized_meta_tags[name] = value
|
|
@@ -99,8 +95,7 @@ module MetaTags
|
|
|
99
95
|
|
|
100
96
|
# Renders noindex and nofollow meta tags.
|
|
101
97
|
#
|
|
102
|
-
# @param [Array<Tag>]
|
|
103
|
-
#
|
|
98
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
104
99
|
def render_noindex(tags)
|
|
105
100
|
meta_tags.extract_robots.each do |name, content|
|
|
106
101
|
tags << Tag.new(:meta, name: name, content: content) if content.present?
|
|
@@ -109,8 +104,7 @@ module MetaTags
|
|
|
109
104
|
|
|
110
105
|
# Renders refresh meta tag.
|
|
111
106
|
#
|
|
112
|
-
# @param [Array<Tag>]
|
|
113
|
-
#
|
|
107
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
114
108
|
def render_refresh(tags)
|
|
115
109
|
refresh = meta_tags.extract(:refresh)
|
|
116
110
|
tags << Tag.new(:meta, "http-equiv" => "refresh", :content => refresh.to_s) if refresh.present?
|
|
@@ -118,8 +112,7 @@ module MetaTags
|
|
|
118
112
|
|
|
119
113
|
# Renders alternate link tags.
|
|
120
114
|
#
|
|
121
|
-
# @param [Array<Tag>]
|
|
122
|
-
#
|
|
115
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
123
116
|
def render_alternate(tags)
|
|
124
117
|
alternate = meta_tags.extract(:alternate)
|
|
125
118
|
return unless alternate
|
|
@@ -135,10 +128,9 @@ module MetaTags
|
|
|
135
128
|
end
|
|
136
129
|
end
|
|
137
130
|
|
|
138
|
-
# Renders
|
|
139
|
-
#
|
|
140
|
-
# @param [Array<Tag>] tags a buffer object to store tag in.
|
|
131
|
+
# Renders the OpenSearch link tag.
|
|
141
132
|
#
|
|
133
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
142
134
|
def render_open_search(tags)
|
|
143
135
|
open_search = meta_tags.extract(:open_search)
|
|
144
136
|
return unless open_search
|
|
@@ -152,8 +144,7 @@ module MetaTags
|
|
|
152
144
|
|
|
153
145
|
# Renders links.
|
|
154
146
|
#
|
|
155
|
-
# @param [Array<Tag>]
|
|
156
|
-
#
|
|
147
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
157
148
|
def render_links(tags)
|
|
158
149
|
[:amphtml, :prev, :next, :image_src, :manifest].each do |tag_name|
|
|
159
150
|
href = meta_tags.extract(tag_name)
|
|
@@ -164,12 +155,11 @@ module MetaTags
|
|
|
164
155
|
end
|
|
165
156
|
end
|
|
166
157
|
|
|
167
|
-
# Renders canonical link
|
|
168
|
-
#
|
|
169
|
-
# @param [Array<Tag>] tags a buffer object to store tag in.
|
|
158
|
+
# Renders the canonical link.
|
|
170
159
|
#
|
|
160
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
171
161
|
def render_canonical_link(tags)
|
|
172
|
-
href = meta_tags.extract(:canonical) # extract, so
|
|
162
|
+
href = meta_tags.extract(:canonical) # extract, so it's not used anywhere else
|
|
173
163
|
return if MetaTags.config.skip_canonical_links_on_noindex && meta_tags[:noindex]
|
|
174
164
|
return if href.blank?
|
|
175
165
|
|
|
@@ -179,30 +169,28 @@ module MetaTags
|
|
|
179
169
|
|
|
180
170
|
# Renders complex hash objects.
|
|
181
171
|
#
|
|
182
|
-
# @param [Array<Tag>]
|
|
183
|
-
|
|
184
|
-
def render_hashes(tags, **opts)
|
|
172
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
173
|
+
def render_hashes(tags)
|
|
185
174
|
meta_tags.meta_tags.each_key do |property|
|
|
186
|
-
render_hash(tags, property
|
|
175
|
+
render_hash(tags, property)
|
|
187
176
|
end
|
|
188
177
|
end
|
|
189
178
|
|
|
190
179
|
# Renders a complex hash object by key.
|
|
191
180
|
#
|
|
192
|
-
# @param [Array<Tag>]
|
|
193
|
-
#
|
|
194
|
-
def render_hash(tags, key
|
|
181
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
182
|
+
# @param key [String, Symbol] top-level meta tag key.
|
|
183
|
+
def render_hash(tags, key)
|
|
195
184
|
data = meta_tags.meta_tags[key]
|
|
196
185
|
return unless data.is_a?(Hash)
|
|
197
186
|
|
|
198
|
-
process_hash(tags, key, data
|
|
187
|
+
process_hash(tags, key, data)
|
|
199
188
|
meta_tags.extract(key)
|
|
200
189
|
end
|
|
201
190
|
|
|
202
191
|
# Renders custom meta tags.
|
|
203
192
|
#
|
|
204
|
-
# @param [Array<Tag>]
|
|
205
|
-
#
|
|
193
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
206
194
|
def render_custom(tags)
|
|
207
195
|
meta_tags.meta_tags.each do |name, data|
|
|
208
196
|
Array(data).each do |val|
|
|
@@ -212,40 +200,39 @@ module MetaTags
|
|
|
212
200
|
end
|
|
213
201
|
end
|
|
214
202
|
|
|
215
|
-
# Recursive method to process all
|
|
216
|
-
#
|
|
217
|
-
# @param [Array<Tag>] tags a buffer object to store tag in.
|
|
218
|
-
# @param [String, Symbol] property a Hash or a String to render as meta tag.
|
|
219
|
-
# @param [Hash, Array, String, Symbol] content text content or a symbol reference to
|
|
220
|
-
# top-level meta tag.
|
|
203
|
+
# Recursive method to process all hashes and arrays in meta tags.
|
|
221
204
|
#
|
|
222
|
-
|
|
223
|
-
|
|
205
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
206
|
+
# @param property [String, Symbol] the meta tag property name.
|
|
207
|
+
# @param content [Hash, Array, String, Symbol] text content or a symbol
|
|
208
|
+
# reference to a top-level meta tag.
|
|
209
|
+
# @param itemprop [String, Symbol, nil] value of the itemprop attribute.
|
|
210
|
+
def process_tree(tags, property, content, itemprop: nil)
|
|
211
|
+
case content
|
|
224
212
|
when Hash
|
|
225
|
-
:
|
|
213
|
+
process_hash(tags, property, content, itemprop: itemprop)
|
|
226
214
|
when Array
|
|
227
|
-
:
|
|
215
|
+
process_array(tags, property, content, itemprop: itemprop)
|
|
228
216
|
else
|
|
229
|
-
|
|
230
|
-
:render_tag
|
|
217
|
+
render_tag(tags, property, content, itemprop: itemprop)
|
|
231
218
|
end
|
|
232
|
-
__send__(method, tags, property, content, itemprop: iprop, **opts)
|
|
233
219
|
end
|
|
234
220
|
|
|
235
|
-
# Recursive method to process a hash
|
|
221
|
+
# Recursive method to process a hash of meta tags.
|
|
236
222
|
#
|
|
237
|
-
# @param [Array<Tag>]
|
|
238
|
-
# @param [String, Symbol]
|
|
239
|
-
# @param [Hash]
|
|
240
|
-
#
|
|
241
|
-
def process_hash(tags, property, content,
|
|
242
|
-
|
|
223
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
224
|
+
# @param property [String, Symbol] the meta tag property name.
|
|
225
|
+
# @param content [Hash] nested meta tag attributes.
|
|
226
|
+
# @param itemprop [String, Symbol, nil] inherited itemprop value.
|
|
227
|
+
def process_hash(tags, property, content, itemprop: nil)
|
|
228
|
+
current_itemprop = content.delete(:itemprop)
|
|
243
229
|
content.each do |key, value|
|
|
244
230
|
if key.to_s == "_"
|
|
245
|
-
iprop = itemprop
|
|
246
231
|
key = property
|
|
232
|
+
next_itemprop = current_itemprop
|
|
247
233
|
else
|
|
248
234
|
key = "#{property}:#{key}"
|
|
235
|
+
next_itemprop = nil
|
|
249
236
|
end
|
|
250
237
|
|
|
251
238
|
normalized_value = if value.is_a?(Symbol)
|
|
@@ -253,44 +240,59 @@ module MetaTags
|
|
|
253
240
|
else
|
|
254
241
|
value
|
|
255
242
|
end
|
|
256
|
-
process_tree(tags, key, normalized_value,
|
|
243
|
+
process_tree(tags, key, normalized_value, itemprop: next_itemprop)
|
|
257
244
|
end
|
|
258
245
|
end
|
|
259
246
|
|
|
260
|
-
# Recursive method to process
|
|
261
|
-
#
|
|
262
|
-
# @param [Array<Tag>] tags a buffer object to store tag in.
|
|
263
|
-
# @param [String, Symbol] property a Hash or a String to render as meta tag.
|
|
264
|
-
# @param [Array] content array of nested meta tag attributes or values.
|
|
247
|
+
# Recursive method to process an array of meta tags.
|
|
265
248
|
#
|
|
266
|
-
|
|
267
|
-
|
|
249
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
250
|
+
# @param property [String, Symbol] the meta tag property name.
|
|
251
|
+
# @param content [Array] array of nested meta tag attributes or values.
|
|
252
|
+
# @param itemprop [String, Symbol, nil] value of the itemprop attribute.
|
|
253
|
+
def process_array(tags, property, content, itemprop: nil)
|
|
254
|
+
content.each { |value| process_tree(tags, property, value, itemprop: itemprop) }
|
|
268
255
|
end
|
|
269
256
|
|
|
270
|
-
#
|
|
271
|
-
#
|
|
272
|
-
# @param [Array<Tag>] tags a buffer object to store tag in.
|
|
273
|
-
# @param [String, Symbol] name a Hash or a String to render as meta tag.
|
|
274
|
-
# @param [String, Symbol] value text content or a symbol reference to
|
|
275
|
-
# top-level meta tag.
|
|
276
|
-
# @param [String, Symbol] itemprop value of the itemprop attribute.
|
|
257
|
+
# Renders a single meta tag.
|
|
277
258
|
#
|
|
259
|
+
# @param tags [Array<Tag>] a buffer object to store tags in.
|
|
260
|
+
# @param name [String, Symbol] the meta tag name.
|
|
261
|
+
# @param value [String, Symbol] text content or a symbol reference to a
|
|
262
|
+
# top-level meta tag.
|
|
263
|
+
# @param itemprop [String, Symbol, nil] value of the itemprop attribute.
|
|
278
264
|
def render_tag(tags, name, value, itemprop: nil)
|
|
279
265
|
name_key ||= configured_name_key(name)
|
|
280
266
|
tags << Tag.new(:meta, name_key => name.to_s, :content => value, :itemprop => itemprop) if value.present?
|
|
281
267
|
end
|
|
282
268
|
|
|
283
|
-
# Returns meta tag property name for a
|
|
269
|
+
# Returns the meta tag property name for a given meta tag based on the
|
|
284
270
|
# configured list of property tags in MetaTags::Configuration#property_tags.
|
|
285
271
|
#
|
|
286
|
-
# @param [String, Symbol]
|
|
272
|
+
# @param name [String, Symbol] tag key.
|
|
287
273
|
# @return [Symbol] meta tag attribute name (:property or :name).
|
|
288
|
-
#
|
|
289
274
|
def configured_name_key(name)
|
|
275
|
+
name = name.to_s
|
|
290
276
|
is_property_tag = MetaTags.config.property_tags.any? do |tag_name|
|
|
291
|
-
name
|
|
277
|
+
property_tag?(name, tag_name.to_s)
|
|
292
278
|
end
|
|
293
279
|
is_property_tag ? :property : :name
|
|
294
280
|
end
|
|
281
|
+
|
|
282
|
+
# Returns true when a configured property tag matches the exact meta tag
|
|
283
|
+
# name or the start of a colon-delimited namespace.
|
|
284
|
+
#
|
|
285
|
+
# @param name [String] rendered meta tag name.
|
|
286
|
+
# @param tag_name [String] configured property tag name or namespace prefix.
|
|
287
|
+
# @return [Boolean]
|
|
288
|
+
def property_tag?(name, tag_name)
|
|
289
|
+
return false unless name.start_with?(tag_name)
|
|
290
|
+
|
|
291
|
+
tag_name_length = tag_name.bytesize
|
|
292
|
+
return true if name.bytesize == tag_name_length
|
|
293
|
+
return true if tag_name.getbyte(tag_name_length - 1) == 58
|
|
294
|
+
|
|
295
|
+
name.getbyte(tag_name_length) == 58
|
|
296
|
+
end
|
|
295
297
|
end
|
|
296
298
|
end
|
data/lib/meta_tags/tag.rb
CHANGED
|
@@ -7,26 +7,28 @@ module MetaTags
|
|
|
7
7
|
|
|
8
8
|
# Initializes a new instance of Tag class.
|
|
9
9
|
#
|
|
10
|
-
# @param [String, Symbol]
|
|
11
|
-
# @param [Hash]
|
|
12
|
-
#
|
|
10
|
+
# @param name [String, Symbol] HTML tag name.
|
|
11
|
+
# @param attributes [Hash] list of HTML tag attributes.
|
|
13
12
|
def initialize(name, attributes = {})
|
|
14
13
|
@name = name.to_s
|
|
15
14
|
@attributes = attributes
|
|
16
15
|
end
|
|
17
16
|
|
|
18
|
-
#
|
|
17
|
+
# Renders the tag in a Rails view.
|
|
19
18
|
#
|
|
20
|
-
# @param [ActionView::Base]
|
|
19
|
+
# @param view [ActionView::Base] instance of a Rails view.
|
|
21
20
|
# @return [String] HTML string for the tag.
|
|
22
|
-
#
|
|
23
21
|
def render(view)
|
|
24
|
-
view.tag(name,
|
|
22
|
+
view.tag(name, serialize_iso8601_attributes(attributes), MetaTags.config.open_meta_tags?)
|
|
25
23
|
end
|
|
26
24
|
|
|
27
25
|
protected
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
# Serializes attribute values that support ISO 8601 formatting.
|
|
28
|
+
#
|
|
29
|
+
# @param attributes [Hash] HTML tag attributes.
|
|
30
|
+
# @return [Hash] normalized HTML tag attributes.
|
|
31
|
+
def serialize_iso8601_attributes(attributes)
|
|
30
32
|
attributes.each do |key, value|
|
|
31
33
|
attributes[key] = value.iso8601 if value.respond_to?(:iso8601)
|
|
32
34
|
end
|