Skip to content

Instantly share code, notes, and snippets.

@rickumali
Created April 25, 2015 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickumali/bad0be8fad81e03ae207 to your computer and use it in GitHub Desktop.
Save rickumali/bad0be8fad81e03ae207 to your computer and use it in GitHub Desktop.
Small Ruby to Renumber Figures
#!/usr/bin/env ruby
#
require 'pp'
require 'optparse'
require 'pry'
options={}
options[:verbose] = false
options[:figures_list] = 'master_figure_list.csv'
options[:chapter_list] = nil
options[:orig_ext] = 'png'
options[:target_ext] = 'png'
OptionParser.new do |opts|
opts.banner = "Usage: work_figures.rb [OPTIONS] [make_csv|check_files|make_image_dir]\n"
opts.on('-v', "--[no-]verbose", "Run verbosely (or not)") do |v|
options[:verbose] = v
end
opts.on('-f', "--figures-list [FILE]", "Figure list (default: master_figure_list.csv)") do |f|
options[:figures_list] = f
end
opts.on('-c', "--chapter-list C1,C2,...CN", Array, "List of chapters") do |chap_list|
chap_list = chap_list.map {|ch| ch.to_i}
chap_list = chap_list.sort
options[:chapter_list] = chap_list
end
opts.on("--orig_ext [EXT]", "Original extension (default: png)") do |o|
options[:orig_ext] = o
options[:target_ext] = o
end
opts.on("--target_ext [EXT]", "Target extension (default: value of --orig_ext)") do |t|
options[:target_ext] = t
end
end.parse!
if options[:verbose]
$stderr.puts options
$stderr.puts ARGV
end
# The figures_list is a file that contains
# NN
# filename1[.ext]
# filename2[.ext]
# ...
# filenameN[.ext]
# NN
# filename1[.ext]
# filename2[.ext]
# ...
#
# NN represents the chapter
# files are given a filename and an extension or not
#
chapter_figures=Hash.new { |hash,key| hash[key] = Array.new }
chapter=nil
figure_count=0
File.open(options[:figures_list]).each_line do |line|
if line =~ /^#/
next
end
if m = /^(?<chapter>\d\d)$/.match(line)
chapter = m['chapter']
$stderr.puts "Found #{chapter}" if options[:verbose]
else
chapter_figures[chapter] << line.chomp
figure_count+=1
end
end
if options[:verbose]
# http://www.thecodingforums.com/threads/prettyprint-or-pp-questions.818171/
# Dumps the pp output to STDERR
PP.pp(chapter_figures,STDERR)
end
$stderr.puts "#{figure_count} figures over #{chapter_figures.keys.length} chapters in #{options[:figures_list]}"
def check_file_in_dir(dir, f)
File.exist?(dir + "/" + f)
end
def fig_name(f, def_ext)
filecomps = f.split(/\./)
if filecomps.length == 1
# No period detected. Assume that f is a filename. Append the def_ext
f = "#{f}.#{def_ext}"
elsif filecomps.length == 2
f
else
$stderr.puts "WARNING: #{f} has more than one period in the name"
return f # TODO Check this logic
end
if check_file_in_dir("screen-captures",f)
"screen-captures/#{f}"
elsif check_file_in_dir("line-art",f)
"line-art/#{f}"
else
nil
end
end
def manning_name(chap, counter, def_ext)
"#{chap}_#{"%02d" % counter}.#{def_ext}"
end
def make_csv(fig, chap, counter, orig_ext, target_ext)
# TODO: If fig has extension, then don't emit :orig_ext. Make target the same
# Use a function to get the extension, then pass it into manning_name
puts "#{fig_name(fig,orig_ext)},#{manning_name(chap,counter,target_ext)}"
end
# This will check for the existence of a graphics
# file in two subdirectories: screen-captures and line-art.
def check_file(fig, chap, counter, orig_ext)
fig_name(fig,orig_ext)
end
def copy_file(fig, chap, counter, orig_ext, target_ext, dir)
orig = fig_name(fig,orig_ext)
target = manning_name(chap,counter,target_ext)
FileUtils.cp orig, "#{dir}/#{target}"
end
if ARGV[0].nil?
$stderr.puts "Pass in a work argument:"
$stderr.puts " make_csv: Make a CSV of the file mapping to its chapter/figure name"
$stderr.puts " check_files: Check for existence of files"
$stderr.puts " make_image_dir: Generate an image directory with Manning naming scheme"
exit
end
num_figures = 0
chapter_figures.each_pair do |chap,list_of_figs|
chap_num = chap.to_i
if !options[:chapter_list].nil? and options[:chapter_list].index(chap_num).nil?
next
end
counter = 0
list_of_figs.each do |fig|
counter += 1
num_figures += 1
case ARGV[0]
when "make_csv"
make_csv(fig,chap,counter,options[:orig_ext],options[:target_ext])
when "check_files"
f = check_file(fig,chap,counter,options[:orig_ext])
if f.nil?
puts "#{fig} DOES NOT exist"
else
puts "#{f} exists"
end
when "make_image_dir"
dir = "images.#{Process.pid}"
if !Dir.exists?(dir)
$stderr.puts "Creating #{dir} directory for images"
FileUtils.mkdir dir
end
copy_file(fig,chap,counter,options[:orig_ext],options[:target_ext],dir)
end
end
end
$stderr.puts "Processed #{num_figures} figures"
@rickumali
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment