Created
December 12, 2010 13:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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