2015-10-25 21:21:46 +00:00
|
|
|
module Jekyll
|
|
|
|
class SeoTag < Liquid::Tag
|
|
|
|
|
|
|
|
attr_accessor :context
|
|
|
|
|
2016-01-06 06:08:27 +00:00
|
|
|
HTML_ESCAPE = {
|
|
|
|
"\u201c".freeze => '“'.freeze,
|
|
|
|
"\u201d".freeze => '”'.freeze
|
|
|
|
}
|
|
|
|
HTML_ESCAPE_REGEX = Regexp.union(HTML_ESCAPE.keys).freeze
|
|
|
|
|
2015-10-25 21:21:46 +00:00
|
|
|
def render(context)
|
|
|
|
@context = context
|
2015-11-25 21:23:59 +00:00
|
|
|
output = Liquid::Template.parse(template_contents).render!(payload, info)
|
|
|
|
|
|
|
|
# Encode smart quotes. See https://github.com/benbalter/jekyll-seo-tag/pull/6
|
2016-01-06 06:08:27 +00:00
|
|
|
output.gsub!(HTML_ESCAPE_REGEX, HTML_ESCAPE)
|
2015-11-25 21:23:59 +00:00
|
|
|
|
|
|
|
output
|
2015-10-25 21:21:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def payload
|
|
|
|
{
|
|
|
|
"page" => context.registers[:page],
|
|
|
|
"site" => context.registers[:site].site_payload["site"]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def info
|
|
|
|
{
|
|
|
|
:registers => context.registers,
|
|
|
|
:filters => [Jekyll::Filters]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|