Class: MetaTags::Renderer

Inherits:
Object show all
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

Instance Method Summary collapse

Constructor Details

#initialize(meta_tags) ⇒ Renderer

Initialized a new instance of Renderer.

Parameters:



12
13
14
15
# File 'lib/meta_tags/renderer.rb', line 12

def initialize(meta_tags)
  @meta_tags = meta_tags
  @normalized_meta_tags = {}
end

Instance Attribute Details

#meta_tags ⇒ MetaTagsCollection (readonly)

Returns the value of attribute meta_tags.

Returns:



6
7
8
# File 'lib/meta_tags/renderer.rb', line 6

def meta_tags
  @meta_tags
end

#normalized_meta_tags ⇒ Hash[Symbol, meta_value] (readonly)

Returns the value of attribute normalized_meta_tags.

Returns:

  • (Hash[Symbol, meta_value])


6
7
8
# File 'lib/meta_tags/renderer.rb', line 6

def normalized_meta_tags
  @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.

Parameters:

  • name (String, Symbol) —

    tag key.

Returns:

  • (Symbol) —

    meta tag attribute name (:property or :name).



285
286
287
288
289
290
# File 'lib/meta_tags/renderer.rb', line 285

def configured_name_key(name)
  is_property_tag = MetaTags.config.property_tags.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

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.

  • property (String, Symbol) —

    a Hash or a String to render as meta tag.

  • content (Array) —

    array of nested meta tag attributes or values.

  • opts (Object)


262
263
264
# File 'lib/meta_tags/renderer.rb', line 262

def process_array(tags, property, content, **opts)
  content.each { |v| process_tree(tags, 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

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.

  • property (String, Symbol) —

    a Hash or a String to render as meta tag.

  • content (Hash) —

    nested meta tag attributes.

  • opts (Object)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/meta_tags/renderer.rb', line 237

def process_hash(tags, 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.kind_of?(Symbol)
                         normalized_meta_tags[value]
                       else
                         value
                       end
    process_tree(tags, 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.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.

  • property (String, Symbol) —

    a Hash or a String to render as meta tag.

  • content (Hash, Array, String, Symbol) —

    text content or a symbol reference to

  • itemprop: (meta_key? itemprop) (defaults to: nil)
  • opts (Object)


218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/meta_tags/renderer.rb', line 218

def process_tree(tags, property, content, itemprop: nil, **opts)
  method = case content
           when Hash
             :process_hash
           when Array
             :process_array
           else
             iprop = itemprop
             :render_tag
           end
  __send__(method, tags, property, content, itemprop: iprop, **opts)
end

#render(view) ⇒ String

Renders meta tags on the page.

Parameters:

  • view (ActionView::Base) —

    Rails view object.

Returns:

  • (String)


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)
  tags = []

  render_charset(tags)
  render_title(tags)
  render_icon(tags)
  render_with_normalization(tags, :description)
  render_with_normalization(tags, :keywords)
  render_refresh(tags)
  render_canonical_link(tags)
  render_noindex(tags)
  render_alternate(tags)
  render_open_search(tags)
  render_links(tags)

  render_hashes(tags)
  render_custom(tags)

  tags.tap(&:compact!).map! { |tag| tag.render(view) }
  view.safe_join tags, MetaTags.config.minify_output ? "" : "\n"
end

#render_alternate(tags) ⇒ void

This method returns an undefined value.

Renders alternate link tags.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/meta_tags/renderer.rb', line 119

def render_alternate(tags)
  alternate = meta_tags.extract(:alternate)
  return unless alternate

  if alternate.kind_of?(Hash)
    alternate.each do |hreflang, href|
      tags << Tag.new(:link, rel: 'alternate', href: href, hreflang: hreflang) if href.present?
    end
  elsif alternate.kind_of?(Array)
    alternate.each do |link_params|
      tags << Tag.new(:link, { rel: 'alternate' }.with_indifferent_access.merge(link_params))
    end
  end
end

This method returns an undefined value.

Renders canonical link

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



167
168
169
170
171
172
173
174
# File 'lib/meta_tags/renderer.rb', line 167

def render_canonical_link(tags)
  href = meta_tags.extract(:canonical) # extract, so its not used anywhere else
  return if MetaTags.config.skip_canonical_links_on_noindex && meta_tags[:noindex]
  return if href.blank?

  @normalized_meta_tags[:canonical] = href
  tags << Tag.new(:link, rel: :canonical, href: href)
end

#render_charset(tags) ⇒ void

This method returns an undefined value.

Renders charset tag.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



48
49
50
51
# File 'lib/meta_tags/renderer.rb', line 48

def render_charset(tags)
  charset = meta_tags.extract(:charset)
  tags << Tag.new(:meta, charset: charset) if charset.present?
end

#render_custom(tags) ⇒ void

This method returns an undefined value.

Renders custom meta tags.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



202
203
204
205
206
207
208
209
# File 'lib/meta_tags/renderer.rb', line 202

def render_custom(tags)
  meta_tags.meta_tags.each do |name, data|
    Array(data).each do |val|
      tags << Tag.new(:meta, configured_name_key(name) => name, content: val)
    end
    meta_tags.extract(name)
  end
end

#render_hash(tags, key, **opts) ⇒ void

This method returns an undefined value.

Renders a complex hash object by key.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.

  • key (Object)
  • opts (Object)


190
191
192
193
194
195
196
# File 'lib/meta_tags/renderer.rb', line 190

def render_hash(tags, key, **opts)
  data = meta_tags.meta_tags[key]
  return unless data.kind_of?(Hash)

  process_hash(tags, key, data, **opts)
  meta_tags.extract(key)
end

#render_hashes(tags, **opts) ⇒ void

This method returns an undefined value.

Renders complex hash objects.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.

  • opts (Object)


180
181
182
183
184
# File 'lib/meta_tags/renderer.rb', line 180

def render_hashes(tags, **opts)
  meta_tags.meta_tags.each_key do |property|
    render_hash(tags, property, **opts)
  end
end

#render_icon(tags) ⇒ void

This method returns an undefined value.

Renders icon(s) tag.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/meta_tags/renderer.rb', line 69

def render_icon(tags)
  icon = meta_tags.extract(:icon)
  return unless icon

  # String? Value is an href
  icon = [{ href: icon }] if icon.kind_of?(String)
  # Hash? Single icon instead of a list of icons
  icon = [icon] if icon.kind_of?(Hash)

  icon.each do |icon_params|
    icon_params = { rel: 'icon', type: 'image/x-icon' }.with_indifferent_access.merge(icon_params)
    tags << Tag.new(:link, icon_params)
  end
end

This method returns an undefined value.

Renders links.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



153
154
155
156
157
158
159
160
161
# File 'lib/meta_tags/renderer.rb', line 153

def render_links(tags)
  [ :amphtml, :prev, :next, :image_src, :manifest ].each do |tag_name|
    href = meta_tags.extract(tag_name)
    if href.present?
      @normalized_meta_tags[tag_name] = href
      tags << 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.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



100
101
102
103
104
# File 'lib/meta_tags/renderer.rb', line 100

def render_noindex(tags)
  meta_tags.extract_robots.each do |name, content|
    tags << 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.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



138
139
140
141
142
143
144
145
146
147
# File 'lib/meta_tags/renderer.rb', line 138

def render_open_search(tags)
  open_search = meta_tags.extract(:open_search)
  return unless open_search

  href = open_search[:href]
  title = open_search[:title]

  type = "application/opensearchdescription+xml"
  tags << 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.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



110
111
112
113
# File 'lib/meta_tags/renderer.rb', line 110

def render_refresh(tags)
  refresh = meta_tags.extract(:refresh)
  tags << 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.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.

  • name (String, Symbol) —

    a Hash or a String to render as meta tag.

  • value (String, Symbol) —

    text content or a symbol reference to

  • itemprop (String, Symbol) (defaults to: nil) —

    value of the itemprop attribute.

  • itemprop: (meta_key? itemprop) (defaults to: nil)


274
275
276
277
# File 'lib/meta_tags/renderer.rb', line 274

def render_tag(tags, name, value, itemprop: nil)
  name_key ||= configured_name_key(name)
  tags << 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.

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.



57
58
59
60
61
62
63
# File 'lib/meta_tags/renderer.rb', line 57

def render_title(tags)
  normalized_meta_tags[:title] = meta_tags.page_title
  normalized_meta_tags[:site] = meta_tags[:site]
  title = meta_tags.extract_full_title
  normalized_meta_tags[:full_title] = title
  tags << ContentTag.new(:title, content: title) if title.present?
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).

Parameters:

  • tags (Array<Tag>) —

    a buffer object to store tag in.

  • name (Symbol)

See Also:



90
91
92
93
94
# File 'lib/meta_tags/renderer.rb', line 90

def render_with_normalization(tags, name)
  value = TextNormalizer.public_send("normalize_#{name}", meta_tags.extract(name))
  normalized_meta_tags[name] = value
  tags << Tag.new(:meta, name: name, content: value) if value.present?
end