From ff8b447b6cbd6080fc2de07311797cd2794a9104 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 6 Jan 2010 22:56:52 -0500 Subject: [PATCH] work on pagination --- classes/TempBitmapToWeb.rb | 28 +++++++++++++++++++++ modules/Pagination.rb | 47 ++++++++++++++++++++++++++++++++++++ tests/TestPagination.rb | 27 +++++++++++++++++++++ tests/TestTempBitmapToWeb.rb | 39 ++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 classes/TempBitmapToWeb.rb create mode 100644 modules/Pagination.rb create mode 100644 tests/TestPagination.rb create mode 100644 tests/TestTempBitmapToWeb.rb diff --git a/classes/TempBitmapToWeb.rb b/classes/TempBitmapToWeb.rb new file mode 100644 index 0000000..23be090 --- /dev/null +++ b/classes/TempBitmapToWeb.rb @@ -0,0 +1,28 @@ +require File.dirname(__FILE__) + '/OutputFilter.rb' + +# +# Process an input file for the Web +# +class TempBitmapToWeb < OutputFilter + attr_accessor :schedule + + def initialize + super + @schedule = nil + end + + def requires_schedule(schedule) + @schedule = schedule + end + + def build(input, output) + quality = @config['quality'] ? @config['quality'] : 80 + convert("\"#{input}\" -quality #{quality} \"#{output}\"") + end + + def filename(info) + index = info['index'].to_i + info['date'] = @schedule[index].strftime(@config['date_format']) + super(info) + end +end diff --git a/modules/Pagination.rb b/modules/Pagination.rb new file mode 100644 index 0000000..3d17cf3 --- /dev/null +++ b/modules/Pagination.rb @@ -0,0 +1,47 @@ +# +# 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/tests/TestPagination.rb b/tests/TestPagination.rb new file mode 100644 index 0000000..f7f8ced --- /dev/null +++ b/tests/TestPagination.rb @@ -0,0 +1,27 @@ +require 'rubygems' +require 'test/unit' +require File.dirname(__FILE__) + '/../modules/Pagination.rb' + +class TestPagination < Test::Unit::TestCase + def setup + @pagination = Class.new do + include Pagination + end.new + end + + def test_paginate_raises + [ '', [], [''] ].each do |files| + assert_raise RuntimeError do + @pagination.paginate(files) + end + end + end + + def test_setup_sheet_faces + assert_equal [ + [ 'file4', 'file1' ], [ 'file2', 'file3' ] + ], @pagination.setup_sheet_faces([ + 'file1', 'file2', 'file3', 'file4' + ]) + end +end diff --git a/tests/TestTempBitmapToWeb.rb b/tests/TestTempBitmapToWeb.rb new file mode 100644 index 0000000..741ea85 --- /dev/null +++ b/tests/TestTempBitmapToWeb.rb @@ -0,0 +1,39 @@ +require 'rubygems' +require 'test/unit' +require 'mocha' +require File.dirname(__FILE__) + '/../classes/TempBitmapToWeb.rb' + +class TestTempBitmapToWeb < Test::Unit::TestCase + def setup + @filter = TempBitmapToWeb.new + @filter.stubs(:convert_pixels) + end + + def test_build + [ + [ nil, 80 ], + [ 75, 75 ] + ]. each do |quality, expected_quality| + @filter.expects(:convert).with("\"file\" -quality #{expected_quality} \"outfile\"") + @filter.config = { + 'quality' => quality + } + @filter.build('file', 'outfile') + end + end + + def test_requires_schedule + @filter.requires_schedule('test') + assert_equal 'test', @filter.schedule + end + + def test_filename + @filter.schedule = [ Date.parse('2010-01-01') ] + @filter.config = { + 'target' => 'test{date}', + 'date_format' => '%Y-%m-%d' + } + assert_equal 'test2010-01-01', @filter.filename({'index' => 0}) + end + +end