diff --git a/classes/FileProcessor.rb b/classes/FileProcessor.rb index 78f1fa7..32bc4b8 100644 --- a/classes/FileProcessor.rb +++ b/classes/FileProcessor.rb @@ -9,7 +9,6 @@ class FileProcessor def process - rsync_files_by_target = {} files.each do |filename| @@ -20,21 +19,6 @@ class FileProcessor puts "Examining #{filename_display}..." - filename_parts = { - 'page_index' => sprintf(page_index_format, @page_index) - } - - if @config['Global']['match'] - all, index, title = ok.to_a - else - index = page_index - 1 - title = "" - end - - if global['title']; title = global['title'].gsub("{index}", index).gsub("{title}", title); end - filename_parts['index'] = index - filename_parts['title'] = title - config.each do |type, info| if type != "Global" input = nil; output = nil @@ -43,34 +27,9 @@ class FileProcessor file_fileinfo = (fileinfo_by_file[fileinfo_key]) ? fileinfo_by_file[fileinfo_key] : {} - extension = File.extname((filename.instance_of? Array) ? filename[0] : filename).downcase + file_fileinfo = info.dup.merge(fileinfo).merge(file_fileinfo) - case extension - when ".svg" - case File.extname(config[type]['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(config[type]['target']).downcase} defined"; end - - input_obj = input.instance - input_obj.config = info.dup.merge(fileinfo).merge(file_fileinfo) - - output_obj = output.instance - output_obj.config = info.dup.merge(fileinfo).merge(file_fileinfo) - - if info['is_paginated'] - output_obj.config['target'] += "-{page_index}.png" - end - - targets = output_obj.targets(filename_parts) + input_obj, output_obj, targets = construct_filters_and_targets(filename, file_fileinfo, ok) rebuild = false @@ -176,4 +135,59 @@ class FileProcessor [ ok, fileinfo, filename ] end + + 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) + 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 end diff --git a/tests/TestFileProcessor.rb b/tests/TestFileProcessor.rb new file mode 100644 index 0000000..e358541 --- /dev/null +++ b/tests/TestFileProcessor.rb @@ -0,0 +1,84 @@ +require 'rubygems' +require 'test/unit' +require File.dirname(__FILE__) + '/../classes/FileProcessor.rb' + +class TestFileProcessor < Test::Unit::TestCase + def test_verify_filename + [ + [ {}, {}, [ false, {}, {} ] ], + [ + { 'file' => 'test' }, + {}, + [ true, { 'file' => 'test' }, 'test' ] + ], + [ + { 'blank' => true }, + { 'Meow' => { 'is_paginated' => false } }, + [ false, {}, { 'blank' => true } ], + ], + [ + { 'blank' => true }, + { 'Meow' => { 'is_paginated' => true } }, + [ false, {}, { 'blank' => true } ], + { + 'Meow' => [ nil ] + } + ], + [ + 'test', + {}, + [ true, {}, 'test' ] + ], + [ + 'test.svg', + { + 'Global' => { 'match' => '.*\.svg' } + }, + [ Regexp.new('.*\.svg').match('test.svg'), {}, 'test.svg' ] + ] + ].each do |filename, config, expected_return, expected_paginated_source_files| + file_processor = FileProcessor.new(config) + + result = file_processor.verify_filename(filename) + if expected_return[0].is_a? MatchData + assert_equal expected_return[1..-1], result[1..-1] + assert_equal expected_return[0].to_a, result[0].to_a + else + assert_equal expected_return, result + end + + if expected_paginated_source_files + assert_equal expected_paginated_source_files, file_processor.paginated_source_files + end + end + end + + def test_build_filename_parts + [ + [ + { 'Global' => { 'page_index_format' => '%02d' } }, + nil, + { 'index' => 9, 'title' => '', 'page_index' => '10' } + ], + [ + { 'Global' => { 'page_index_format' => '%02d' } }, + Regexp.new('(.*)-(.*)').match('10-test'), + { 'index' => 10, 'title' => 'test', 'page_index' => '10' } + ], + [ + { 'Global' => { 'page_index_format' => '%02d', 'title' => '{index}-{title}' } }, + Regexp.new('(.*)-(.*)').match('10-test'), + { 'index' => 10, 'title' => '10-test', 'page_index' => '10' } + ], + ].each do |config, match_data, expected_result| + file_processor = FileProcessor.new(config) + file_processor.page_index = 10 + + assert_equal expected_result, file_processor.build_filename_parts(match_data) + end + end + + def test_construct_filters_and_targets + + end +end