Created
June 15, 2011 19:21
-
-
Save dpk/1027863 to your computer and use it in GitHub Desktop.
LESS CSS FCGI script. Using this with a FastCGI server will mean you can serve your LESS files directly as CSS, compiling them on-the-fly as a request is made. Uses the content of the LESS source file to generate an Etag for Validation caching.
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
#!/usr/bin/ruby | |
require 'fcgi' | |
require 'less' | |
require 'digest/sha1' | |
FCGI.each do |request| | |
out = request.out | |
source = File.new(request.env["SCRIPT_FILENAME"], 'r').read; | |
hash = Digest::SHA1.hexdigest(source).inspect | |
# if request.env["HTTP_IF_NONE_MATCH"] == hash | |
# out.print request.env["SERVER_PROTOCOL"]+" 304 Not Modified\r\n" | |
# out.print "\r\n" | |
# else | |
Dir.chdir File.dirname request.env["SCRIPT_FILENAME"] | |
begin | |
code = Less::Parser.new.parse(source).to_css :compress => true | |
rescue Exception => err | |
out.print request.env["SERVER_PROTOCOL"]+"500 Internal Server Error\r\nContent-Type: text/css\r\n\r\n/* * *\r\n" | |
out.print err.message | |
out.print "\r\n* * */" | |
else | |
out.print "Content-Type: text/css\r\n" | |
out.print "Etag: "+hash+"\r\n" | |
out.print "\r\n" | |
out.print code | |
end | |
request.finish | |
end |
24/09/11: Made it report LESS parsing errors (in admittedly crappy way, compared to viewing errors with the lessc
command-line tool), and enabled minification of the compiled CSS. (remove :compress => true
from line 19 to disable)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
19/09/2011: Removed 304 stuff, which was causing problems for me. Also made it do a chdir to the location of the LESS file, so @import works properly.