minicomic-backend/classes/FileProcessor.rb

194 lines
5.0 KiB
Ruby
Raw Normal View History

2010-01-08 01:30:48 +00:00
class FileProcessor
2010-01-08 02:46:03 +00:00
attr_accessor :config, :paginated_source_files, :page_index
def initialize(config)
@page_index = 0
@config = config
@paginated_source_files = {}
end
2010-01-08 01:30:48 +00:00
def process
2010-01-10 14:54:53 +00:00
@config['Global']['files'].each do |filename|
2010-01-08 02:46:03 +00:00
ok, fileinfo, filename = verify_filename(filename)
2010-01-08 01:30:48 +00:00
if ok
filename_display = (filename.instance_of? Array) ? filename.join(", ") : filename
puts "Examining #{filename_display}..."
config.each do |type, info|
if type != "Global"
fileinfo_key = (filename.instance_of? Array) ? filename.join(",") : filename
2010-01-10 14:54:53 +00:00
file_fileinfo = (@config['Global']['fileinfo_by_file'][fileinfo_key]) ? @config['Global']['fileinfo_by_file'][fileinfo_key] : {}
2010-01-08 01:30:48 +00:00
2010-01-08 04:06:11 +00:00
file_fileinfo = info.dup.merge(fileinfo).merge(file_fileinfo)
2010-01-08 01:30:48 +00:00
2010-01-08 04:06:11 +00:00
input_obj, output_obj, targets = construct_filters_and_targets(filename, file_fileinfo, ok)
2010-01-08 01:30:48 +00:00
2010-01-10 00:14:43 +00:00
if determine_rebuild(targets, filename)
2010-01-08 01:30:48 +00:00
any_rebuilt = true
puts "Rebuilding #{filename_display} (#{type})..."
puts " Using #{filename} as a source"
puts " and writing to #{targets.inspect}"
2010-01-10 14:54:53 +00:00
do_build(targets, filename, input_obj, output_obj)
2010-01-08 01:30:48 +00:00
end
if info['is_paginated']
2010-01-10 14:54:53 +00:00
if !@paginated_source_files[type]; @paginated_source_files[type] = []; end
@paginated_source_files[type] << targets
2010-01-08 01:30:48 +00:00
end
end
end
page_index += 1
end
end
config.each do |type, info|
if info['is_paginated']
output = TempBitmapToPaginatedPrint
output_obj = output.instance
output_obj.config = info.dup
output_obj.paginate(paginated_source_files[type].flatten)
end
end
end
2010-01-08 02:46:03 +00:00
def verify_filename(filename)
ok = true
fileinfo = {}
if filename.instance_of? Hash
if filename['blank']
ok = false
@config.each do |type, info|
if info['is_paginated']
if !@paginated_source_files[type]
@paginated_source_files[type] = []
end
@paginated_source_files[type] << nil
end
end
@page_index += 1
else
if filename['file']
fileinfo = filename
filename = fileinfo['file']
else
ok = false
end
end
else
if @config['Global']
if @config['Global']['match']
ok = Regexp.new(@config['Global']['match']).match(filename)
end
end
end
[ ok, fileinfo, filename ]
end
2010-01-08 04:06:11 +00:00
def build_filename_parts(match_data)
filename_parts = {
'page_index' => sprintf(@config['Global']['page_index_format'], @page_index)
}
if match_data.is_a? MatchData
all, index, title = match_data.to_a
else
index = @page_index - 1
title = ""
end
if @config['Global']['title']
title = @config['Global']['title'].gsub("{index}", index).gsub("{title}", title)
end
filename_parts['index'] = index.to_i
filename_parts['title'] = title
filename_parts
end
def construct_filters_and_targets(filename, info, match_data)
2010-01-10 00:14:43 +00:00
input = nil; output = nil
2010-01-08 04:06:11 +00:00
extension = File.extname((filename.instance_of? Array) ? filename[0] : filename).downcase
case extension
when ".svg"
case File.extname(info['target']).downcase
when ".jpg", ".jpeg", ".png", ".gif"
input = SVGToTempBitmap
output = TempBitmapToWeb
when ".pdf"
input = SVGToTempBitmap
output = (info['is_paginated']) ? TempBitmapToPaginatedPrint : TempBitmapToPrint
end
end
if !input; raise "No input handler for #{extension} defined"; end
if !output; raise "No output handler for #{File.extname(info['target']).downcase} defined"; end
input_obj = input.new
input_obj.config = info
output_obj = output.new
output_obj.config = info
if info['is_paginated']
output_obj.config['target'] += "-{page_index}.png"
end
targets = output_obj.targets(build_filename_parts(match_data))
[ input_obj, output_obj, targets ]
end
2010-01-10 00:14:43 +00:00
def file_mtime(file)
File.mtime(file)
end
def determine_rebuild(targets, filename)
rebuild = false
[ targets ].flatten.each do |t|
if !File.exists?(t)
rebuild = true
break
else
[ filename ].flatten.each do |f|
if File.basename(f) != "blank"
if file_mtime(f) > file_mtime(t)
rebuild = true
break
end
end
end
end
end
rebuild
end
def do_build(targets, filename, input_obj, output_obj)
tmp_files = input_obj.build(filename)
case tmp_files.class.to_s
when "String"
output_obj.build(tmp_files, targets)
when "Array"
[0, 1].each do |i|
output_obj.build(tmp_files[i], targets[i], (i == 0) ? "left" : "right")
end
end
input_obj.cleanup
end
2010-01-08 01:30:48 +00:00
end