Sha256: aa7046b0547eccbef440c34b42cb56a11691ce01539d7cdba774087bf962e536

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

require 'shine'
require 'sass'

module Spontaneous
  module Output
    module Assets
      module Compression
        extend self

        def compress_js(filelist, options={})
          shine_compress_files(filelist, :js, options)
        end

        def compress_js_string(js, options={})
          shine_compress_string(js, :js, options)
        end

        def compress_css(filelist, options={})
          # compress_files(filelist, :css, options)
          opts = {
            :load_paths => [Spontaneous.css_dir],
            # :filename => sass_template,
            :cache => false,
            :style => :compressed
          }.merge(options)
          css = filelist.map do |path|
            Sass::Engine.for_file(path, opts).render
          end.join("\n")
          hash = digest(css)
          [css, hash]
        end

        def shine_compress_string(string, format, options = {})
          compressed = Shine::compress_string(string, format, options).force_encoding("UTF-8")
          hash = digest(compressed)
          [compressed, hash]
        end

        def shine_compress_files(filelist, format, options = {})
          original_size = filesize(filelist)
          compressed = Shine::compress_files(filelist, format, options).force_encoding("UTF-8")
          logger.info("Compressed #{filelist.length} files. Original size #{original_size}, compressed size #{compressed.length}, ratio #{(100*compressed.length.to_f/original_size.to_f).round}%")
          hash = digest(compressed)
          [compressed, hash]
        end

        def digest(str)
          hash = Digest::SHA1.new.update(str).hexdigest
        end


        def filesize(paths)
          paths.compact.inject(0) { |sum, path| sum += File.size(path) }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta1 lib/spontaneous/output/assets/compression.rb
spontaneous-0.2.0.alpha7 lib/spontaneous/output/assets/compression.rb
spontaneous-0.2.0.alpha6 lib/spontaneous/output/assets/compression.rb
spontaneous-0.2.0.alpha5 lib/spontaneous/output/assets/compression.rb
spontaneous-0.2.0.alpha4 lib/spontaneous/output/assets/compression.rb
spontaneous-0.2.0.alpha3 lib/spontaneous/output/assets/compression.rb