lib/jekyll-linkpreview.rb in jekyll-linkpreview-0.6.0.rc1 vs lib/jekyll-linkpreview.rb in jekyll-linkpreview-0.6.0
- old
+ new
@@ -8,31 +8,34 @@
module Jekyll
module Linkpreview
class OpenGraphProperties
@@template_file = 'linkpreview.html'
- def initialize(title, url, image, description, domain)
+ def initialize(title, type, url, image, description, domain)
@title = title
+ @type = type
@url = url
@image = image
@description = description
@domain = domain
end
def to_hash()
{
'title' => @title,
+ 'type' => @type,
'url' => @url,
'image' => @image,
'description' => @description,
'domain' => @domain,
}
end
def to_hash_for_custom_template()
{
'link_title' => @title,
+ 'link_type' => @type,
'link_url' => @url,
'link_image' => @image,
'link_description' => @description,
'link_domain' => @domain
}
@@ -46,20 +49,21 @@
class OpenGraphPropertiesFactory
def from_page(page)
og_properties = page.meta_tags['property']
image_url = get_og_property(og_properties, 'og:image')
title = get_og_property(og_properties, 'og:title')
+ type = get_og_property(og_properties, 'og:type')
url = get_og_property(og_properties, 'og:url')
image = convert_to_absolute_url(image_url, page.root_url)
description = get_og_property(og_properties, 'og:description')
domain = page.host
- OpenGraphProperties.new(title, url, image, description, domain)
+ OpenGraphProperties.new(title, type, url, image, description, domain)
end
def from_hash(hash)
OpenGraphProperties.new(
- hash['title'], hash['url'], hash['image'], hash['description'], hash['domain'])
+ hash['title'], hash['type'], hash['url'], hash['image'], hash['description'], hash['domain'])
end
private
def get_og_property(properties, key)
if !properties.key? key then
@@ -114,12 +118,11 @@
end
end
class NonOpenGraphPropertiesFactory
def from_page(page)
- NonOpenGraphProperties.new(
- page.title, page.url, get_description(page), page.host)
+ NonOpenGraphProperties.new(page.best_title, page.url, get_description(page), page.host)
end
def from_hash(hash)
NonOpenGraphProperties.new(
hash['title'], hash['url'], hash['description'], hash['domain'])
@@ -187,11 +190,13 @@
File.open(filepath, 'w') { |f| f.write JSON.generate(properties.to_hash) }
end
private
def create_properties_from_page(page)
- if page.meta_tags['property'].empty? then
+ if !%w[og:title og:type og:url og:image].all? { |required_tag|
+ page.meta_tags['property'].include?(required_tag)
+ }
factory = NonOpenGraphPropertiesFactory.new
else
factory = OpenGraphPropertiesFactory.new
end
factory.from_page(page)
@@ -237,19 +242,18 @@
domain = hash['domain']
image = hash['image']
image_html = ""
if image then
image_html = <<-EOS
-<div class="jekyll-linkpreview-image">
- <a href="#{url}" target="_blank">
- <img src="#{image}" />
- </a>
-</div>
+ <div class="jekyll-linkpreview-image">
+ <a href="#{url}" target="_blank">
+ <img src="#{image}" />
+ </a>
+ </div>
EOS
end
html = <<-EOS
<div class="jekyll-linkpreview-wrapper">
- <p><a href="#{url}" target="_blank">#{url}</a></p>
<div class="jekyll-linkpreview-wrapper-inner">
<div class="jekyll-linkpreview-content">
#{image_html}
<div class="jekyll-linkpreview-body">
<h2 class="jekyll-linkpreview-title">