#!/usr/bin/ruby # This file provides a way to extract links to images from ruby files # converting the files and getting the images into static links. require 'optparse' require 'ostruct' require 'fileutils' options = OpenStruct.new opts = OptionParser.new do |opts| opts.banner = "$0 [options] [files]" opts.on("-l","--link LINK", "How to link the images from the files") do |l| options.link = l end opts.on("-d","--dir DIR", "Where to save images") do |w| options.dir = w end end opts.parse! options.dir ||= "images" options.link ||= options.dir files = ARGV.dup # The files to be processed if files.empty? open '.document' do |f| for line in f puts line files += Dir.glob(line.chomp) end end end p files # return nil for file in files puts file next if File.directory?(file) File.rename(file, file + ".old") open(file + ".old" ) do |f| o = file output = open(o, "w") for line in f if line =~ /(http:.*(jpg|gif|jpeg|png))/ image = $1 image =~ /.*\/(.*)/ file_name = $1 puts "Retrieving #{file_name}" system "wget -nv -c -O #{options.dir}/#{file_name} #{image}" # Then, conversion to PNG; we keep the PNG file if it is smaller # that the original file. png = file_name.sub(/(\.[^.]+)?$/, ".png") puts "Converting #{file_name} to #{png}" system "convert #{options.dir}/#{file_name} #{options.dir}/#{png}" FileUtils.rm "#{options.dir}/#{file_name}" output.print line.sub(image,"link:#{options.link}/#{png}") else output.print line end end end end