module Octopress
module Gist
class Tag < Liquid::Tag
def initialize(tag_name, markup, tokens)
super
@cache_disabled = false
@tag_name = tag_name
@markup = markup
end
def render(context)
if Pygments.clean_markup(@markup) =~ /([\S]*)\s*(\S*)/
gist_id, filename = $1.strip, $2.strip
filename = $2.strip == "" ? nil : $2.strip
options = {
gist_id: $1.strip,
cache: !@cache_disabled
}
options = Pygments.parse_markup(@markup, options)
if @cache_disabled or ! gist = Cache.read_cache(options)
gist = download_gist(options)
end
options[:url] ||= gist['html_url']
options[:link_text] ||= "View Gist"
output = []
gist_files(gist, filename).each do |file|
opt = {
title: file['filename'],
lang: file['language'].downcase
}.merge(options)
begin
code = Pygments.highlight(file['content'], opt)
code = "
#{error_msg}\n#{syntax_msg}" end end def gist_files(gist, filename=nil) gists = gist['files'] # Return a single file if specified if filename if file = gists[filename] [file] else # List file names found in the error message. filenames = gists.keys.each { |k| k.to_s } raise "File '#{filename}' not found in gist #{gist['id']}. Did you mean '#{filenames.join('\' or \'')}'?" end else gists.values.each{ |v| v } end end def download_gist(options) retried = false begin response = URI.parse("https://api.github.com/gists/#{options[:gist_id]}").read Cache.write_cache(response, options) if options[:cache] JSON.parse(response) rescue => e raise if retried retried = true retry end rescue => e puts "Failed to download Gist: #{gist}.".red puts e.extend Error end end end end