Class: ResizeCss
- Inherits:
-
Object
- Object
- ResizeCss
- Defined in:
- lib/world_flags/tools/resize_css.rb
Overview
TODO: Needs some heavy refactoring and cleaning up!
Instance Attribute Summary collapse
-
#base_size ⇒ Object
readonly
Returns the value of attribute base_size.
-
#new_size ⇒ Object
readonly
Returns the value of attribute new_size.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#size_prefix ⇒ Object
readonly
Returns the value of attribute size_prefix.
-
#sprite_name ⇒ Object
readonly
Returns the value of attribute sprite_name.
-
#stylesheet_folder ⇒ Object
readonly
Returns the value of attribute stylesheet_folder.
-
#stylesheet_name ⇒ Object
readonly
Returns the value of attribute stylesheet_name.
-
#template_lang ⇒ Object
readonly
Returns the value of attribute template_lang.
Instance Method Summary collapse
- #auto_exec? ⇒ Boolean
- #blank?(txt) ⇒ Boolean
- #darken ⇒ Object
- #darken? ⇒ Boolean
- #execute(options = {:ext => 'css', :semi => false}) ⇒ Object
- #factor ⇒ Object
- #gray? ⇒ Boolean
- #grayscale ⇒ Object
- #images_dir ⇒ Object
-
#initialize(base_size, new_size, options) ⇒ ResizeCss
constructor
A new instance of ResizeCss.
- #lighten ⇒ Object
- #lighten? ⇒ Boolean
- #replace_pos(pos_match, line) ⇒ Object
- #stylesheet_full_path ⇒ Object
- #stylesheets_dir ⇒ Object
- #stylesheets_folder ⇒ Object
- #template_ext ⇒ Object
- #vendor_assets_path ⇒ Object
Constructor Details
#initialize(base_size, new_size, options) ⇒ ResizeCss
Returns a new instance of ResizeCss.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/world_flags/tools/resize_css.rb', line 14 def initialize base_size, new_size, @base_size = base_size @new_size = new_size = @stylesheet_folder = [:stylesheet_folder] if [:stylesheet_folder] @stylesheet_name = [:stylesheet_name] if [:stylesheet_name] @sprite_name = [:sprite_name] if [:sprite_name] @template_lang = [:template_lang] if [:template_lang] @size_prefix = [:size_prefix] if [: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 |
Instance Attribute Details
#base_size ⇒ Object (readonly)
Returns the value of attribute base_size.
11 12 13 |
# File 'lib/world_flags/tools/resize_css.rb', line 11 def base_size @base_size end |
#new_size ⇒ Object (readonly)
Returns the value of attribute new_size.
11 12 13 |
# File 'lib/world_flags/tools/resize_css.rb', line 11 def new_size @new_size end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/world_flags/tools/resize_css.rb', line 12 def end |
#size_prefix ⇒ Object (readonly)
Returns the value of attribute size_prefix.
12 13 14 |
# File 'lib/world_flags/tools/resize_css.rb', line 12 def size_prefix @size_prefix end |
#sprite_name ⇒ Object (readonly)
Returns the value of attribute sprite_name.
11 12 13 |
# File 'lib/world_flags/tools/resize_css.rb', line 11 def sprite_name @sprite_name end |
#stylesheet_folder ⇒ Object (readonly)
Returns the value of attribute stylesheet_folder.
11 12 13 |
# File 'lib/world_flags/tools/resize_css.rb', line 11 def stylesheet_folder @stylesheet_folder end |
#stylesheet_name ⇒ Object (readonly)
Returns the value of attribute stylesheet_name.
11 12 13 |
# File 'lib/world_flags/tools/resize_css.rb', line 11 def stylesheet_name @stylesheet_name end |
#template_lang ⇒ Object (readonly)
Returns the value of attribute template_lang.
12 13 14 |
# File 'lib/world_flags/tools/resize_css.rb', line 12 def template_lang @template_lang end |
Instance Method Details
#auto_exec? ⇒ Boolean
78 79 80 |
# File 'lib/world_flags/tools/resize_css.rb', line 78 def auto_exec? [:auto_exec] end |
#blank?(txt) ⇒ Boolean
122 123 124 |
# File 'lib/world_flags/tools/resize_css.rb', line 122 def blank? txt !txt || txt.empty? end |
#darken ⇒ Object
70 71 72 |
# File 'lib/world_flags/tools/resize_css.rb', line 70 def darken "-level 0%,100%,0.5" end |
#darken? ⇒ Boolean
58 59 60 |
# File 'lib/world_flags/tools/resize_css.rb', line 58 def darken? [:dark] end |
#execute(options = {:ext => 'css', :semi => false}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/world_flags/tools/resize_css.rb', line 126 def execute = {:ext => 'css', :semi => false} semi = [:semi] ? '-semi': '' ext = [:ext] ? [:ext] : 'css' src_file_path = File. "#{stylesheet_full_path}/#{stylesheet_name}#{base_size}#{semi}.#{ext}#{template_ext}", File.dirname(__FILE__) target_file_path = File. "#{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 ⇒ Object
161 162 163 |
# File 'lib/world_flags/tools/resize_css.rb', line 161 def factor @factor ||= new_size / base_size.to_f end |
#gray? ⇒ Boolean
62 63 64 |
# File 'lib/world_flags/tools/resize_css.rb', line 62 def gray? [:gray] end |
#grayscale ⇒ Object
74 75 76 |
# File 'lib/world_flags/tools/resize_css.rb', line 74 def grayscale "-colorspace Gray" end |
#images_dir ⇒ Object
90 91 92 |
# File 'lib/world_flags/tools/resize_css.rb', line 90 def images_dir @images_dir |
#lighten ⇒ Object
66 67 68 |
# File 'lib/world_flags/tools/resize_css.rb', line 66 def lighten "-level 0%,100%,2.0" end |
#lighten? ⇒ Boolean
54 55 56 |
# File 'lib/world_flags/tools/resize_css.rb', line 54 def lighten? [:light] end |
#replace_pos(pos_match, line) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# 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 ⇒ Object
102 103 104 |
# 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 ⇒ Object
94 95 96 |
# File 'lib/world_flags/tools/resize_css.rb', line 94 def stylesheets_dir @stylesheets_dir ||= File.join(vendor_assets_path, 'stylesheets') end |
#stylesheets_folder ⇒ Object
98 99 100 |
# File 'lib/world_flags/tools/resize_css.rb', line 98 def stylesheets_folder @stylesheets_folder ||= "flags" end |
#template_ext ⇒ Object
118 119 120 |
# File 'lib/world_flags/tools/resize_css.rb', line 118 def template_ext !blank?(template_lang) ? ".#{template_lang}" : '' end |
#vendor_assets_path ⇒ Object
86 87 88 |
# File 'lib/world_flags/tools/resize_css.rb', line 86 def vendor_assets_path "../../../vendor/assets/" end |