#!/usr/bin/ruby # # * BOOH * # # A.k.a `Best web-album Of the world, Or your money back, Humerus'. # # The acronyn sucks, however this is a tribute to Dragon Ball by # Akira Toriyama, where the last enemy beaten by heroes of Dragon # Ball is named "Boo". But there was already a free software project # called Boo, so this one will be it "Booh". Or whatever. # # # Copyright (c) 2004 Guillaume Cottenceau # # This software may be freely redistributed under the terms of the GNU # public license version 2. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. require 'getoptlong' require 'html_merges' #- options $options = [ [ '--help', '-h', GetoptLong::NO_ARGUMENT, "Get help message" ], [ '--no-check', '-n', GetoptLong::NO_ARGUMENT, "Don't check for needed external programs at startup" ], [ '--source', '-s', GetoptLong::REQUIRED_ARGUMENT, "Directory which contains original images/videos as files or subdirs" ], [ '--destination', '-d', GetoptLong::REQUIRED_ARGUMENT, "Directory which will contain the web-album" ], [ '--clean', '-c', GetoptLong::NO_ARGUMENT, "Clean destination directory" ], [ '--theme', '-t', GetoptLong::REQUIRED_ARGUMENT, "Select HTML theme to use" ], [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, "Set max verbosity level (0: warnings, 1: important messages, " + "2: other messages)" ], ] #- default values for some globals $theme = 'simple' $convert = 'convert -interlace line +profile \"*\"' $verbose_level = 2 def usage puts "Usage: #{File.basename($0)} [OPTION]..." $options.each { |ary| printf " %3s, %-15s %s\n", ary[1], ary[0], ary[3] } end def msg(verbose_level, msg) if verbose_level <= $verbose_level if verbose_level == 0 warn "Warning: #{msg}" else puts msg end end end def die(msg) puts msg exit 1 end def handle_options parser = GetoptLong.new parser.set_options(*$options.collect { |ary| ary[0..2] }) begin parser.each_option do |name, arg| case name when '--help' usage exit(0) when '--no-check' $no_check = true when '--source' $source = arg if !File.directory?($source) die "Argument to --source must be a directory" end when '--destination' $dest = arg if File.exists?($dest) && !File.directory?($dest) die "If --destination exists, it must be a directory" end when '--clean' system("rm -rf #{$dest}") when '--theme' $theme = arg when '--verbose-level' $verbose_level = arg.to_i end end rescue usage exit(1) end if !$source die 'Missing --source parameter.' end if !$dest die 'Missing --destination parameter.' end end def check_installation if $no_check return end %w(convert transcode exif).each { |prg| if !system("which #{prg} >/dev/null") die "The `#{prg}' program is typically needed. Re-run with --no-check if you're sure you're fine without it." end } end def replace_line(prefix, keyword, line) begin contents = eval "$#{keyword}" line.sub!(/#{prefix}#{keyword}/, contents) rescue NameError die "No `#{keyword}' found for substitution" end end def build_html_skeleton theme_file = File.open("themes/#{$theme}/skeleton.html").readlines msg 2, "Read theme `#{$theme}'" for line in theme_file while line =~ /~~~(\w+)/ replace_line('~~~', $1, line) end end return theme_file end def sys(cmd) msg 1, cmd system(cmd) end def walk_source_dir `find #{$source} -type d`.each { |dir| dir.chomp! msg 2, "Examining #{dir}..." images = Dir["#{dir}/*.{jpg,jpeg,gif,bmp,png}"] msg 2, "\t#{images.length} images" videos = Dir["#{dir}/*.{mov,avi,mpg,mpeg}"] msg 2, "\t#{videos.length} videos" dest_dir = dir.sub(/^#{$source}/, $dest) system("mkdir -p #{dest_dir}") msg 2, "Outputting in #{dest_dir}..." final_images = [] images.each { |img| dest_img = img.sub(/^#{$source}/, $dest). sub(/\.[^\.]+$/, '') + '-704.jpg' final_images << File.basename(dest_img) if !File.exists?(dest_img) sys "#{$convert} -geometry 704x528 #{img} #{dest_img}" end } html = $skeleton.collect { |l| l.clone } images4js = final_images.collect { |img| "\"#{img}\"" }.join(', ') for i in html i.sub!(/~~images/, images4js) end File.open("#{dest_dir}/index.html", "w").write(html) } end handle_options check_installation $skeleton = build_html_skeleton walk_source_dir