Skip to content

Instantly share code, notes, and snippets.

@cristibalan
Created November 13, 2009 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cristibalan/233974 to your computer and use it in GitHub Desktop.
Save cristibalan/233974 to your computer and use it in GitHub Desktop.
require 'net/http'
module Rack
class GistInline
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
body = body.inject("") { |html, part| html += part }
body.gsub!(/<script[^>]*gist\.github\.com\/(\d+)\.js[^>]*><\/script>/) do |script|
src = script.match(/src=['"]?([^"']*)["']?/)[1]
js = Net::HTTP.get(URI.parse(src))
output = []
js.each do |line|
if line.chomp =~/^document\.write\('(.*)'\);?/
output << $1.gsub(/\\(["'\/])/, '\1').gsub('\n', "\n").gsub('\\\\', '\\')
end
end
output.join("\n")
end
headers['Content-Length'] = body.length.to_s
[status, headers, body]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment