Adding functionality for fetching code from github

hack for 1.8.7

Don't miss test_markup's relative require

need some food now
This commit is contained in:
Henrik
2012-09-04 22:25:16 +02:00
committed by bootstraponline
parent 0cf0fad50e
commit 749b5a5ff8
8 changed files with 159 additions and 7 deletions
+46
View File
@@ -0,0 +1,46 @@
require 'net/http'
require 'uri'
require 'open-uri'
module Gollum
class Gitcode
def initialize path
raise(ArgumentError, 'path is nil or empty') if path.nil? or path.empty?
@uri = URI::HTTP.build({
:path => self.unchomp(path),
:host => 'raw.github.com',
:scheme => 'https',
:port => 443 })
end
def contents
@contents ||= self.req @uri
#req @uri
end
def unchomp p
p[0] == '/' ? p : ('/' + p)
end
def req uri, cut = 1
#puts uri.to_s
return "Too many redirects or retries" if cut >= 10
http = Net::HTTP.new uri.host, uri.port
http.use_ssl = true
resp = http.get uri.path, {
'Accept' => 'text/plain',
'Cache-Control' => 'no-cache',
'Connection' => 'keep-alive',
'Host' => uri.host,
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0'
}
code = resp.code.to_i
return resp.body if code == 200
return "Not Found" if code == 404
return "Unhandled Response Code #{code}" unless code == 304 or not resp.header['location'].nil?
loc = URI.parse resp.header['location']
uri2 = loc.relative?() ? (uri + loc) : loc # overloads (+)
return req uri2, (cut + 1)
end
end
end
+16
View File
@@ -2,6 +2,7 @@ require 'digest/sha1'
require 'cgi'
require 'pygments'
require 'base64'
require File.expand_path( '../gitcode', __FILE__ )
# initialize Pygments
Pygments.start
@@ -50,6 +51,7 @@ module Gollum
data = @data.dup
data = extract_metadata(data)
data = extract_gitcode(data)
data = extract_code(data)
data = extract_tex(data)
data = extract_wsd(data)
@@ -449,6 +451,20 @@ module Gollum
end
end
#########################################################################
#
# Gitcode - fetch code from github search path and replace the contents
# to a code-block that gets run the next parse.
#
#########################################################################
def extract_gitcode data
data.gsub /^[ \t]*``` ?(\w+):(.+)```$/ do
gc = Gollum::Gitcode.new $2
"```#{$1}\n#{gc.contents}\n```"
end
end
#########################################################################
#
# Code