Friday, January 25, 2008

My first Ruby Program - the Icon Indexer

Well.. I work a lot on eclipse and I always wanted to get the complete eclipse icon set from my eclipse directory (for my eclipse rich client applications) and have a nice index.html file to view the same. Decided to spin a ruby program to do the job.

(Yeah...I do know about ISharedImages.. but that doesn't give everything)


require 'find'

class IconIndexer
def index(base_dir)
idx_path = base_dir + '\index.html'
idxh = File.new(idx_path, "w")
puts "Created index file at " + File.expand_path(idx_path)
puts "Scanning image files (.gif/.png) from #{base_dir} ..."
idxh.puts <<-EOS
<html>
<head>
<title>Icon Index generated for #{base_dir} on #{Date.today()}</title>
</head>
<body>
<table>
EOS
cnt = 0;
ncols = 3;
icon_size = 3 * 1024;

# Scan base_dir get all gifs/png's less than 'icon_size' kb and
# create index html consisting of 'ncol' sized table with these images.
# ofcouse this is a dumb program since it doesn't take img dimentions into account
# but nvm.
Find.find(base_dir) do |path|

if File.extname(path) =~ /(.gif|.png)/ and File.size(path) < icon_size
if cnt % ncols == 0
idxh.puts "<tr>\n"
end
img_path = File.expand_path(path);
html = "<td><a href='#{img_path}'><img border='0' src='#{img_path}'/></a></td>"
cnt+=1
idxh.puts(html)
if cnt % ncols == 0
idxh.puts "</tr>\n"
end
end
end

idxh.puts <<-EOS
</table>
</body>
</html>
EOS

end
end

indexer = IconIndexer.new()
if ARGV[0]
indexer.index(ARGV[0])
else
script_name = File.basename(__FILE__);
puts "Usage: #{script_name} <search_dir>"
end