2016-01-29 18:35:40 +00:00
|
|
|
require 'jekyll-seo-tag/filters'
|
|
|
|
|
2015-10-25 21:21:46 +00:00
|
|
|
module Jekyll
|
|
|
|
class SeoTag < Liquid::Tag
|
|
|
|
attr_accessor :context
|
|
|
|
|
2016-02-09 23:48:24 +00:00
|
|
|
MINIFY_REGEX = /(>\n|[%}]})\s+(<|{[{%])/
|
|
|
|
|
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)
|
2015-11-25 21:23:59 +00:00
|
|
|
|
|
|
|
output
|
2015-10-25 21:21:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def payload
|
|
|
|
{
|
2016-02-20 20:49:35 +00:00
|
|
|
'seo_tag' => { 'version' => VERSION, 'author' => author },
|
|
|
|
'page' => page,
|
|
|
|
'site' => site
|
2015-10-25 21:21:46 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-02-20 20:49:35 +00:00
|
|
|
def page
|
|
|
|
context.registers[:page]
|
|
|
|
end
|
|
|
|
|
|
|
|
def site
|
|
|
|
context.registers[:site].site_payload['site']
|
|
|
|
end
|
|
|
|
|
2015-10-25 21:21:46 +00:00
|
|
|
def info
|
|
|
|
{
|
2016-02-09 23:48:24 +00:00
|
|
|
registers: context.registers,
|
|
|
|
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
|
|
|
|
File.expand_path './template.html', File.dirname(__FILE__)
|
|
|
|
end
|
2015-10-25 21:21:46 +00:00
|
|
|
end
|
2016-02-20 20:49:35 +00:00
|
|
|
|
|
|
|
# Returns a hash representing the post author
|
|
|
|
#
|
|
|
|
# Sources, in order:
|
|
|
|
#
|
|
|
|
# 1. page.author, if page.author is a hash
|
|
|
|
# 2. site.author, if site.author is a hash
|
|
|
|
# 3. site.data.authors[page.author] if page.author is a string
|
|
|
|
# 4. page.author if page.author is a string
|
|
|
|
def author
|
|
|
|
author = page['author'] || site['author']
|
|
|
|
return if author.nil?
|
|
|
|
return author if author.is_a?(Hash)
|
|
|
|
|
|
|
|
if author.is_a?(String)
|
|
|
|
if site['data']['authors'] && site['data']['authors'][author]
|
|
|
|
site['data']['authors'][author]
|
|
|
|
else
|
|
|
|
{ 'twitter' => author }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-10-25 21:21:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('seo', Jekyll::SeoTag)
|