A Haml server for web designers
I have a script named serve in my bin directory that let's me quickly startup a Webrick server in any directory. This is handy for serving HTML mockups or other files. Today, updated it to work with Haml:
#!/usr/bin/env ruby
require 'webrick'
require 'rubygems'
require 'haml'
class HamlHandler < WEBrick::HTTPServlet::AbstractServlet
def initialize(server, name)
super
@script_filename = name
end
def do_GET(req, res)
begin
data = open(@script_filename){|io| io.read }
res.body = parse_haml(data)
res['content-type'] = 'text/html'
rescue StandardError => ex
raise
rescue Exception => ex
@logger.error(ex)
raise HTTPStatus::InternalServerError, ex.message
end
end
alias do_POST do_GET
private
def parse_haml(string)
engine = Haml::Engine.new(string,
:attr_wrapper => '"',
:filename => @script_filename
)
engine.render
end
end
WEBrick::HTTPServlet::FileHandler.add_handler("haml", HamlHandler)
args = ARGV.join(' ')
args.gsub!(%r{^http://}, '')
args = args.split(/[ :]/).compact
server = WEBrick::HTTPServer.new(
:Port => args.pop || 3000,
:BindAddress => args.pop || '0.0.0.0',
:DocumentRoot => Dir.pwd
)
trap("INT") { server.shutdown }
server.start#!/usr/bin/env ruby
require 'webrick'
require 'rubygems'
require 'haml'
class HamlHandler < WEBrick::HTTPServlet::AbstractServlet
def initialize(server, name)
super
@script_filename = name
end
def do_GET(req, res)
begin
data = open(@script_filename){|io| io.read }
res.body = parse_haml(data)
res['content-type'] = 'text/html'
rescue StandardError => ex
raise
rescue Exception => ex
@logger.error(ex)
raise HTTPStatus::InternalServerError, ex.message
end
end
alias do_POST do_GET
private
def parse_haml(string)
engine = Haml::Engine.new(string,
:attr_wrapper => '"',
:filename => @script_filename
)
engine.render
end
end
WEBrick::HTTPServlet::FileHandler.add_handler("haml", HamlHandler)
args = ARGV.join(' ')
args.gsub!(%r{^http://}, '')
args = args.split(/[ :]/).compact
server = WEBrick::HTTPServer.new(
:Port => args.pop || 3000,
:BindAddress => args.pop || '0.0.0.0',
:DocumentRoot => Dir.pwd
)
trap("INT") { server.shutdown }
server.startYou might also like…
Introducing Serve: now with gemmi goodness!
Based on my HAML server for web designers article, I've created a small gem "serve" which makes it extremely easy for a Web Designer to get up and running with Haml and Sass.
Serve: a rapid prototyping framework for designers
Since Adam McCrea has introduced his solution for simplifying HTML mockups over on the EdgeCase blog, I thought I would take the time to reintroduce my own: Serve.

How I manage my dev workflow with three Agent skills
One of the most exciting things about LLMs is their ability to do useful things that would be hard to script in the traditional sense. Skills take this further, letting you build intelligent workflows that handle the messy, context-dependent parts of product development.

Give your AI the full picture with a context repository
Poor AI output usually isn't a prompting problem, it's a context problem. A context repository gives your AI the business knowledge it needs to make good decisions. Here's how to build one.
