Skip to content

Instantly share code, notes, and snippets.

@dpk
Created June 15, 2011 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpk/1027863 to your computer and use it in GitHub Desktop.
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.
#!/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
@dpk
Copy link
Author

dpk commented Sep 19, 2011

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.

@dpk
Copy link
Author

dpk commented Sep 24, 2011

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