class ResizeCss
TODO: Needs some heavy refactoring and cleaning up!
Attributes
base_size[R]
new_size[R]
options[R]
size_prefix[R]
sprite_name[R]
stylesheet_folder[R]
stylesheet_name[R]
template_lang[R]
Public Class Methods
new(base_size, new_size, options)
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 14 def initialize base_size, new_size, options @base_size = base_size @new_size = new_size @options = options @stylesheet_folder = options[:stylesheet_folder] if options[:stylesheet_folder] @stylesheet_name = options[:stylesheet_name] if options[:stylesheet_name] @sprite_name = options[:sprite_name] if options[:sprite_name] @template_lang = options[:template_lang] if options[:template_lang] @size_prefix = options[:size_prefix] if options[:size_prefix] resize = "convert #{sprite_name}#{base_size}.png -resize #{factor * 100}%" new_file_name = "#{sprite_name}#{new_size}" command_resize = "#{resize} #{new_file_name}.png" command_resize_light = "#{resize} #{lighten} #{new_file_name}_light.png" command_resize_dark = "#{resize} #{darken} #{new_file_name}_dark.png" command_resize_gray = "#{resize} #{grayscale} #{new_file_name}_gray.png" if auto_exec? Open3.popen3("pwd") do |stdin, stdout, e, t| base_dir = stdout Open3.popen3 "cd #{images_dir}" Open3.popen3 command_resize Open3.popen3 command_resize_light if lighten? Open3.popen3 command_resize_dark if darken? Open3.popen3 command_resize_gray if gray? Open3.popen3 "cd #{base_dir}" end else puts "Use ImageMagick to resize #{sprite_name} sprites:" puts command_resize puts command_resize_light if lighten? puts command_resize_dark if darken? puts command_resize_gray if gray? end end
Public Instance Methods
auto_exec?()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 78 def auto_exec? options[:auto_exec] end
blank?(txt)
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 122 def blank? txt !txt || txt.empty? end
darken()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 70 def darken "-level 0%,100%,0.5" end
darken?()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 58 def darken? options[:dark] end
execute(options = {:ext => 'css', :semi => false})
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 126 def execute options = {:ext => 'css', :semi => false} semi = options[:semi] ? '-semi': '' ext = options[:ext] ? options[:ext] : 'css' src_file_path = File.expand_path "#{stylesheet_full_path}/#{stylesheet_name}#{base_size}#{semi}.#{ext}#{template_ext}", File.dirname(__FILE__) target_file_path = File.expand_path "#{stylesheet_full_path}/#{stylesheet_name}#{new_size}#{semi}.#{ext}#{template_ext}", File.dirname(__FILE__) puts "src: #{src_file_path} -> #{target_file_path}" lines = File.open(src_file_path).readlines img_exp = "#{sprite_name}-#{base_size}" replace_img = "#{sprite_name}-#{new_size}" # puts "img_exp: #{img_exp}" # puts "replace_img: #{replace_img}" repositioned_css = lines.map do |line| pos_match = line.match /.+position:\s-?(\d+)(px)?\s-?(\d+)(px)?/ # puts "pos_match: #{line} - #{pos_match.inspect}" img_match = line.match /#{img_exp}/ new_line = line.sub(/#{img_exp}/, replace_img) if img_match new_line = replace_pos pos_match, line if pos_match # puts "line: #{line} -> #{new_line}" new_line ||= line new_line end.join("") puts "Write new CSS:" puts repositioned_css File.open(target_file_path, 'w') do |f| f.puts repositioned_css.gsub ".#{size_prefix}#{base_size}", ".#{size_prefix}#{new_size}" end end
factor()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 161 def factor @factor ||= new_size / base_size.to_f end
gray?()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 62 def gray? options[:gray] end
grayscale()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 74 def grayscale "-colorspace Gray" end
images_dir()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 90 def images_dir @images_dirĀ ||= File.join(vendor_assets_path, 'images') end
lighten()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 66 def lighten "-level 0%,100%,2.0" end
lighten?()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 54 def lighten? options[:light] end
replace_pos(pos_match, line)
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 165 def replace_pos pos_match, line pos_x = pos_match[1].to_i new_pos_x = (pos_x * factor).to_i.to_s pos_y = pos_match[3].to_i new_pos_y = (pos_y * factor).to_i.to_s # puts "new pos: #{new_pos_x}, #{new_pos_y}" rest = line[6..-1] rest = rest.sub /#{pos_x}/, new_pos_x.to_s rest = rest.sub /#{pos_y}/, new_pos_y.to_s rest = rest.gsub /#{base_size}px;/, "#{new_size}px;" # first = line[0..10].sub(".f#{base_size}", ".f#{newsize}") line[0..5] + rest end
stylesheet_full_path()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 102 def stylesheet_full_path @stylesheet_full_path ||= File.join(stylesheets_dir, stylesheets_folder) end
stylesheets_dir()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 94 def stylesheets_dir @stylesheets_dir ||= File.join(vendor_assets_path, 'stylesheets') end
stylesheets_folder()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 98 def stylesheets_folder @stylesheets_folder ||= "flags" end
template_ext()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 118 def template_ext !blank?(template_lang) ? ".#{template_lang}" : '' end
vendor_assets_path()
click to toggle source
# File lib/world_flags/tools/resize_css.rb, line 86 def vendor_assets_path "../../../vendor/assets/" end