Skip to content

Instantly share code, notes, and snippets.

@botanicus
Created April 11, 2011 14:51
Show Gist options
  • Save botanicus/913640 to your computer and use it in GitHub Desktop.
Save botanicus/913640 to your computer and use it in GitHub Desktop.
Preview raw HTML from gist or pastie as a formatted HTML.
.rvmrc
.bundle

About

Previews raw HTML source as HTML. Useful for showing raw HTML from pastie services or gists in more readable formatted form.

Simply run it as a rack app and add ?url=<your-gist-url> in order to preview it.

#!/usr/bin/env rackup
# encoding: utf-8
require "open-uri"
class Preview
def call(env)
request = Rack::Request.new(env)
if url = request.params["url"]
data = open(url, &:read)
body = <<-EOF
<!DOCTYPE html>
<html>
<head>
</head>
<body>
#{data}
<footer>From <a href="#{url}">#{url}</a>. Powered by <a href="https://gist.github.com/913640">Gist 913640</a>.</footer>
</body>
</html>
EOF
[200, {"Content-Length" => body.bytesize.to_s, "Content-Type" => "text/html"}, [body]]
else
body = <<-EOF
<h1>Not Found</h1>
<p>
You have to provide <code>?url=url_with_raw_html_data</code> in order to preview it as HTML!
</p>
EOF
[404, {"Content-Length" => body.bytesize.to_s, "Content-Type" => "text/html"}, [body]]
end
end
end
map "/preview" do
run Preview.new
end
# encoding: utf-8
source "http://gemcutter.org"
gem "rack"
#!/bin/sh
ROOT=/webs/services/preview
PIDFILE=/var/run/preview.pid
start () {
$ROOT/config.ru --port=1001 --env=production --daemonize --pid $PIDFILE
}
stop () {
kill -9 $(cat $PIDFILE)
}
case "$1" in
start|"")
start ;;
restart)
stop && start ;;
stop)
stop ;;
*)
echo "Usage: $0 [start|restart|stop]" >&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment