Class: ResizeCss
- Inherits:
-
Object
- Object
- ResizeCss
- Defined in:
- lib/world_flags/tools/resize_css.rb
Overview
use ImageMagick to Resize, using 32px version as base convert flags32.png -resize 75% flags24.png convert flags32_semi.png -resize 75% flags24_semi.png
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
- #execute(options = {:ext => 'css', :semi => false}) ⇒ Object
- #factor ⇒ Object
-
#initialize(base_size, new_size, options) ⇒ ResizeCss
constructor
A new instance of ResizeCss.
- #replace_pos(pos_match, line) ⇒ Object
- #stylesheet_full_path ⇒ Object
- #stylesheet_path ⇒ Object
- #template_ext ⇒ Object
Constructor Details
#initialize(base_size, new_size, options) ⇒ ResizeCss
Returns a new instance of ResizeCss.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/world_flags/tools/resize_css.rb', line 10 def initialize base_size, new_size, @base_size = base_size @new_size = new_size @options = @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] command1 = "convert #{sprite_name}#{base_size}.png -resize #{factor * 100}% #{sprite_name}#{new_size}.png" command2 = "convert #{sprite_name}#{base_size}_semi.png -resize #{factor * 100}% #{sprite_name}#{new_size}_semi.png" if auto_exec? %x[command1] %x[command2] else puts "Use ImageMagick to resize #{sprite_name} sprites:" puts command1 puts command2 end end |
Instance Attribute Details
#base_size ⇒ Object (readonly)
Returns the value of attribute base_size.
7 8 9 |
# File 'lib/world_flags/tools/resize_css.rb', line 7 def base_size @base_size end |
#new_size ⇒ Object (readonly)
Returns the value of attribute new_size.
7 8 9 |
# File 'lib/world_flags/tools/resize_css.rb', line 7 def new_size @new_size end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/world_flags/tools/resize_css.rb', line 8 def @options end |
#size_prefix ⇒ Object (readonly)
Returns the value of attribute size_prefix.
8 9 10 |
# File 'lib/world_flags/tools/resize_css.rb', line 8 def size_prefix @size_prefix end |
#sprite_name ⇒ Object (readonly)
Returns the value of attribute sprite_name.
7 8 9 |
# File 'lib/world_flags/tools/resize_css.rb', line 7 def sprite_name @sprite_name end |
#stylesheet_folder ⇒ Object (readonly)
Returns the value of attribute stylesheet_folder.
7 8 9 |
# File 'lib/world_flags/tools/resize_css.rb', line 7 def stylesheet_folder @stylesheet_folder end |
#stylesheet_name ⇒ Object (readonly)
Returns the value of attribute stylesheet_name.
7 8 9 |
# File 'lib/world_flags/tools/resize_css.rb', line 7 def stylesheet_name @stylesheet_name end |
#template_lang ⇒ Object (readonly)
Returns the value of attribute template_lang.
8 9 10 |
# File 'lib/world_flags/tools/resize_css.rb', line 8 def template_lang @template_lang end |
Instance Method Details
#auto_exec? ⇒ Boolean
34 35 36 |
# File 'lib/world_flags/tools/resize_css.rb', line 34 def auto_exec? [:auto_exec] end |
#execute(options = {:ext => 'css', :semi => false}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/world_flags/tools/resize_css.rb', line 70 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}/flags/flags#{new_size}#{semi}.#{ext}#{template_ext}", File.dirname(__FILE__) lines = File.open(src_file_path).readlines img_exp = "#{sprite_name}#{base_size}" replace_img = "#{sprite_name}#{new_size}" repositioned_css = lines.map do |line| pos_match = line.match /.+position:0 -(\d+)px.*/ 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 new_line ||= line new_line end.join("") 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
95 96 97 |
# File 'lib/world_flags/tools/resize_css.rb', line 95 def factor @factor ||= new_size / base_size end |
#replace_pos(pos_match, line) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/world_flags/tools/resize_css.rb', line 99 def replace_pos pos_match, line pos_y = pos_match[1].to_i new_pos_y = (pos_y * factor).to_i.to_s # puts "pos: #{pos_y} -> #{new_pos_y}" rest = line[10..-1] rest = rest.sub /#{pos_y}/, new_pos_y.to_s # first = line[0..10].sub(".f#{base_size}", ".f#{newsize}") line[0..10] + rest end |
#stylesheet_full_path ⇒ Object
50 51 52 |
# File 'lib/world_flags/tools/resize_css.rb', line 50 def stylesheet_full_path @stylesheet_full_path ||= File.join(stylesheet_path, stylesheet_folder) end |
#stylesheet_path ⇒ Object
42 43 44 |
# File 'lib/world_flags/tools/resize_css.rb', line 42 def stylesheet_path "../../../vendor/assets/stylesheets" end |
#template_ext ⇒ Object
66 67 68 |
# File 'lib/world_flags/tools/resize_css.rb', line 66 def template_ext template_lang ? ".#{template_lang}" : '' end |