Skip to content

Instantly share code, notes, and snippets.

@chsh
Created April 14, 2013 03:22
Show Gist options
  • Select an option

  • Save chsh/5381286 to your computer and use it in GitHub Desktop.

Select an option

Save chsh/5381286 to your computer and use it in GitHub Desktop.
Embed URL and content like blockquote.
# A Liquid tag for Jekyll sites that allows embedding URL.
#
# Usage: {% crawlquote http://www.google.co.jp/ %}
require 'cgi'
require 'digest/md5'
require 'net/https'
require 'uri'
require 'open-uri'
require 'nokogiri'
require 'digest/sha2'
module Jekyll
class CrawlQuoteTag < Liquid::Tag
def initialize(tag_name, text, token)
super
@text = text
@cache_disabled = false
@cache_folder = File.expand_path "../.crawl-cache", File.dirname(__FILE__)
FileUtils.mkdir_p @cache_folder
end
def render(context)
if parts = @text.match(/(https?:\/\/.*)/)
url = parts[1].strip
doc = Nokogiri::HTML(cache(url))
title = doc.css('title').text
description = doc.css('meta[name="description"]')
if description
description = description.attr('content')
end
"<blockquote><p>#{description}</p><cite><a href='#{url}'>#{title}</a></cite></blockquote>"
else
""
end
end
def cache(url)
cp = cache_path(url)
content = nil
if File.exist? cp
content = File.read(cp)
else
content = open(url).read
File.open(cp, "w") do |io|
io.write content
end
end
content
end
def cache_path(url)
dig = Digest::SHA256.hexdigest url
File.join @cache_folder, "#{dig}.cache"
end
end
end
Liquid::Template.register_tag('crawlquote', Jekyll::CrawlQuoteTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment