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

52 lines
1.0 KiB
Ruby
Raw Normal View History

2015-10-25 21:21:46 +00:00
module Jekyll
class SeoTag < Liquid::Tag
attr_accessor :context
2016-01-13 07:09:28 +00:00
def initialize(_, markup, _)
super
@options = {
"title" => !(markup =~ /title\s*:\s*false/i)
}
end
2015-10-25 21:21:46 +00:00
def render(context)
@context = context
2016-01-11 00:49:18 +00:00
output = template.render!(payload, info)
output
2015-10-25 21:21:46 +00:00
end
private
def payload
{
"page" => context.registers[:page],
2016-01-13 07:09:28 +00:00
"site" => context.registers[:site].site_payload["site"],
"seo" => @options
2015-10-25 21:21:46 +00:00
}
end
def info
{
:registers => context.registers,
:filters => [Jekyll::Filters]
}
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-01-06 20:29:14 +00:00
@template_contents ||= File.read(template_path).gsub(/(>\n|[%}]})\s+(<|{[{%])/,'\1\2').chomp
2015-10-25 21:21:46 +00:00
end
def template_path
@template_path ||= File.expand_path "./template.html", File.dirname(__FILE__)
end
end
end
Liquid::Template.register_tag('seo', Jekyll::SeoTag)