class Jekyll::Linkpreview::LinkpreviewTag
Public Class Methods
new(tag_name, markup, parse_context)
click to toggle source
Calls superclass method
# File lib/jekyll-linkpreview.rb, line 142 def initialize(tag_name, markup, parse_context) super @markup = markup.strip() end
Public Instance Methods
get_properties(url)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 153 def get_properties(url) cache_filepath = "#{@@cache_dir}/%s.json" % Digest::MD5.hexdigest(url) if File.exist?(cache_filepath) then hash = load_cache_file(cache_filepath) return create_properties_from_hash(hash) end page = fetch(url) properties = create_properties_from_page(page) if Dir.exists?(@@cache_dir) then save_cache_file(cache_filepath, properties) else # TODO: This message will be shown at all linkprevew tag warn "'#{@@cache_dir}' directory does not exist. Create it for caching." end properties end
render(context)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 147 def render(context) url = get_url_from(context) properties = get_properties(url) render_linkpreview properties end
Protected Instance Methods
save_cache_file(filepath, properties)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 186 def save_cache_file(filepath, properties) File.open(filepath, 'w') { |f| f.write JSON.generate(properties.to_hash) } end
Private Instance Methods
create_properties_from_hash(hash)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 201 def create_properties_from_hash(hash) if hash['image'] then factory = OpenGraphPropertiesFactory.new else factory = NonOpenGraphPropertiesFactory.new end factory.from_hash(hash) end
create_properties_from_page(page)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 191 def create_properties_from_page(page) if page.meta_tags['property'].empty? then factory = NonOpenGraphPropertiesFactory.new else factory = OpenGraphPropertiesFactory.new end factory.from_page(page) end
fetch(url)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 176 def fetch(url) MetaInspector.new(url) end
gen_custom_template(template_path, hash)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 266 def gen_custom_template(template_path, hash) template = File.read template_path Liquid::Template.parse(template).render!(hash) end
gen_default_template(hash)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 227 def gen_default_template(hash) title = hash['title'] url = hash['url'] description = hash['description'] 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> 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"> <a href="#{url}" target="_blank">#{title}</a> </h2> <div class="jekyll-linkpreview-description">#{description}</div> </div> </div> <div class="jekyll-linkpreview-footer"> <a href="//#{domain}" target="_blank">#{domain}</a> </div> </div> </div> EOS html end
get_custom_template_path(properties)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 222 def get_custom_template_path(properties) File.join Dir.pwd, @@template_dir, properties.template_file end
get_url_from(context)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 171 def get_url_from(context) context[@markup] end
load_cache_file(filepath)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 181 def load_cache_file(filepath) JSON.parse(File.open(filepath).read) end
render_linkpreview(properties)
click to toggle source
# File lib/jekyll-linkpreview.rb, line 211 def render_linkpreview(properties) template_path = get_custom_template_path properties if File.exist?(template_path) hash = properties.to_hash_for_custom_template gen_custom_template template_path, hash else gen_default_template properties.to_hash end end