module Storefront module Helpers module HeadHelper def meta_tags(*args) options = args.extract_options! options[:site] ||= args.shift result = [] storefront_config.meta_tags.each do |meta_tag_name| if options[meta_tag_name].present? result << meta_tag(meta_tag_name, options[meta_tag_name]) end end result.join("\n") end alias_method :meta, :meta_tags def meta_tag(name, content = '') capture_haml { haml_tag :meta, :name => name, :content => content } end def title(*args) @content_for_title = *args end def title_tag(*args) options = args.extract_options! # Title title = @content_for_title if title.present? options.merge!(title.extract_options!) title = @content_for_title.shift || options[:title] end if options[:lowercase] === true title = title.downcase unless title.blank? end site = options.has_key?(:site) ? options[:site] : args.first # Prefix (leading space) if options[:prefix] prefix = options[:prefix] elsif options[:prefix] === false prefix = '' else prefix = ' ' end # Separator unless options[:separator].blank? separator = options[:separator] else separator = '|' end # Suffix (trailing space) if options[:suffix] suffix = options[:suffix] elsif options[:suffix] === false suffix = '' else suffix = ' ' end # title if title.blank? result = content_tag :title, site else title = normalize_title(title) title = [site] + title if site title.reverse! unless options[:reverse] === false sep = prefix + separator + suffix result = content_tag(:title, title.join(sep)) end result end def description(string) @content_for_description = string end def description_tag(default='') content = normalize_description(@content_for_description || default) meta_tag(:description, content) unless content.blank? end def keywords(string) @content_for_keywords = string end def keywords_tag(default = '') content = normalize_keywords(@content_for_keywords || default) meta_tag(:keywords, content) unless content.blank? end def copyright(string) @content_for_copyright = string end def copyright_tag(default='') content = @content_for_copyright || default meta_tag(:copyright, content) unless content.blank? end def robots(*args) content_for(:robots, args.join(", ")) end def robots_tag(*args) options = args.extract_options! noindex_name = tags[:noindex].is_a?(String) ? tags[:noindex] : 'robots' nofollow_name = tags[:nofollow].is_a?(String) ? tags[:nofollow] : 'robots' if noindex_name == nofollow_name content = [(tags[:noindex] ? 'noindex' : nil), (tags[:nofollow] ? 'nofollow' : nil)].compact.join(', ') unless content.blank? result << "\n" result << meta_tag(noindex_name, content) end else if tags[:noindex] result << "\n" result << meta_tag(noindex_name, "noindex") end if tags[:nofollow] result << "\n" result << meta_tag(nofollow_name, "nofollow") end end return result end def canonical_link_tag(path) link_tag(:rel => :canonical, :href => path) end def http_meta_tag(name, content) capture_haml { haml_tag :meta, :"http-equiv" => name, :content => content } end # @example Redirect to the site home page in 20 seconds. # # refresh_meta_tag root_url, :in => 20.seconds def refresh_meta_tag(path, options = {}) http_meta_tag("refresh", "#{options[:in].to_i};url=#{path}") end def copyright(from) now = Time.now.year now.eql?(from) ? now : "#{from} - #{now}" end # Apple # http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html # Facebook (and opengraph) # http://developers.facebook.com/docs/opengraph#types # http://wiki.developers.facebook.com/index.php/Facebook_Share/Specifying_Meta_Tags def snapshot_link_tag(href) link_tag(:rel => "image_src", :href => href) end def html4_content_type_tag(charset = "UTF-8", type = "text/html") http_meta_tag("Content-Type", "#{type}; charset=#{charset}") end def html5_content_type_tag(charset = "UTF-8") capture_haml { haml_tag :meta, "charset" => charset } end alias_method :content_type_tag, :html5_content_type_tag def search_link_tag(href, title) link_tag(:rel => "search", :type => "application/opensearchdescription+xml", :href => href, :title => title) end def favicon_link_tag(favicon = "/favicon.ico") link_tag(:rel => "shortcut icon", :href => favicon, :type => "image/x-icon") end def link_tag(options = {}) capture_haml { haml_tag :link, options } end def ie_application_meta_tags(title, options = {}) result = [] result << meta_tag("application-name", title) result << meta_tag("msapplication-tooltip", options[:tooltip]) if options[:tooltip].present? result << meta_tag("msapplication-starturl", options[:url]) if options[:url].present? if options[:width].present? && options[:height].present? result << meta_tag("msapplication-window", "width=#{options[:width]};height=#{options[:height]}") end result << meta_tag("msapplication-navbutton-color", options[:color]) if options[:color].present? result.join("\n") end def ie_task_meta_tag(name, path, icon = nil) content = [] content << "name=#{name}" content << "uri=#{path}" content << "icon-uri=#{icon}" if icon meta_tag("msapplication-task", content.join(";")) end def apple_meta_tags(options = {}) result = [] result << apple_viewport_meta_tag(options) result << apple_full_screen_meta_tag(options[:full_screen]) if options.has_key?(:full_screen) result << apple_mobile_compatible_meta_tag(options[:mobile]) if options.has_key?(:mobile) result.join end # http://www.html5rocks.com/en/mobile/mobifying.html def apple_viewport_meta_tag(options = {}) viewport = [] viewport << "width=#{options[:width]}" if options[:width].present? viewport << "height=#{options[:height]}" if options[:height].present? viewport << "initial-scale=#{options[:scale] || 1.0}" viewport << "minimum-scale=#{options[:min]}" if options[:min].present? viewport << "maximum-scale=#{options[:max]}" if options[:max].present? viewport << "user-scalable=#{options[:scalable] == true ? "yes" : "no"}" if options.has_key?(:scalable) meta_tag("viewport", viewport.join(", ")) end def apple_full_screen_meta_tag(value) meta_tag("apple-touch-fullscreen", value == true ? "yes" : "no") end def apple_mobile_compatible_meta_tag(value) meta_tag("apple-mobile-web-app-capable", value == true ? "yes" : "no") end def apple_touch_icon_link_tag(path, options = {}) rel = ["apple-touch-icon"] rel << "#{options[:size]}x#{options[:size]}" if options[:size].present? rel << "precomposed" if options[:precomposed] link_tag(:rel => rel.join("-"), :href => path) end def apple_touch_icon_link_tags(path, *sizes) options = sizes.extract_options! sizes.map { |size| apple_touch_icon_link_tag(path, options.merge(:size => size)) }.join end def open_graph_meta_tags(options = {}) result = [] result << open_graph_meta_tag("og:title", options[:title]) if options[:title] result << open_graph_meta_tag("og:type", options[:type]) if options[:type] result << open_graph_meta_tag("og:image", options[:image]) if options[:image] result << open_graph_meta_tag("og:site_name", options[:site]) if options[:site] result << open_graph_meta_tag("og:description", options[:description]) if options[:description] result << open_graph_meta_tag("og:email", options[:email]) if options[:email] result << open_graph_meta_tag("og:phone_number", options[:phone]) if options[:phone] result << open_graph_meta_tag("og:fax_number", options[:fax]) if options[:fax] result << open_graph_meta_tag("og:latitude", options[:lat]) if options[:lat] result << open_graph_meta_tag("og:longitude", options[:lng]) if options[:lng] result << open_graph_meta_tag("og:street-address", options[:street]) if options[:street] result << open_graph_meta_tag("og:locality", options[:city]) if options[:city] result << open_graph_meta_tag("og:region", options[:state]) if options[:state] result << open_graph_meta_tag("og:postal-code", options[:zip]) if options[:zip] result << open_graph_meta_tag("og:country-name", options[:country]) if options[:country] result.join("\n") end def open_graph_meta_tag(property, content) capture_haml { haml_tag :meta, :property => property, :content => content } end # first, last, next, prev def pagination_meta_tags(options = {}) end private def normalize_title(title) if title.is_a? String title = [title] end title.map { |t| h(strip_tags(t)) } end def normalize_description(description) return '' unless description truncate(strip_tags(description).gsub(/\s+/, ' '), :length => 200) end def normalize_keywords(keywords) return '' unless keywords keywords = keywords.flatten.join(', ') if keywords.is_a?(Array) strip_tags(keywords).mb_chars.downcase end end end end