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 "
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
2018-10-26 14:49:04 +00:00
MINIFY_REGEX = %r!(?<=[{}]|[>,]\n)\s+(?\!-)! . freeze
2016-02-09 23:48:24 +00:00
2016-02-21 19:16:00 +00:00
def initialize ( _tag_name , text , _tokens )
2016-01-13 07:09:28 +00:00
super
2016-02-21 19:16:00 +00:00
@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
2016-02-21 19:16:00 +00:00
def options
{
2016-09-08 15:33:03 +00:00
" version " = > Jekyll :: SeoTag :: VERSION ,
2017-01-30 01:53:19 +00:00
" title " = > title? ,
2016-02-21 19:16:00 +00:00
}
end
2015-10-25 21:21:46 +00:00
def payload
2022-04-28 12:06:46 +00:00
# site_payload is an instance of UnifiedPayloadDrop. See https://github.com/jekyll/jekyll/blob/22f2724a1f117a94cc16d18c499a93d5915ede4f/lib/jekyll/site.rb#L261-L276
2020-10-18 08:36:55 +00:00
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
2019-03-16 08:30:11 +00:00
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
{
2016-08-11 15:35:31 +00:00
: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 )