Skip to content

Instantly share code, notes, and snippets.

@robertjwhitney
Forked from seth/yui_compressor.rb
Created April 28, 2011 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertjwhitney/946486 to your computer and use it in GitHub Desktop.
Save robertjwhitney/946486 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