View Haml in Browser

1,676 views
Skip to first unread message

Paul Barry

unread,
Aug 20, 2008, 5:23:18 PM8/20/08
to Haml
Does anyone have a tool yet that allows you to double click on a haml
file and see it's render HTML content in a browser? And for bonus
points be able to edit the file, press refresh in the browser and see
the updated HTML?

Chris Eppstein

unread,
Aug 20, 2008, 5:27:05 PM8/20/08
to Haml
haml foo.haml > foo.html

Paul Barry

unread,
Aug 20, 2008, 5:31:05 PM8/20/08
to Haml
So I know this isn't really a Haml question, but is there a way to
have FireFox do that every time I open/refresh file:///Users/pbarry/projects/haml/index.haml?

Rhett Sutphin

unread,
Aug 20, 2008, 5:32:00 PM8/20/08
to ha...@googlegroups.com

Almost. There was a thread just a few days ago about viewing haml
directly in Apache:

http://groups.google.com/group/haml/browse_thread/thread/3a1d93760f33d70e

I use a ruby script that uses Webrick to serve all the haml and sass
files in a directory. It's based on one by John Long I found at
Wiseheart Design (inline below).

Neither of these allow you to double click on the file to open it (you
have to know the URL to use, either served from Apache or Webrick),
but otherwise they have the properties you mention.

Rhett

---- BEGIN SCRIPT -----

#!/usr/bin/env ruby

# Simple server that lets you automatically preview Haml outside of an
# application. From http://wiseheartdesign.com/2007/9/4/a-haml-server-for-web-designers/

require 'webrick'
require 'rubygems'
require 'haml'
require 'sass'

class AbstractHamlHandler < 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(data)
res['content-type'] = content_type
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(string)
engine = engine_class.new(string,
:attr_wrapper => '"',
:filename => @script_filename
)
engine.render
end
end

class HamlHandler < AbstractHamlHandler
def content_type
'text/html'
end

def engine_class
Haml::Engine
end
end

class SassHandler < AbstractHamlHandler
def content_type
'text/css'
end

def engine_class
Sass::Engine
end
end

WEBrick::HTTPServlet::FileHandler.add_handler("haml", HamlHandler)
WEBrick::HTTPServlet::FileHandler.add_handler("sass", SassHandler)

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

Paul Barry

unread,
Aug 20, 2008, 6:00:31 PM8/20/08
to Haml
Yeah, I'm specifically trying to avoid developers to require having
any kind of server running to view the haml.

haml foo.haml > foo.html && open foo.html

That's close, but still doesn't solve the open in finder or refresh
problems. Probably time to learn how to hack out a firefox extension.

On Aug 20, 5:32 pm, Rhett Sutphin <rhett.sutp...@gmail.com> wrote:
> On Aug 20, 2008, at 4:23 PM, Paul Barry wrote:
>
> > Does anyone have a tool yet that allows you to double click on a haml
> > file and see it's render HTML content in a browser? And for bonus
> > points be able to edit the file, press refresh in the browser and see
> > the updated HTML?
>
> Almost. There was a thread just a few days ago about viewing haml
> directly in Apache:
>
> http://groups.google.com/group/haml/browse_thread/thread/3a1d93760f33...
>
> I use a ruby script that uses Webrick to serve all the haml and sass
> files in a directory. It's based on one by John Long I found at
> Wiseheart Design (inline below).
>
> Neither of these allow you to double click on the file to open it (you
> have to know the URL to use, either served from Apache or Webrick),
> but otherwise they have the properties you mention.
>
> Rhett
>
> ---- BEGIN SCRIPT -----
>
> #!/usr/bin/env ruby
>
> # Simple server that lets you automatically preview Haml outside of an
> # application. Fromhttp://wiseheartdesign.com/2007/9/4/a-haml-server-for-web-designers/

Chris Eppstein

unread,
Aug 20, 2008, 6:14:32 PM8/20/08
to Haml
If you're on a Mac, you can use automator to process your file using a
command line action and then open the result in your browser. You can
even make the automator action the default for double clicking the
file.

chris

P.S. Would love to see a firefox extension to allow native reading of
haml AND sass.
Reply all
Reply to author
Forward
0 new messages