Last active
December 17, 2015 04:19
-
-
Save j5ik2o/5550083 to your computer and use it in GitHub Desktop.
Octopress Gist Tag プラグイン 改造版
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers. | |
| # by: Brandon Tilly | |
| # Source URL: https://gist.github.com/1027674 | |
| # Post http://brandontilley.com/2011/01/31/gist-tag-for-jekyll.html | |
| # | |
| # Example usage: {% gist 1027674 gist_tag.rb %} //embeds a gist for this plugin | |
| require 'cgi' | |
| require 'digest/md5' | |
| require 'net/https' | |
| require 'uri' | |
| require 'json' | |
| require './plugins/pygments_code' | |
| require './plugins/raw' | |
| require './plugins/octopress_filters' | |
| module Jekyll | |
| class GistTag < Liquid::Tag | |
| include HighlightCode | |
| include TemplateWrapper | |
| include OctopressFilters | |
| def initialize(tag_name, text, token) | |
| @title = nil | |
| @caption = nil | |
| @highlight = true | |
| super | |
| @text = text | |
| @cache_disabled = false | |
| @cache_folder = File.expand_path "../.gist-cache", File.dirname(__FILE__) | |
| FileUtils.mkdir_p @cache_folder | |
| end | |
| def render(context) | |
| if parts = @text.match(/([\d]*) (.*)/) | |
| output = super | |
| gist, file = parts[1].strip, parts[2].strip | |
| script_url = script_url_for gist, file | |
| json = get_cached_gist(gist, file) || get_gist_from_web(gist, file) | |
| files = json["files"] | |
| url = json["html_url"] | |
| description = json["description"] | |
| source = "" | |
| files.each do | name, info| | |
| if !file.empty? && file != name | |
| next | |
| end | |
| code = info["content"] | |
| lang = info["language"].downcase | |
| if lang == "markdown" | |
| # マークダウンのレンダリング | |
| # source += pre_filter(code) | |
| # partial = Liquid::Template.parse(source) | |
| # context.stack do | |
| # partial.render(context) | |
| # end | |
| else | |
| caption = "<figcaption><span>#{description}</span><a target='_blank' href='#{url}'>link</a></figcaption>" | |
| code = "<figure class='code'>#{caption}#{highlight(code, lang)}</figure>" | |
| code = safe_wrap(code) | |
| code = context['pygments_prefix'] + code if context['pygments_prefix'] | |
| code = code + context['pygments_suffix'] if context['pygments_suffix'] | |
| source += code | |
| end | |
| end | |
| source | |
| else | |
| "" | |
| end | |
| end | |
| def script_url_for(gist_id, filename) | |
| url = "https://gist.github.com/#{gist_id}.js" | |
| url = "#{url}?file=#{filename}" unless filename.nil? or filename.empty? | |
| url | |
| end | |
| def get_gist_url_for(gist, file) | |
| "https://api.github.com/gists/#{gist}" | |
| end | |
| def cache(gist, file, json) | |
| cache_file = get_cache_file_for gist, file | |
| File.open(cache_file, "w") do |io| | |
| JSON.dump(json, io) | |
| end | |
| end | |
| def get_cached_gist(gist, file) | |
| return nil if @cache_disabled | |
| cache_file = get_cache_file_for gist, file | |
| #data = File.read cache_file if File.exist? cache_file | |
| data = nil | |
| if File.exist? cache_file | |
| open(cache_file) do |io| | |
| data = JSON.load(io) | |
| end | |
| end | |
| data | |
| end | |
| def get_cache_file_for(gist, file) | |
| bad_chars = /[^a-zA-Z0-9\-_.]/ | |
| gist = gist.gsub bad_chars, '' | |
| file = file.gsub bad_chars, '' | |
| md5 = Digest::MD5.hexdigest "#{gist}-#{file}" | |
| File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache" | |
| end | |
| def http_get_data(uri) | |
| raw_uri = URI.parse uri | |
| proxy = ENV['http_proxy'] | |
| if proxy | |
| proxy_uri = URI.parse(proxy) | |
| https = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new raw_uri.host, raw_uri.port | |
| else | |
| https = Net::HTTP.new raw_uri.host, raw_uri.port | |
| end | |
| https.use_ssl = true | |
| https.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
| request = Net::HTTP::Get.new raw_uri.request_uri | |
| data = https.request request | |
| if data.code.to_i != 200 | |
| raise RuntimeError, "Gist replied with #{data.code} for #{uri}" | |
| end | |
| data | |
| end | |
| def get_gist_from_web(gist, file) | |
| gist_url = get_gist_url_for gist, file | |
| data = http_get_data(gist_url) | |
| json = JSON.parse(data.body) | |
| cache gist, file, json unless @cache_disabled | |
| json | |
| end | |
| end | |
| class GistTagNoCache < GistTag | |
| def initialize(tag_name, text, token) | |
| super | |
| @cache_disabled = true | |
| end | |
| end | |
| end | |
| Liquid::Template.register_tag('gist', Jekyll::GistTag) | |
| Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment