#!/usr/bin/env ruby require 'yaml' require 'optparse' require 'digest' require 'mini_magick' require 'fileutils' require 'erb' def copyimage(path, file, target, extension) hash = Digest::MD5.file(file).hexdigest hash = Digest::MD5.hexdigest(hash.to_s + target) filename = "#{hash}.#{extension}" dest = File.join(path, filename) if File.exist?(dest) puts "Skipping" return filename end puts "Writing #{dest}" # Resize image and write it to destination image = MiniMagick::Image.open(file) image.resize(target) image.format(extension) image.write(dest) return filename end def forImgs(imgs, &block) if imgs.respond_to?("each") imgs.each &block else block.call(imgs) end end def placeImage(file, src, href = nil) if href file.puts "" end file.puts "" if href file.puts "" end end options = {} OptionParser.new do |opts| opts.banner = "Usage: build.rb dest" opts.on("-d", "--dest PATH", "PATH to put build files") do |path| options[:path] = path end opts.on("-s", "--size SIZE", "Size to resize images to") do |size| options[:size] = size end opts.on("-g", "--commit HASH", "Commit to display on website") do |hash| options[:commit] = hash end opts.on("-c", "--clean", "Cleanup unused images") do |v| options[:clean] = true end opts.on("-b", "--linkbase URL", "Add links to images, with URL as base") do |url| options[:base] = url end opts.on("-e", "--extension EXT", "Image extension to use with imagemagick") do |ext| options[:extension] = ext end end.parse! options[:path] = "build" unless options[:path] options[:size] = "1920x1080" unless options[:size] options[:clean] = false unless options[:clean] options[:extension] = "jpg" unless options[:extension] puts options # Create build dir FileUtils.mkdir_p(options[:path]) defs = YAML.load(File.read("imginfo.yml")) files = {} puts defs defs["groups"].each do |value| # For each image inside group forImgs(value["imgs"]) do |img| puts "Converting file #{img}" dest = copyimage(options[:path], img, options[:size], options[:extension]) files[img] = dest end end puts files template = File.read("index.tmpl") template = template.split("%BODY%") # Cleanup old files if options[:clean] Dir.glob(File.join(options[:path], "*")) do |file| hash = File.basename(file) if !files.values.include? hash puts "Cleaning #{hash}" File.delete(file) end end end File.open(File.join(options[:path], "index.html"), "w") do |file| # Print preample file.write(template[0]) defs["groups"].each do |value| file.puts "
" if value[:title] file.puts "

#{value[:title]}

" end forImgs(value["imgs"]) do |img| href = options[:base] # If href is given add a filename at the end if href href = File.join(href, img) end placeImage(file, files[img], href) end if value["what"] file.puts "

#{value["what"]}" end file.puts "

" end if options[:commit] file.puts "

Build from commit #{options[:commit]}

" end # Print postampleee? file.write(template[1]) end