1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#!/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
|