Module: SqweezeUtils
- Included in:
- Compressor, ConfManager, DOMCompiler
- Defined in:
- lib/sqweezeUtils.rb
Constant Summary collapse
- EMBED_MIME_TYPES =
Mapping from extension to mime-type of all embeddable assets.
{ '.png' => 'image/png', '.jpg' => 'image/jpeg', '.jpeg' => 'image/jpeg', '.gif' => 'image/gif', '.tif' => 'image/tiff', '.tiff' => 'image/tiff', '.ttf' => 'font/truetype', '.otf' => 'font/opentype' }
Instance Method Summary collapse
- #ansi_bold(bold) ⇒ Object
- #ansi_green(green) ⇒ Object
- #ansi_nocolour(text) ⇒ Object
- #ansi_red(red) ⇒ Object
- #ansi_yellow(yellow) ⇒ Object
-
#byteweight(path_or_string) ⇒ Object
Gets the byte weight of input strames, wether these are file paths or just strings.
- #compression_percentage(before_bweight, after_bweight) ⇒ Object
-
#encoded_contents(asset_path) ⇒ Object
Return the Base64-encoded contents of an asset on a single line.
-
#find_file_in_targetdir(endpath) ⇒ Object
TODO: rewrite this! (it sucks..).
-
#mime_type(asset_path) ⇒ Object
Grab the mime-type of an asset, by filename.
-
#notify(message, log_level = :info) ⇒ Object
Prints to STDERR and adds a log entry.
-
#remap_filepath(path) ⇒ Object
Remaps a file in the source directory to its compressed version in the target directory.
- #write_file(fbody, fpath) ⇒ Object
Instance Method Details
#ansi_bold(bold) ⇒ Object
105 106 107 |
# File 'lib/sqweezeUtils.rb', line 105 def ansi_bold(bold) "\033[1m#{bold}\033[0m" end |
#ansi_green(green) ⇒ Object
111 112 113 |
# File 'lib/sqweezeUtils.rb', line 111 def ansi_green(green) "\033[32m#{green}\033[0m" end |
#ansi_nocolour(text) ⇒ Object
117 118 119 |
# File 'lib/sqweezeUtils.rb', line 117 def ansi_nocolour(text) text.gsub(/\033\[[0-9]{1,2}/,'') end |
#ansi_red(red) ⇒ Object
108 109 110 |
# File 'lib/sqweezeUtils.rb', line 108 def ansi_red(red) "\033[31m#{red}\033[0m" end |
#ansi_yellow(yellow) ⇒ Object
114 115 116 |
# File 'lib/sqweezeUtils.rb', line 114 def ansi_yellow(yellow) "\033[33m#{yellow}\033[0m" end |
#byteweight(path_or_string) ⇒ Object
Gets the byte weight of input strames, wether these are file paths or just strings
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sqweezeUtils.rb', line 55 def byteweight(path_or_string) path_or_string="" if path_or_string.nil? if File.exists?(path_or_string) File.size(path_or_string) else bweight=0 path_or_string.each_byte{|b|bweight+=1} bweight end end |
#compression_percentage(before_bweight, after_bweight) ⇒ Object
67 68 69 |
# File 'lib/sqweezeUtils.rb', line 67 def compression_percentage(before_bweight,after_bweight) sprintf('%.2f',after_bweight/before_bweight.to_f * 100) end |
#encoded_contents(asset_path) ⇒ Object
Return the Base64-encoded contents of an asset on a single line.
47 48 49 50 |
# File 'lib/sqweezeUtils.rb', line 47 def encoded_contents(asset_path) data = open(asset_path, 'rb'){|f| f.read } Base64.encode64(data).gsub(/\n/, '') end |
#find_file_in_targetdir(endpath) ⇒ Object
TODO: rewrite this! (it sucks..)
78 79 80 81 82 |
# File 'lib/sqweezeUtils.rb', line 78 def find_file_in_targetdir(endpath) pattern= [get_confManager.target_dir,"**/*#{File.extname(endpath)}"].join('/') #puts "searcing for files in #{pattern} ending with #{endpath}" Dir[pattern].find{|path| path =~ Regexp.new("#{endpath}$")} end |
#mime_type(asset_path) ⇒ Object
Grab the mime-type of an asset, by filename.
24 25 26 |
# File 'lib/sqweezeUtils.rb', line 24 def mime_type(asset_path) EMBED_MIME_TYPES[File.extname(asset_path)] if asset_path end |
#notify(message, log_level = :info) ⇒ Object
Prints to STDERR and adds a log entry.
Log entries can be of four types:
-
info
-
warning
-
error
-
debug
95 96 97 98 99 100 101 102 103 |
# File 'lib/sqweezeUtils.rb', line 95 def notify( , log_level=:info) raise "Log level #{log_level} does not exist!" unless [:info,:warn,:error,:debug].include?(log_level.to_sym) cm=get_confManager $log=Logger.new( "#{cm.target_dir}/sqweeze.log" ) unless $log $log.send(log_level, ansi_nocolour()) puts unless cm.get_conf( "suppress_#{log_level}".to_sym) end |
#remap_filepath(path) ⇒ Object
Remaps a file in the source directory to its compressed version in the target directory
if the resource is an absolute URL fine, Otherwise, if the resource is a relative url in the source dir, try to map it to its compressed version in the target directory
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sqweezeUtils.rb', line 35 def remap_filepath(path) path=Pathname.new(path) parent_dir, path_basename = path.parent, path.basename is_absolute = URI.parse(path).absolute? unless is_absolute find_file_in_targetdir( [parent_dir, path_basename].join('/')) else path.to_s end end |
#write_file(fbody, fpath) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/sqweezeUtils.rb', line 16 def write_file(fbody,fpath) File.open(fpath,'w') do |f| f.write(fbody) f.close end end |