lib/smallvictories/compiler.rb in smallvictories-0.0.11 vs lib/smallvictories/compiler.rb in smallvictories-0.0.12

- old
+ new

@@ -1,30 +1,36 @@ require 'sprockets' require 'autoprefixer-rails' require 'liquid' require 'premailer' +require 'sprite_factory' +require 'rails-sass-images' module SmallVictories class Compiler attr_accessor :config def initialize attributes={} self.config = attributes[:config] end def compile_css - package [config.stylesheets] + package [config.stylesheets] if config.compile_css end def compile_js - package [config.javascripts] + package [config.javascripts] if config.compile_js end def compile_html - liquid + liquid if config.compile_html end + def compile_sprite + sprite if config.compile_sprite + end + def inline_html premail end def liquid @@ -45,11 +51,11 @@ pathname = Pathname.new(path) file_name = pathname.basename.to_s.split('.').first folder_path = pathname.dirname.to_s.gsub(config.full_source_path, '') full_output_path = File.join(config.full_destination_path, folder_path) - next if file_name =~ /^_/ # do not render partials + next if file_name =~ /^_/ # do not render partials or layout file = File.open(path).read liquid = Liquid::Template.parse(file) stylesheet_path = Pathname.new(config.full_destination_path) file_path = Pathname.new(full_output_path) @@ -78,10 +84,11 @@ environment.gzip = true environment.logger = SmallVictories.logger environment.js_compressor = options[:js_compressor] || :uglify environment.css_compressor = options[:css_compressor] || :sass end + RailsSassImages.install(sprockets) sprockets.append_path('.') bundles.each do |bundle| begin if assets = sprockets.find_asset(bundle.first) @@ -144,8 +151,26 @@ end rescue => e SmallVictories.logger.error "Inline Error\n#{e}" end end + end + + def sprite + css = "@import 'rails-sass-images';\n" + css += SpriteFactory.run!(File.join(config.full_source_path, config.source_sprite), + output_image: File.join(config.full_source_path, config.destination_sprite_file), + style: :scss, + margin: 20, + layout: :vertical, + nocss: true, + sanitizer: true) do |images| + images.map do |image_name, image_data| + ".sprite-#{image_name} { background-image: url('#{config.destination_sprite_file}'); background-size: (image-width('#{File.join(config.destination_sprite_file)}')/2) auto; background-repeat: no-repeat; background-position: (#{image_data[:cssx]}px/-2) (#{image_data[:cssy]}px/-2); height: (#{image_data[:cssh]}px/2) + 1px; width: (#{image_data[:cssw]}px/2) + 1px;}" + end.join("\n") + end + FileUtils.cp(File.join(config.full_source_path, config.destination_sprite_file), File.join(config.full_destination_path, config.destination_sprite_file)) + File.open(File.join(config.full_source_path, config.destination_sprite_style), 'w') { |file| file.write(css) } + SmallVictories.logger.info "compiled #{File.join(config.destination, config.destination_sprite_style)}" end end end