Module: Middleman::OGP::Helper

Includes:
Padrino::Helpers::TagHelpers
Defined in:
lib/middleman-ogp/extension.rb

Overview

Middleman::OGP::Helper

Class Method Summary collapse

Class Method Details

.og_tag(key, obj = nil, prefix = 'og', &block) ⇒ Object

rubocop:disable Metrics/MethodLength



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/middleman-ogp/extension.rb', line 135

def self.og_tag(key, obj = nil, prefix = 'og', &block) # rubocop:disable Metrics/MethodLength
  case key
  when String, Symbol
    key = [key]
  when Hash
    prefix = obj if obj
    obj = key
    key = []
  end
  case obj
  when Hash
    obj.map do |k, v|
      og_tag(k.to_s.empty? ? key.dup : (key.dup << k.to_s), v, prefix, &block)
    end.join("\n")
  when Array
    obj.map do |v|
      og_tag(key, v, prefix, &block)
    end.join("\n")
  else
    if obj.respond_to?(:value)
      value = obj.value
      raise 'Unknown value' unless value.is_a?(Hash)

      value.map do |k, v|
        og_tag(k.to_s.empty? ? key.dup : (key.dup << k.to_s), v, prefix, &block)
      end.join("\n")
    else
      name = [prefix].concat(key).join(':')
      value = obj.to_s
      if Middleman::OGP::Helper.image_base_url && name == 'og:image' && !%r{^https?://}.match(value)
        value = URI.join(Middleman::OGP::Helper.image_base_url, value)
      end
      block.call name, value
    end
  end
end

.ogp_tags(opts = {}, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/middleman-ogp/extension.rb', line 113

def self.ogp_tags(opts = {}, &block) # rubocop:disable Metrics/MethodLength
  opts ||= {}
  options = (namespaces.respond_to?(:to_h) ? namespaces.to_h : namespaces || {}).dup
  opts.stringify_keys!
  options.stringify_keys!
  options = options.deep_merge4(opts) do |_k, old_value, new_value|
    if old_value.is_a?(Hash)
      if new_value.is_a? Hash
        old_value.deep_merge4 new_value
      else
        old_value[''] = new_value
        old_value
      end
    else
      new_value
    end
  end.symbolize_keys
  options.map  do |k, v|
    og_tag([], v, k, &block)
  end.join("\n")
end