class Bunto::SeoTag::Drop

Constants

FORMAT_STRING_METHODS
HOMEPAGE_OR_ABOUT_REGEX
TITLE_SEPARATOR

Attributes

context[R]

Public Class Methods

new(text, context) click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 12
def initialize(text, context)
  @obj = {}
  @mutations = {}
  @text    = text
  @context = context
end

Public Instance Methods

author() click to toggle source

Returns a nil or a hash representing the author Author name will be pulled from:

  1. The `author` key, if the key is a string

  2. The first author in the `authors` key

  3. The `author` key in the site config

If the result from the name search is a string, we'll also check to see if the author exists in `site.data.authors`

# File lib/bunto-seo-tag/drop.rb, line 84
def author
  @author ||= begin
    return if author_string_or_hash.to_s.empty?

    author = if author_string_or_hash.is_a?(String)
               author_hash(author_string_or_hash)
             else
               author_string_or_hash
             end

    author["twitter"] ||= author["name"]
    author["twitter"].delete! "@" if author["twitter"]
    author.to_liquid
  end
end
canonical_url() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 184
def canonical_url
  @canonical_url ||= begin
    if page["canonical_url"].to_s.empty?
      filters.absolute_url(page["url"]).to_s.gsub(%r!/index\.html$!, "/")
    else
      page["canonical_url"]
    end
  end
end
date_modified() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 100
def date_modified
  @date_modified ||= begin
    date = if page_seo["date_modified"]
             page_seo["date_modified"]
           elsif page["last_modified_at"]
             page["last_modified_at"].to_liquid
           else
             page["date"]
           end
    filters.date_to_xmlschema(date) if date
  end
end
date_published() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 113
def date_published
  @date_published ||= filters.date_to_xmlschema(page["date"]) if page["date"]
end
description() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 69
def description
  @description ||= begin
    format_string(page["description"] || page["excerpt"]) || site_description
  end
end
image() click to toggle source

Returns nil or a hash representing the page image The image hash will always contain a path, pulled from:

  1. The `image` key if it's a string

  2. The `image.path` key if it's a hash

  3. The `image.facebook` key

  4. The `image.twitter` key

The resulting path is always an absolute URL

# File lib/bunto-seo-tag/drop.rb, line 161
def image
  return @image if defined?(@image)

  image = page["image"]
  return @image = nil unless image

  image = { "path" => image } if image.is_a?(String)
  image["path"] ||= image["facebook"] || image["twitter"]
  return @image = nil unless image["path"]

  unless absolute_url? image["path"]
    image["path"] = filters.absolute_url image["path"]
  end

  image["path"] = filters.uri_escape image["path"]

  @image = image.to_liquid
end
name() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 56
def name
  return @name if defined?(@name)
  @name = if seo_name
            seo_name
          elsif !homepage_or_about?
            nil
          elsif site_social["name"]
            format_string site_social["name"]
          elsif site_title
            site_title
          end
end
page_lang() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 180
def page_lang
  @page_lang ||= page["lang"] || site["lang"] || "en_US"
end
page_title() click to toggle source

Page title without site title or description appended

# File lib/bunto-seo-tag/drop.rb, line 39
def page_title
  @page_title ||= format_string(page["title"]) || site_title
end
site_description() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 34
def site_description
  @site_description ||= format_string site["description"]
end
site_title() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 30
def site_title
  @site_title ||= format_string(site["title"] || site["name"])
end
title() click to toggle source

Page title with site title or description appended

# File lib/bunto-seo-tag/drop.rb, line 44
def title
  @title ||= begin
    if site_title && page_title != site_title
      page_title + TITLE_SEPARATOR + site_title
    elsif site_description && site_title
      site_title + TITLE_SEPARATOR + site_description
    else
      page_title || site_title
    end
  end
end
title?() click to toggle source

Should the `<title>` tag be generated for this page?

# File lib/bunto-seo-tag/drop.rb, line 24
def title?
  return false unless title
  return @display_title if defined?(@display_title)
  @display_title = (@text !~ %r!title=false!i)
end
type() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 117
def type
  @type ||= begin
    if page_seo["type"]
      page_seo["type"]
    elsif homepage_or_about?
      "WebSite"
    elsif page["date"]
      "BlogPosting"
    else
      "WebPage"
    end
  end
end
version() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 19
def version
  Bunto::SeoTag::VERSION
end

Private Instance Methods

absolute_url?(string) click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 218
def absolute_url?(string)
  return unless string
  Addressable::URI.parse(string).absolute?
rescue Addressable::URI::InvalidURIError
  nil
end
author_hash(author_string) click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 242
def author_hash(author_string)
  if site.data["authors"] && site.data["authors"][author_string]
    hash = site.data["authors"][author_string]
    hash["name"]    ||= author_string
    hash["twitter"] ||= author_string
    hash
  else
    { "name" => author_string }
  end
end
author_string_or_hash() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 233
def author_string_or_hash
  @author_string_or_hash ||= begin
    author = page["author"]
    author = page["authors"][0] if author.to_s.empty? && page["authors"]
    author = site["author"] if author.to_s.empty?
    author
  end
end
fallback_data() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 214
def fallback_data
  @fallback_data ||= {}
end
filters() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 196
def filters
  @filters ||= Bunto::SeoTag::Filters.new(@context)
end
format_string(string) click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 225
def format_string(string)
  string = FORMAT_STRING_METHODS.reduce(string) do |memo, method|
    filters.public_send(method, memo)
  end

  string unless string.empty?
end
homepage_or_about?() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 208
def homepage_or_about?
  page["url"] =~ HOMEPAGE_OR_ABOUT_REGEX
end
page() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 200
def page
  @page ||= @context.registers[:page].to_liquid
end
page_seo() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 257
def page_seo
  @page_seo ||= sub_hash(page, "seo")
end
seo_name() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 253
def seo_name
  @seo_name ||= format_string(page_seo["name"]) if page_seo["name"]
end
site() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 204
def site
  @site ||= @context.registers[:site].site_payload["site"].to_liquid
end
site_social() click to toggle source
# File lib/bunto-seo-tag/drop.rb, line 261
def site_social
  @site_social ||= sub_hash(site, "social")
end
sub_hash(hash, key) click to toggle source

Safely returns a sub hash

hash - the parent hash key - the key in the parent hash

Returns the sub hash or an empty hash, if it does not exist

# File lib/bunto-seo-tag/drop.rb, line 271
def sub_hash(hash, key)
  if hash[key].is_a?(Hash)
    hash[key]
  else
    {}
  end
end