jekyll-seo-tag/lib/jekyll-seo-tag.rb

96 lines
2.6 KiB
Ruby
Raw Normal View History

2017-10-22 16:37:44 +00:00
# frozen_string_literal: true
2017-04-06 23:02:32 +00:00
require "jekyll"
2016-09-08 15:33:03 +00:00
require "jekyll-seo-tag/version"
2016-01-29 18:35:40 +00:00
2015-10-25 21:21:46 +00:00
module Jekyll
class SeoTag < Liquid::Tag
2017-08-23 20:11:55 +00:00
autoload :JSONLD, "jekyll-seo-tag/json_ld"
2017-08-23 15:07:52 +00:00
autoload :AuthorDrop, "jekyll-seo-tag/author_drop"
2017-08-23 20:11:55 +00:00
autoload :ImageDrop, "jekyll-seo-tag/image_drop"
2017-08-23 20:25:28 +00:00
autoload :JSONLDDrop, "jekyll-seo-tag/json_ld_drop"
2017-08-23 20:11:55 +00:00
autoload :UrlHelper, "jekyll-seo-tag/url_helper"
autoload :Drop, "jekyll-seo-tag/drop"
autoload :Filters, "jekyll-seo-tag/filters"
autoload :Webmaster, "jekyll-seo-tag/webmaster"
2017-04-07 21:01:20 +00:00
2015-10-25 21:21:46 +00:00
attr_accessor :context
2016-10-15 19:24:13 +00:00
# Matches all whitespace that follows either
# 1. A '}', which closes a Liquid tag
# 2. A '{', which opens a JSON block
# 3. A '>' followed by a newline, which closes an XML tag or
# 4. A ',' followed by a newline, which ends a JSON line
# We will strip all of this whitespace to minify the template
# We will not strip any whitespace if the next character is a '-'
# so that we do not interfere with the HTML comment at the
# very begining
MINIFY_REGEX = %r!(?<=[{}]|[>,]\n)\s+(?\!-)!.freeze
2016-02-09 23:48:24 +00:00
def initialize(_tag_name, text, _tokens)
2016-01-13 07:09:28 +00:00
super
@text = text
2016-01-13 07:09:28 +00:00
end
2015-10-25 21:21:46 +00:00
def render(context)
@context = context
2017-08-24 17:22:59 +00:00
SeoTag.template.render!(payload, info)
2015-10-25 21:21:46 +00:00
end
private
def options
{
2016-09-08 15:33:03 +00:00
"version" => Jekyll::SeoTag::VERSION,
2017-01-30 01:53:19 +00:00
"title" => title?,
}
end
2015-10-25 21:21:46 +00:00
def payload
# site_payload is an instance of UnifiedPayloadDrop. See https://git.io/v5ajm
context.registers[:site].site_payload.tap do |site_payload|
site_payload["page"] = context.registers[:page]
site_payload["paginator"] = context["paginator"]
site_payload["seo_tag"] = drop
end
2015-10-25 21:21:46 +00:00
end
2017-04-06 23:02:32 +00:00
def drop
if context.registers[:site].liquid_renderer.respond_to?(:cache)
Jekyll::SeoTag::Drop.new(@text, @context)
else
@drop ||= Jekyll::SeoTag::Drop.new(@text, @context)
end
2016-02-21 19:25:15 +00:00
end
2015-10-25 21:21:46 +00:00
def info
{
:registers => context.registers,
2017-01-30 01:53:19 +00:00
:filters => [Jekyll::Filters],
2015-10-25 21:21:46 +00:00
}
end
2017-08-24 17:22:59 +00:00
class << self
def template
@template ||= Liquid::Template.parse template_contents
end
2016-01-11 00:49:18 +00:00
2017-08-24 17:22:59 +00:00
private
def template_contents
@template_contents ||= begin
File.read(template_path).gsub(MINIFY_REGEX, "")
end
2016-02-09 23:48:24 +00:00
end
2015-10-25 21:21:46 +00:00
2017-08-24 17:22:59 +00:00
def template_path
@template_path ||= begin
File.expand_path "./template.html", File.dirname(__FILE__)
end
2016-02-09 23:48:24 +00:00
end
2015-10-25 21:21:46 +00:00
end
end
end
2016-09-08 15:33:03 +00:00
Liquid::Template.register_tag("seo", Jekyll::SeoTag)