Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created December 12, 2010 13:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timruffles/738058 to your computer and use it in GitHub Desktop.
Save timruffles/738058 to your computer and use it in GitHub Desktop.
Lets you use haml, sass and coffee script from Sinatra by compiling on demand
# sass handler
get /\/(.*)\.css/ do |stylesheet|
headers 'Content-Type' => 'text/css; charset=utf-8'
sass stylesheet.to_sym
end
# coffee script handler
get %r{/(.*)\.js} do |js|
# compile coffee script and place in respective JS folder
coffee = "#{ROOT_DIR}/js/#{js}.coffee"
target = "#{ROOT_DIR}/js/compiled/#{js}.js"
raise "Couldn't find #{coffee}" unless File.exists? coffee
if system <<-CMD
mkdir -p #{target.gsub(%r{[^/]*\w*$},'')} &&
/usr/local/bin/coffee --print #{coffee} > #{target}
CMD
File.read target
else
$?
end
end
# haml handler for templates
get %r{/(.*)\.html} do |template|
haml_target = "#{ROOT_DIR}/js/#{template}.haml"
haml_engine = Haml::Engine.new(File.read(haml_target))
haml_engine.render
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment