move logic to ruby

This commit is contained in:
Ben Balter 2016-02-20 15:49:35 -05:00
parent 43fcf5b7dd
commit 546448be1b
3 changed files with 45 additions and 36 deletions

View File

@ -8,3 +8,12 @@ Style/Documentation:
Style/FileName:
Enabled: false
Metrics/AbcSize:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false

View File

@ -17,12 +17,20 @@ module Jekyll
def payload
{
'seo_tag' => { 'version' => VERSION },
'page' => context.registers[:page],
'site' => context.registers[:site].site_payload['site']
'seo_tag' => { 'version' => VERSION, 'author' => author },
'page' => page,
'site' => site
}
end
def page
context.registers[:page]
end
def site
context.registers[:site].site_payload['site']
end
def info
{
registers: context.registers,
@ -45,6 +53,28 @@ module Jekyll
File.expand_path './template.html', File.dirname(__FILE__)
end
end
# 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
end
end

View File

@ -39,38 +39,8 @@
{% assign seo_description = seo_description | markdownify | strip_html | strip_newlines | escape_once %}
{% endif %}
{% if page.author %}
{% if site.data.authors[page.author] %}
{% assign seo_author_name = site.data.authors[page.author].name %}
{% assign seo_author_twitter = site.data.authors[page.author].twitter | default: seo_author_name %}
{% elsif page.author.name %}
{% assign seo_author_name = page.author.name %}
{% assign seo_author_twitter = page.author.twitter | default: seo_author_name %}
{% elsif page.author.twitter %}
{% assign seo_author_twitter = page.author.twitter %}
{% assign seo_author_twitter = page.author.twitter %}
{% else %}
{% assign seo_author_name = page.author %}
{% assign seo_author_twitter = page.author %}
{% endif %}
{% elsif site.author %}
{% if site.data.authors[site.author] %}
{% assign seo_author_name = site.data.authors[site.author].name %}
{% assign seo_author_twitter = site.data.authors[site.author].twitter | default: seo_author_name %}
{% elsif site.author.name %}
{% assign seo_author_name = site.author.name %}
{% assign seo_author_twitter = site.author.twitter | default: seo_author_name %}
{% else %}
{% assign seo_author_name = site.author %}
{% assign seo_author_twitter = site.author %}
{% endif %}
{% endif %}
{% if seo_author_name %}
{% assign seo_author_name = seo_author_name | strip_html | escape_once %}
{% endif %}
{% if seo_author_twitter %}
{% assign seo_author_twitter = seo_author_twitter | replace:"@","" | prepend:"@" | strip_html | escape_once %}
{% if seo_tag.author %}
{% assign seo_author_twitter = seo_tag.author.twitter | replace:"@","" %}
{% endif %}
{% if seo_title %}
@ -142,7 +112,7 @@
{% endif %}
{% if seo_author_twitter %}
<meta name="twitter:creator" content="{{ seo_author_twitter }}" />
<meta name="twitter:creator" content="@{{ seo_author_twitter }}" />
{% endif %}
{% endif %}