Skip to content

Instantly share code, notes, and snippets.

@kassens
Created October 15, 2008 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kassens/16920 to your computer and use it in GitHub Desktop.
Save kassens/16920 to your computer and use it in GitHub Desktop.
hackish mootools docs server
require 'rubygems'
require 'maruku'
unless Enumerable.instance_methods.include? "group_by" then
module Enumerable
def group_by(&blck)
result = Hash.new;
each do |x|
k = yield x
result[k] ||= [];
result[k] << x
end
result
end
end
end
class DocsServlet < HTTPServlet::AbstractServlet
# reloads this servlet on each request
def DocsServlet.get_instance config, *options
load __FILE__
# load 'build.rb' # remove
DocsServlet.new config, *options
end
def initialize(a, path)
@path = path
end
def comps
load(File.dirname(__FILE__) + "/build.rb")
data = {}
MooTools.new('./core').data.each do |script, detail|
key = detail[:folder]
data[key] ||= []
data[key] << script
end
data.to_a
end
def menu
'<div class="menu">' + comps.map { |c| menu_category(*c) }.join('<br />') + '</div>'
end
def menu_category(folder, files)
"<b>#{folder}</b>" +
files.map do |file|
"<br /><a href=\"#{folder}/#{file}\">#{file}</a>"
end.join
end
def header
<<-HTML.gsub(/^\s+/, '')
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<base href="#{@base_href}">
<title>MooTools Docs</title>
<style type="text/css" media="screen">
* {
font-family: sans-serif;
}
div.menu {
width: 160px;
float: left;
background-color: #eee;
margin: 0 2em 1em 0;
padding: 1em;
}
div.menu a {
margin-left: .5em;
}
div.menu a {
color: #444;
font-weight: normal;
text-decoration: none;
}
</style>
</head>
<body>
<h1>MooTools Docs</h1>
HTML
end
def footer
'</body></html>'
end
def parse_url(url)
url[/(.+)\/(.*[\w])/]
raise HTTPStatus::NotFound.new("not found") unless $& && File.exists?(file_name([$1, $2]))
[$1, $2]
end
def file_name(path)
File.join(@path, 'Docs', path[0], path[1]) + '.md'
end
def show(path)
data = File.read file_name(path)
Maruku.new(data).to_html
end
def do_GET(req, res)
@base_href = req.script_name + "/"
res['Content-Type'] = "text/html"
res.body = header + menu
unless req.path_info.empty? then
path = parse_url(req.path_info)
res.body += show(path)
end
end
end
#!/usr/bin/env ruby
unless ARGV.length == 1 then
puts "USAGE\n docs.rb <root path>"
exit
end
require 'webrick'
include WEBrick
require File.dirname(__FILE__) + '/docs.rb'
server = HTTPServer.new( :Port => 2000 )
server.mount '/', DocsServlet, ARGV[0]
trap("INT"){ server.shutdown }
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment