From 194ff517154e5d93b2525576300a651579034283 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 12 Sep 2010 09:06:45 -0400 Subject: [PATCH] gemifying this thing before i fix some issues --- bin/minicomic-backend.rb | 36 ++++ classes/ConfigLoader.rb | 85 -------- classes/FileProcessor.rb | 193 ------------------ classes/Filter.rb | 58 ------ classes/InputFilter.rb | 10 - classes/OutputFilter.rb | 17 -- classes/SVGToTempBitmap.rb | 120 ----------- classes/Scheduler.rb | 99 --------- classes/TempBitmapToPaginatedPrint.rb | 45 ---- classes/TempBitmapToPrint.rb | 17 -- classes/TempBitmapToWeb.rb | 31 --- lib/minicomic-backend.rb | 0 minicomic-backend.rb | 28 --- modules/ImageProcessing.rb | 61 ------ modules/Pagination.rb | 47 ----- modules/PrintHandling.rb | 70 ------- {tests => test}/TestFilter.rb | 0 {tests => test}/TestImageProcessing.rb | 0 {tests => test}/TestOutputFilter.rb | 0 {tests => test}/TestPagination.rb | 0 {tests => test}/TestPrintHandling.rb | 0 {tests => test}/TestSVGToTempBitmap.rb | 0 {tests => test}/TestScheduler.rb | 0 {tests => test}/TestTempBitmapToWeb.rb | 0 .../config_loader_test.rb | 8 +- {tests => test}/data/test_dimensions.png | Bin .../file_processor_test.rb | 12 +- test/test_helper.rb | 11 + 28 files changed, 56 insertions(+), 892 deletions(-) create mode 100755 bin/minicomic-backend.rb delete mode 100644 classes/ConfigLoader.rb delete mode 100644 classes/FileProcessor.rb delete mode 100644 classes/Filter.rb delete mode 100644 classes/InputFilter.rb delete mode 100644 classes/OutputFilter.rb delete mode 100644 classes/SVGToTempBitmap.rb delete mode 100644 classes/Scheduler.rb delete mode 100644 classes/TempBitmapToPaginatedPrint.rb delete mode 100644 classes/TempBitmapToPrint.rb delete mode 100644 classes/TempBitmapToWeb.rb create mode 100644 lib/minicomic-backend.rb delete mode 100755 minicomic-backend.rb delete mode 100644 modules/ImageProcessing.rb delete mode 100644 modules/Pagination.rb delete mode 100644 modules/PrintHandling.rb rename {tests => test}/TestFilter.rb (100%) rename {tests => test}/TestImageProcessing.rb (100%) rename {tests => test}/TestOutputFilter.rb (100%) rename {tests => test}/TestPagination.rb (100%) rename {tests => test}/TestPrintHandling.rb (100%) rename {tests => test}/TestSVGToTempBitmap.rb (100%) rename {tests => test}/TestScheduler.rb (100%) rename {tests => test}/TestTempBitmapToWeb.rb (100%) rename tests/TestConfigLoader.rb => test/config_loader_test.rb (96%) rename {tests => test}/data/test_dimensions.png (100%) rename tests/TestFileProcessor.rb => test/file_processor_test.rb (94%) create mode 100644 test/test_helper.rb diff --git a/bin/minicomic-backend.rb b/bin/minicomic-backend.rb new file mode 100755 index 0000000..7e1b4df --- /dev/null +++ b/bin/minicomic-backend.rb @@ -0,0 +1,36 @@ +#!/usr/bin/ruby + +require 'yaml' +begin + require 'home_run' +rescue LoadError + STDERR.puts "Install home_run for faster date handling" +end + +require 'time' + +begin + require 'minicomic-backend' +rescue LoadError + $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) + require 'minicomic-backend' +end + +require 'minicomic-backend/config_loader' +require 'minicomic-backend/file_processor' + +if !ARGV[0] + STDERR.puts "Usage: #{File.basename(__FILE__)} " + exit 0 +end + +if !File.exists?(ARGV[0]) + STDERR.puts "#{ARGV[0]} doesn't exist!" + exit 1 +end + +config_loader = ConfigLoader.new +config = config_loader.load(ARGV[0]) + +file_processor = FileProcessor.new(config) +file_processor.process! diff --git a/classes/ConfigLoader.rb b/classes/ConfigLoader.rb deleted file mode 100644 index 77e378d..0000000 --- a/classes/ConfigLoader.rb +++ /dev/null @@ -1,85 +0,0 @@ -require File.dirname(__FILE__) + '/Scheduler.rb' - -class ConfigLoader - def load(file) - config = load_yaml(file) - - if !config['Global'] - return false - end - - global = config['Global'] - - if !global['path'] - return false - end - - if !global['page_index_format'] - global['page_index_format'] = count_pattern(global['path']) - end - - files = [] - fileinfo_by_file = {} - - if global['pages'] - files = global['pages'].collect do |f| - result = nil - case f.class.to_s - when 'String' - result = global['path'] + f - if f == "blank" - fileinfo_by_file[result] = { 'file' => f } - end - when 'Hash' - if f['file'] - case f['file'].class.to_s - when 'String' - result = global['path'] + f['file'] - fileinfo_by_file[result] = f - when 'Array' - result = f['file'].collect { |sub_f| global['path'] + sub_f } - fileinfo_by_file[result.join(",")] = f - end - else - result = f - end - end - result - end - else - if global['match'] - re = Regexp.new(global['match']) - - files = Dir[global['path'] + '/*'].sort.collect do |filename| - if matches = re.match(filename) - filename - end - end.compact - end - end - - global['files'] = files - global['fileinfo_by_file'] = fileinfo_by_file - - config['Global'] = global - - scheduler = Scheduler.instance - config.each do |type, info| - if type != "Global" - if info['schedule'] - info['publish_dates'] = scheduler.schedule(info['schedule'], files.length) - end - end - end - - config - end - - def load_yaml(file) - YAML::load(File.open(file, "r")) - end - - def count_pattern(path) - "%0#{Math.log10(Dir[path].length).ceil}d" - end -end diff --git a/classes/FileProcessor.rb b/classes/FileProcessor.rb deleted file mode 100644 index 04d4d64..0000000 --- a/classes/FileProcessor.rb +++ /dev/null @@ -1,193 +0,0 @@ -class FileProcessor - attr_accessor :config, :paginated_source_files, :page_index - - def initialize(config) - @page_index = 0 - @config = config - @paginated_source_files = {} - end - - def process - @config['Global']['files'].each do |filename| - ok, fileinfo, filename = verify_filename(filename) - - 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 - - file_fileinfo = (@config['Global']['fileinfo_by_file'][fileinfo_key]) ? @config['Global']['fileinfo_by_file'][fileinfo_key] : {} - - file_fileinfo = info.dup.merge(fileinfo).merge(file_fileinfo) - - input_obj, output_obj, targets = construct_filters_and_targets(filename, file_fileinfo, ok) - - if determine_rebuild(targets, filename) - any_rebuilt = true - - puts "Rebuilding #{filename_display} (#{type})..." - puts " Using #{filename} as a source" - puts " and writing to #{targets.inspect}" - - do_build(targets, filename, input_obj, output_obj) - end - if info['is_paginated'] - if !@paginated_source_files[type]; @paginated_source_files[type] = []; end - @paginated_source_files[type] << targets - 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 - - 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 - - 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) - input = nil; output = nil - - 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 - - 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 -end diff --git a/classes/Filter.rb b/classes/Filter.rb deleted file mode 100644 index aaffc47..0000000 --- a/classes/Filter.rb +++ /dev/null @@ -1,58 +0,0 @@ -class Filter - @config = {} - @cleanup = [] - - attr_accessor :config, :cleanup - - def initialize - @config = {} - @cleanup = [] - end - - def cleanup - @cleanup.each do |f| - if File.exists? f; File.unlink(f); end - end - end - - def recalc_pixels - if @config['print'] - if !@config['dpi']; raise "DPI not defined"; end - if (@config['dpi'].to_i.to_s) != @config['dpi'].to_s; raise "DPI not integer"; end - if @config['width_inches'] && (@config['width_inches'].to_f != 0) - @config['width'] = (@config['width_inches'].to_f * @config['dpi'].to_f).to_i - else - @config.delete('width') - end - if @config['height_inches'] && (@config['height_inches'].to_f != 0) - @config['height'] = (@config['height_inches'].to_f * @config['dpi'].to_f).to_i - else - @config.delete('height') - end - - if (!@config['width'] && !@config['height']) - raise "No dimensions defined!" - end - end - end - - # - # Set the config - # - def config=(c) - @config = c - - recalc_pixels - end - - # - # Get the dimensions of a file - # - def get_dimensions(input) - dimensions = nil - IO.popen("identify -format '%w,%h' \"#{input}\"") do |fh| - dimensions = fh.readlines.first.split(",").collect { |d| d.to_i } - end - dimensions - end -end diff --git a/classes/InputFilter.rb b/classes/InputFilter.rb deleted file mode 100644 index 504d55d..0000000 --- a/classes/InputFilter.rb +++ /dev/null @@ -1,10 +0,0 @@ -require File.dirname(__FILE__) + '/Filter.rb' - -class InputFilter < Filter - attr_accessor :output_filename - - def initialize - super - @output_filename = "tmp.png" - end -end diff --git a/classes/OutputFilter.rb b/classes/OutputFilter.rb deleted file mode 100644 index 1279e71..0000000 --- a/classes/OutputFilter.rb +++ /dev/null @@ -1,17 +0,0 @@ -require File.dirname(__FILE__) + '/Filter.rb' - -class OutputFilter < Filter - # - # get the output filename for this filter - # - def filename(info) - target = @config['target'] - info.each { |k,v| target = target.gsub("{#{k}}", v.to_s) } - target - end - - # - # get the output targets for this filter - # - def targets(info); filename(info); end -end diff --git a/classes/SVGToTempBitmap.rb b/classes/SVGToTempBitmap.rb deleted file mode 100644 index da2432f..0000000 --- a/classes/SVGToTempBitmap.rb +++ /dev/null @@ -1,120 +0,0 @@ -require File.dirname(__FILE__) + '/../modules/PrintHandling.rb' -require File.dirname(__FILE__) + '/../modules/ImageProcessing.rb' -require File.dirname(__FILE__) + '/InputFilter.rb' - -class SVGToTempBitmap < InputFilter - include PrintHandling, ImageProcessing - - # - # select which build method to use based on the number of provided images. - # - def build(input) - (input.instance_of? Array) ? multiple(input) : single(input) - end - - # - # process a single input file, possibly splitting it into two output files if it's a spread - # - def single(input) - filename = Dir.pwd + '/' + @output_filename - inkscape(Dir.pwd + '/' + input, filename) - @cleanup << filename - - if @config['spread'] - targets = build_spread(filename) - @cleanup.concat targets - return targets - end - - filename - end - - def build_spread(filename) - width, height = get_dimensions(filename) - targets = [] - - [ [ "left", 0 ], [ "right", width / 2 ] ].each do |side, offset| - target = filename + "-#{side}.png" - command = [ - "\"#{filename}\"", - "-gravity Northwest", - "-crop #{width/2}x#{height}+#{offset}+0", - "+repage", - "\"#{target}\"" - ] - - convert(command) - - targets << target - end - - targets - end - - # - # combine multiple input files onto a single page-sized canvas. - # images are added left-to-right and top-to-bottom starting from the top-left of the page. - # leave a grid square blank by passing the filename "/blank". - # - def multiple(files) - if @config['spread']; raise "Spreads and grids combined do not make sense"; end - - width, height = @config['grid'].split("x").collect { |f| f.to_f } - - page_width, page_height = calculate_page_size - - grid_width = page_width / width - grid_height = page_height / height - - joined_files = join_files(files, width) - - command = [ - "-size #{page_width}x#{page_height}", - "xc:white" - ] - - command.concat generate_joined_files_command(joined_files, grid_width, grid_height) - - command << "\"#{@output_filename}\"" - - convert(command) - - @cleanup.concat joined_files.collect { |file, x, y| file } - @cleanup << @output_filename - - @output_filename - end - - def join_files(files, width) - joined_files = [] - - files.each_index do |i| - x = i % width - y = (i / width).floor - - if files[i].split('/').last != "blank" - tmp_file = @output_filename + "-#{i}.png" - inkscape(Dir.pwd + '/' + files[i], tmp_file) - - joined_files << [ tmp_file, x, y ] - end - end - - joined_files - end - - def generate_joined_files_command(files, grid_width, grid_height) - command = [] - - files.each do |file, x, y| - image_width, image_height = get_dimensions(file) - - x_offset = (grid_width - image_width) / 2 - y_offset = (grid_height - image_height) / 2 - - command << "\"#{file}\" -geometry +#{x * grid_width + x_offset}+#{y * grid_height + y_offset} -composite" - end - - command - end -end diff --git a/classes/Scheduler.rb b/classes/Scheduler.rb deleted file mode 100644 index 8425d67..0000000 --- a/classes/Scheduler.rb +++ /dev/null @@ -1,99 +0,0 @@ -require 'singleton' - -class Scheduler - include Singleton - - WEEKLY = [ 7 ] - DAILY = [ 1 ] - - def skip_to_dow(date, dow) - if dow.is_a? String - dow = Date::DAYNAMES.collect { |d| d.downcase }.index(dow.downcase) - end - - if dow.is_a? Fixnum - while date.wday != dow - date += 1 - end - end - date - end - - def ok_to_add(date, index, prior, breaks) - ok = true - breaks.each do |i| - if i['from'] && i['to'] - if (i['from'] <= date) && (i['to'] >= date) - ok = false - end - end - - if i['at_index'] && i['for_days'] && prior - if i['at_index'] == index - ok = (date > (prior + i['for_days'])) - end - end - end - ok - end - - def schedule(parameters, to_produce) - dates = [] - - if parameters['start'] - current = parameters['start'] - prior = nil - - breaks = parameters['breaks'] || [] - - all_strings = true - parameters['interval'].each do |i| - if !i.is_a? String - all_strings = false - break - end - end - - if all_strings - parameters['interval'].each_index do |i| - interval = parameters['interval'].shift - if current == skip_to_dow(current, interval) - parameters['interval'].unshift(interval) - break - else - parameters['interval'].push(interval) - end - end - end - - index = 0 - while dates.length < to_produce - interval = parameters['interval'].shift - - what_to_add = nil - - if interval.is_a? String - current = skip_to_dow(current, interval) - - what_to_add = 1 - end - - if ok_to_add(current, index, prior, breaks) - dates << current - prior = current - index += 1 - end - - if interval.is_a? Fixnum - what_to_add = interval - end - - current += what_to_add - - parameters['interval'] << interval - end - end - - dates - end -end diff --git a/classes/TempBitmapToPaginatedPrint.rb b/classes/TempBitmapToPaginatedPrint.rb deleted file mode 100644 index 9252763..0000000 --- a/classes/TempBitmapToPaginatedPrint.rb +++ /dev/null @@ -1,45 +0,0 @@ -require File.dirname(__FILE__) + '/OutputFilter.rb' -require File.dirname(__FILE__) + '/../modules/PrintHandling.rb' -require File.dirname(__FILE__) + '/../modules/Pagination.rb' - -# -# Convert bitmap files to a paginated print-ready file -# -class TempBitmapToPaginatedPrint < OutputFilter - include PrintHandling, Pagination - def build(input, output, side = "none") - build_for_print(input, output, side) - end - - def targets(info) - (@config['spread'] == true) ? [ filename(info) + "-left.png", filename(info) + "-right.png" ] : filename(info) - end - - def process_pagination(output, face, total_faces, left, right) - page_width, page_height = calculate_page_size - - commands = [ - "-size #{page_width * 2}x#{page_height}", - "xc:white" - ] - - left_creep = 0 - right_creep = 0 - if @config['page_thickness'] - max_creep = (total_faces / 2) - - left_creep = ((max_creep - (face - max_creep).abs) * @config['page_thickness'].to_f * @config['dpi'].to_i).floor - right_creep = ((max_creep - (total_faces - face - max_creep).abs) * @config['page_thickness'].to_f * @config['dpi'].to_i).floor - end - - if left - commands << "\"#{left}\" -geometry +#{left_creep.to_i}+0 -composite" - end - if right - commands << "\"#{right}\" -geometry +#{(page_width + right_creep).to_i}+0 -composite" - end - commands << "-colorspace RGB -depth 8 pnm:- | sam2p -c:lzw -m:dpi:#{(72.0 * (72.0 / @config['dpi'].to_f))} - PDF:\"#{output}\"" - - convert(commands) - end -end diff --git a/classes/TempBitmapToPrint.rb b/classes/TempBitmapToPrint.rb deleted file mode 100644 index 8d94747..0000000 --- a/classes/TempBitmapToPrint.rb +++ /dev/null @@ -1,17 +0,0 @@ -require File.dirname(__FILE__) + '/OutputFilter.rb' -require File.dirname(__FILE__) + '/../modules/PrintHandling.rb' - -# -# Convert a bitmap to a single page print for proofing -# -class TempBitmapToPrint < OutputFilter - include PrintHandling - - def build(input, output) - build_for_print(input, "| pbm:- | sam2p -c:lzw -m:dpi:#{calculate_dpi} - PDF:\"#{output}\"") - end - - def calculate_dpi - (72.0 * (72.0 / @config['dpi'].to_f)) - end -end diff --git a/classes/TempBitmapToWeb.rb b/classes/TempBitmapToWeb.rb deleted file mode 100644 index 85e4c77..0000000 --- a/classes/TempBitmapToWeb.rb +++ /dev/null @@ -1,31 +0,0 @@ -require File.dirname(__FILE__) + '/OutputFilter.rb' -require File.dirname(__FILE__) + '/../modules/ImageProcessing.rb' -require 'fileutils' - -# -# Process an input file for the Web -# -class TempBitmapToWeb < OutputFilter - include ImageProcessing - - def build(input, output) - FileUtils.mkdir_p(File.split(output).first) - quality = @config['quality'] ? @config['quality'] : 80 - convert("\"#{input}\" -quality #{quality} \"#{output}\"") - end - - def filename(info) - info['date'] = @config['publish_dates'][info['index'].to_i - 1].strftime(@config['date_format']) - info['subdir'] = '' - if @config['subdirs'] - @config['subdirs'].each do |dir, subdir_info| - if Range.new(subdir_info['from'].to_i, subdir_info['to'].to_i).include? info['index'].to_i - info['subdir'] = dir + File::SEPARATOR - break - end - end - end - - super(info) - end -end diff --git a/lib/minicomic-backend.rb b/lib/minicomic-backend.rb new file mode 100644 index 0000000..e69de29 diff --git a/minicomic-backend.rb b/minicomic-backend.rb deleted file mode 100755 index d0e68f9..0000000 --- a/minicomic-backend.rb +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/ruby - -require 'yaml' -require 'time' - -THIS_FILE = File.symlink?(__FILE__) ? File.expand_path(File.readlink(__FILE__)) : File.expand_path(__FILE__) - -%w(classes modules).each do |which| - Dir[File.dirname(THIS_FILE) + "/#{which}/*.rb"].each do |file| - require file - end -end - -if !ARGV[0] - puts "Usage: #{File.basename(__FILE__)} " - exit 0 -end - -if !File.exists?(ARGV[0]) - puts "#{ARGV[0]} doesn't exist!" - exit 1 -end - -config_loader = ConfigLoader.new -config = config_loader.load(ARGV[0]) - -file_processor = FileProcessor.new(config) -file_processor.process diff --git a/modules/ImageProcessing.rb b/modules/ImageProcessing.rb deleted file mode 100644 index b3cca47..0000000 --- a/modules/ImageProcessing.rb +++ /dev/null @@ -1,61 +0,0 @@ -module ImageProcessing - def setup_inkscape(target) - params = [] - - width = @config['width'] - height = @config['height'] - inkscape_target = target - - if @config['rotate'] - case @config['rotate'] - when 90, -90 - t = width; width = height; height = t - inkscape_target = target + "-pre.png" - end - end - - if width && (width.to_i != 0) - params << "-w #{width}" - end - if height && (height.to_i != 0) - params << "-h #{height}" - end - - [ params, inkscape_target ] - end - - # - # Build a PNG from an SVG file - # - def inkscape(input, target) - params, inkscape_target = setup_inkscape(target) - - call_system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"") - - handle_inkscape_rotation(inkscape_target, target) - end - - def handle_inkscape_rotation(inkscape_target, target) - if @config['rotate'] - command = [ - "\"#{inkscape_target}\"", - "-rotate #{@config['rotate']}", - "\"#{target}\"" - ] - - convert(command) - File.unlink(inkscape_target) - end - end - - def convert(command, verbose = false) - if verbose - puts "convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ") - end - call_system("convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" ")) - end - - def call_system(command) - Kernel.system(command) - end -end diff --git a/modules/Pagination.rb b/modules/Pagination.rb deleted file mode 100644 index 3d17cf3..0000000 --- a/modules/Pagination.rb +++ /dev/null @@ -1,47 +0,0 @@ -# -# Code to help with pagination -# -module Pagination - def paginate(files) - if !files.instance_of? Array; raise "File list must be an array"; end - if files.length == 0; raise "File list cannot be empty"; end - if (files.length % 4) != 0; raise "File list must be divisible by 4"; end - - tmp_pdf_files = [] - sheet_faces = setup_sheet_faces(files) - - sheet_faces.each_index do |i| - f = @config['target'] + "-#{i}.pdf" - process_pagination(f, i, sheet_faces.length, *sheet_faces[i]) - tmp_pdf_files << f - end - - system("pdfjoin #{tmp_pdf_files.collect { |f| "\"#{f}\"" }.join(" ")} --outfile \"#{@config['target']}\"") - end - - def setup_sheet_faces(files) - number_of_sheet_faces = (files.length / 4) * 2 - - sheet_faces = [] - - is_right = 1 - is_descending = 1 - sheet_face_index = 0 - - files.each do |file| - if !sheet_faces[sheet_face_index]; sheet_faces[sheet_face_index] = []; end - - sheet_faces[sheet_face_index][is_right] = file - is_right = 1 - is_right - - sheet_face_index += is_descending - - if sheet_face_index == number_of_sheet_faces - sheet_face_index -= 1 - is_descending = -1 - end - end - - sheet_faces - end -end diff --git a/modules/PrintHandling.rb b/modules/PrintHandling.rb deleted file mode 100644 index 1016a48..0000000 --- a/modules/PrintHandling.rb +++ /dev/null @@ -1,70 +0,0 @@ -module PrintHandling - # - # calculate the page size in PPI - # - def calculate_page_size - ok = false - - if @config['dpi'] - if @config['page_size'] - case @config['page_size'].downcase - when "letter", "letter_portrait" - page_width = 8.5; page_height = 11; ok = true - when "letter_landscape" - page_width = 11; page_height = 8.5; ok = true - when "half_letter_landscape" - page_width = 5.5; page_height = 8.5; ok = true - end - else - if @config['page_width_inches'] && @config['page_height_inches'] - page_width = @config['page_width_inches'] - page_height = @config['page_height_inches'] - ok = true - end - end - - if ok - page_width *= @config['dpi'].to_i - page_height *= @config['dpi'].to_i - end - end - - if !ok - page_width = @config['page_width'] - page_height = @config['page_height'] - end - - [ page_width, page_height ] - end - - # - # align the provided image on a white page - # - def build_for_print(input, output, side = "none") - page_width, page_height = calculate_page_size - command = [ - "-density #{@config['dpi']}", - "-size #{page_width.to_i}x#{page_height.to_i}", - "xc:white" - ] - - case side - when "none" - command << "-gravity Center" - when "left" - command << "-gravity East" - when "right" - command << "-gravity West" - end - - command << "-draw 'image Over 0,0 0,0 \"#{input}\"'" - - if output[0,1] == "|" - command << output[1..-1] - else - command << "\"#{output}\"" - end - - convert(command) - end -end diff --git a/tests/TestFilter.rb b/test/TestFilter.rb similarity index 100% rename from tests/TestFilter.rb rename to test/TestFilter.rb diff --git a/tests/TestImageProcessing.rb b/test/TestImageProcessing.rb similarity index 100% rename from tests/TestImageProcessing.rb rename to test/TestImageProcessing.rb diff --git a/tests/TestOutputFilter.rb b/test/TestOutputFilter.rb similarity index 100% rename from tests/TestOutputFilter.rb rename to test/TestOutputFilter.rb diff --git a/tests/TestPagination.rb b/test/TestPagination.rb similarity index 100% rename from tests/TestPagination.rb rename to test/TestPagination.rb diff --git a/tests/TestPrintHandling.rb b/test/TestPrintHandling.rb similarity index 100% rename from tests/TestPrintHandling.rb rename to test/TestPrintHandling.rb diff --git a/tests/TestSVGToTempBitmap.rb b/test/TestSVGToTempBitmap.rb similarity index 100% rename from tests/TestSVGToTempBitmap.rb rename to test/TestSVGToTempBitmap.rb diff --git a/tests/TestScheduler.rb b/test/TestScheduler.rb similarity index 100% rename from tests/TestScheduler.rb rename to test/TestScheduler.rb diff --git a/tests/TestTempBitmapToWeb.rb b/test/TestTempBitmapToWeb.rb similarity index 100% rename from tests/TestTempBitmapToWeb.rb rename to test/TestTempBitmapToWeb.rb diff --git a/tests/TestConfigLoader.rb b/test/config_loader_test.rb similarity index 96% rename from tests/TestConfigLoader.rb rename to test/config_loader_test.rb index 10377eb..74ac550 100644 --- a/tests/TestConfigLoader.rb +++ b/test/config_loader_test.rb @@ -1,8 +1,6 @@ -require 'rubygems' -require 'test/unit' -require 'mocha' -require File.dirname(__FILE__) + '/../classes/ConfigLoader.rb' -require File.dirname(__FILE__) + '/../classes/Scheduler.rb' +require 'test_helper' +require 'minicomic-backend/config_loader' +require 'minicomic-backend/scheduler' class TestConfigLoader < Test::Unit::TestCase def setup diff --git a/tests/data/test_dimensions.png b/test/data/test_dimensions.png similarity index 100% rename from tests/data/test_dimensions.png rename to test/data/test_dimensions.png diff --git a/tests/TestFileProcessor.rb b/test/file_processor_test.rb similarity index 94% rename from tests/TestFileProcessor.rb rename to test/file_processor_test.rb index fdab3a0..b5c9dfe 100644 --- a/tests/TestFileProcessor.rb +++ b/test/file_processor_test.rb @@ -1,9 +1,9 @@ -require 'rubygems' -require 'test/unit' -require 'fakefs/safe' -Dir[File.dirname(__FILE__) + '/../classes/*.rb'].each do |f| - require f -end +require 'test_helper' +require 'minicomic-backend/file_processor' +require 'minicomic-backend/svg_to_temp_bitmap' +require 'minicomic-backend/temp_bitmap_to_web' +require 'minicomic-backend/temp_bitmap_to_print' +require 'minicomic-backend/temp_bitmap_to_paginated_print' class TestFileProcessor < Test::Unit::TestCase def test_verify_filename diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..2b5f237 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,11 @@ +require 'rubygems' +require 'test/unit' +require 'mocha' +require 'fakefs/safe' + +begin + require 'minicomic-backend' +rescue LoadError + $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) + require 'minicomic-backend' +end