Skip to content

Instantly share code, notes, and snippets.

@seth
Created January 11, 2011 04:23
Show Gist options
  • Save seth/774015 to your computer and use it in GitHub Desktop.
Save seth/774015 to your computer and use it in GitHub Desktop.
A Nanoc filter for minifying CSS and Javascript using the YUI Compressor
YUI_JAR = File.dirname(__FILE__) + "/../tools/yuicompressor-2.4.2.jar"
class YuiCompressor < Nanoc3::Filter
identifier :yui_compress
type :text => :binary
def run(content, params={})
type = type_from_extension
cmd = "java -jar #{YUI_JAR} --type #{type} -o #{output_filename}"
IO.popen(cmd, 'w') { |f| f.write(content) }
raise "yuicompressor exited with #{$?} for '#{cmd}'" unless $? == 0
end
def type_from_extension
case @item[:extension]
when /^css/
"css"
when /^js/
"js"
else
raise "unknown type for yuicompressor '#{@item[:extension]}'"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment