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

68 lines
1.3 KiB
Ruby
Raw Normal View History

2016-09-08 15:33:03 +00:00
require "jekyll-seo-tag/filters"
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
attr_accessor :context
2016-09-08 15:33:03 +00:00
MINIFY_REGEX = %r!([>,]\n|[%}]})\s+?(<|{[{%]|[ ]+\")!
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
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,
"title" => title?
}
end
2015-10-25 21:21:46 +00:00
def payload
{
2016-09-08 15:33:03 +00:00
"page" => context.registers[:page],
"site" => context.registers[:site].site_payload["site"],
"paginator" => context["paginator"],
"seo_tag" => options
2015-10-25 21:21:46 +00:00
}
end
2016-02-21 19:25:15 +00:00
def title?
2016-09-08 15:33:03 +00:00
!(@text =~ %r!title=false!i)
2016-02-21 19:25:15 +00:00
end
2015-10-25 21:21:46 +00:00
def info
{
:registers => context.registers,
2016-09-08 15:33:03 +00:00
:filters => [Jekyll::Filters, JekyllSeoTag::Filters]
2015-10-25 21:21:46 +00:00
}
end
2016-01-11 00:49:18 +00:00
def template
@template ||= Liquid::Template.parse template_contents
end
2015-10-25 21:21:46 +00:00
def template_contents
2016-02-09 23:48:24 +00:00
@template_contents ||= begin
File.read(template_path).gsub(MINIFY_REGEX, '\1\2').chomp
end
2015-10-25 21:21:46 +00:00
end
def template_path
2016-02-09 23:48:24 +00:00
@template_path ||= begin
2016-09-08 15:33:03 +00:00
File.expand_path "./template.html", File.dirname(__FILE__)
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)