Class: MetaTags::Renderer
- Defined in:
- lib/meta_tags/renderer.rb,
sig/lib/meta_tags/renderer.rbs
Overview
This class is used by MetaTags gems to render HTML meta tags into page.
Instance Attribute Summary collapse
-
#meta_tags ⇒ MetaTagsCollection
readonly
Returns the value of attribute meta_tags.
-
#normalized_meta_tags ⇒ Hash[Symbol, meta_value]
readonly
Returns the value of attribute normalized_meta_tags.
Instance Method Summary collapse
-
#configured_name_key(name) ⇒ Symbol
Returns meta tag property name for a give meta tag based on the configured list of property tags in MetaTags::Configuration#property_tags.
-
#initialize(meta_tags) ⇒ Renderer
constructor
Initialized a new instance of Renderer.
-
#process_array(tags, property, content, **opts) ⇒ void
Recursive method to process a hash with meta tags.
-
#process_hash(tags, property, content, **opts) ⇒ void
Recursive method to process a hash with meta tags.
-
#process_tree(tags, property, content, itemprop: nil, **opts) ⇒ void
Recursive method to process all the hashes and arrays on meta tags.
-
#render(view) ⇒ String
Renders meta tags on the page.
-
#render_alternate(tags) ⇒ void
Renders alternate link tags.
-
#render_canonical_link(tags) ⇒ void
Renders canonical link.
-
#render_charset(tags) ⇒ void
Renders charset tag.
-
#render_custom(tags) ⇒ void
Renders custom meta tags.
-
#render_hash(tags, key, **opts) ⇒ void
Renders a complex hash object by key.
-
#render_hashes(tags, **opts) ⇒ void
Renders complex hash objects.
-
#render_icon(tags) ⇒ void
Renders icon(s) tag.
-
#render_links(tags) ⇒ void
Renders links.
-
#render_noindex(tags) ⇒ void
Renders noindex and nofollow meta tags.
-
#render_open_search(tags) ⇒ void
Renders open_search link tag.
-
#render_refresh(tags) ⇒ void
Renders refresh meta tag.
-
#render_tag(tags, name, value, itemprop: nil) ⇒ void
Recursive method to process a hash with meta tags.
-
#render_title(tags) ⇒ void
Renders title tag.
-
#render_with_normalization(tags, name) ⇒ void
Renders meta tag with normalization (should have a corresponding normalize_ method in TextNormalizer).
Constructor Details
#initialize(meta_tags) ⇒ Renderer
Initialized a new instance of Renderer.
12 13 14 15 |
# File 'lib/meta_tags/renderer.rb', line 12 def initialize() @meta_tags = @normalized_meta_tags = {} end |
Instance Attribute Details
#meta_tags ⇒ MetaTagsCollection (readonly)
Returns the value of attribute meta_tags.
6 7 8 |
# File 'lib/meta_tags/renderer.rb', line 6 def @meta_tags end |
#normalized_meta_tags ⇒ Hash[Symbol, meta_value] (readonly)
Returns the value of attribute normalized_meta_tags.
6 7 8 |
# File 'lib/meta_tags/renderer.rb', line 6 def @normalized_meta_tags end |
Instance Method Details
#configured_name_key(name) ⇒ Symbol
Returns meta tag property name for a give meta tag based on the configured list of property tags in MetaTags::Configuration#property_tags.
289 290 291 292 293 294 |
# File 'lib/meta_tags/renderer.rb', line 289 def configured_name_key(name) is_property_tag = MetaTags.config..any? do |tag_name| name.to_s.match(/^#{Regexp.escape(tag_name.to_s)}\b/) end is_property_tag ? :property : :name end |
#process_array(tags, property, content, **opts) ⇒ void
This method returns an undefined value.
Recursive method to process a hash with meta tags
266 267 268 |
# File 'lib/meta_tags/renderer.rb', line 266 def process_array(, property, content, **opts) content.each { |v| process_tree(, property, v, **opts) } end |
#process_hash(tags, property, content, **opts) ⇒ void
This method returns an undefined value.
Recursive method to process a hash with meta tags
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/meta_tags/renderer.rb', line 241 def process_hash(, property, content, **opts) itemprop = content.delete(:itemprop) content.each do |key, value| if key.to_s == "_" iprop = itemprop key = property else key = "#{property}:#{key}" end normalized_value = if value.is_a?(Symbol) [value] else value end process_tree(, key, normalized_value, **opts.merge(itemprop: iprop)) end end |
#process_tree(tags, property, content, itemprop: nil, **opts) ⇒ void
This method returns an undefined value.
Recursive method to process all the hashes and arrays on meta tags
top-level meta tag.
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/meta_tags/renderer.rb', line 222 def process_tree(, property, content, itemprop: nil, **opts) method = case content when Hash :process_hash when Array :process_array else iprop = itemprop :render_tag end __send__(method, , property, content, itemprop: iprop, **opts) end |
#render(view) ⇒ String
Renders meta tags on the page.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/meta_tags/renderer.rb', line 20 def render(view) = [] # : Array[Tag] render_charset() render_title() render_icon() render_with_normalization(, :description) render_with_normalization(, :keywords) render_refresh() render_canonical_link() render_noindex() render_alternate() render_open_search() render_links() render_hashes() render_custom() = .tap(&:compact!).map { |tag| tag.render(view) } view.safe_join , MetaTags.config.minify_output ? "" : "\n" end |
#render_alternate(tags) ⇒ void
This method returns an undefined value.
Renders alternate link tags.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/meta_tags/renderer.rb', line 123 def render_alternate() alternate = .extract(:alternate) return unless alternate if alternate.is_a?(Hash) alternate.each do |hreflang, href| << Tag.new(:link, rel: "alternate", href: href, hreflang: hreflang) if href.present? end elsif alternate.is_a?(Array) alternate.each do |link_params| << Tag.new(:link, {rel: "alternate"}.with_indifferent_access.merge(link_params)) end end end |
#render_canonical_link(tags) ⇒ void
This method returns an undefined value.
Renders canonical link
171 172 173 174 175 176 177 178 |
# File 'lib/meta_tags/renderer.rb', line 171 def render_canonical_link() href = .extract(:canonical) # extract, so its not used anywhere else return if MetaTags.config.skip_canonical_links_on_noindex && [:noindex] return if href.blank? @normalized_meta_tags[:canonical] = href << Tag.new(:link, rel: :canonical, href: href) end |
#render_charset(tags) ⇒ void
This method returns an undefined value.
Renders charset tag.
48 49 50 51 |
# File 'lib/meta_tags/renderer.rb', line 48 def render_charset() charset = .extract(:charset) << Tag.new(:meta, charset: charset) if charset.present? end |
#render_custom(tags) ⇒ void
This method returns an undefined value.
Renders custom meta tags.
206 207 208 209 210 211 212 213 |
# File 'lib/meta_tags/renderer.rb', line 206 def render_custom() ..each do |name, data| Array(data).each do |val| << Tag.new(:meta, configured_name_key(name) => name, :content => val) end .extract(name) end end |
#render_hash(tags, key, **opts) ⇒ void
This method returns an undefined value.
Renders a complex hash object by key.
194 195 196 197 198 199 200 |
# File 'lib/meta_tags/renderer.rb', line 194 def render_hash(, key, **opts) data = .[key] return unless data.is_a?(Hash) process_hash(, key, data, **opts) .extract(key) end |
#render_hashes(tags, **opts) ⇒ void
This method returns an undefined value.
Renders complex hash objects.
184 185 186 187 188 |
# File 'lib/meta_tags/renderer.rb', line 184 def render_hashes(, **opts) ..each_key do |property| render_hash(, property, **opts) end end |
#render_icon(tags) ⇒ void
This method returns an undefined value.
Renders icon(s) tag.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/meta_tags/renderer.rb', line 73 def render_icon() icon = .extract(:icon) return unless icon # String? Value is an href icon = [{href: icon}] if icon.is_a?(String) # Hash? Single icon instead of a list of icons icon = [icon] if icon.is_a?(Hash) icon.each do |icon_params| icon_params = {rel: "icon", type: "image/x-icon"}.with_indifferent_access.merge(icon_params) << Tag.new(:link, icon_params) end end |
#render_links(tags) ⇒ void
This method returns an undefined value.
Renders links.
157 158 159 160 161 162 163 164 165 |
# File 'lib/meta_tags/renderer.rb', line 157 def render_links() [:amphtml, :prev, :next, :image_src, :manifest].each do |tag_name| href = .extract(tag_name) if href.present? @normalized_meta_tags[tag_name] = href << Tag.new(:link, rel: tag_name, href: href) end end end |
#render_noindex(tags) ⇒ void
This method returns an undefined value.
Renders noindex and nofollow meta tags.
104 105 106 107 108 |
# File 'lib/meta_tags/renderer.rb', line 104 def render_noindex() .extract_robots.each do |name, content| << Tag.new(:meta, name: name, content: content) if content.present? end end |
#render_open_search(tags) ⇒ void
This method returns an undefined value.
Renders open_search link tag.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/meta_tags/renderer.rb', line 142 def render_open_search() open_search = .extract(:open_search) return unless open_search href = open_search[:href] title = open_search[:title] type = "application/opensearchdescription+xml" << Tag.new(:link, rel: "search", type: type, href: href, title: title) if href.present? end |
#render_refresh(tags) ⇒ void
This method returns an undefined value.
Renders refresh meta tag.
114 115 116 117 |
# File 'lib/meta_tags/renderer.rb', line 114 def render_refresh() refresh = .extract(:refresh) << Tag.new(:meta, "http-equiv" => "refresh", :content => refresh.to_s) if refresh.present? end |
#render_tag(tags, name, value, itemprop: nil) ⇒ void
This method returns an undefined value.
Recursive method to process a hash with meta tags
top-level meta tag.
278 279 280 281 |
# File 'lib/meta_tags/renderer.rb', line 278 def render_tag(, name, value, itemprop: nil) name_key ||= configured_name_key(name) << Tag.new(:meta, name_key => name.to_s, :content => value, :itemprop => itemprop) if value.present? end |
#render_title(tags) ⇒ void
This method returns an undefined value.
Renders title tag.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/meta_tags/renderer.rb', line 57 def render_title() [:title] = .page_title [:site] = [:site] title = .extract_full_title [:full_title] = title default_attributes = MetaTags.config.title_tag_attributes || {} if title.present? << ContentTag.new(:title, {content: title}.with_defaults(default_attributes)) end end |
#render_with_normalization(tags, name) ⇒ void
This method returns an undefined value.
Renders meta tag with normalization (should have a corresponding normalize_ method in TextNormalizer).
94 95 96 97 98 |
# File 'lib/meta_tags/renderer.rb', line 94 def render_with_normalization(, name) value = TextNormalizer.public_send(:"normalize_#{name}", .extract(name)) [name] = value << Tag.new(:meta, name: name, content: value) if value.present? end |