work on pagination
This commit is contained in:
parent
ffd0a583dd
commit
ff8b447b6c
28
classes/TempBitmapToWeb.rb
Normal file
28
classes/TempBitmapToWeb.rb
Normal file
@ -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
|
47
modules/Pagination.rb
Normal file
47
modules/Pagination.rb
Normal file
@ -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
|
27
tests/TestPagination.rb
Normal file
27
tests/TestPagination.rb
Normal file
@ -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
|
39
tests/TestTempBitmapToWeb.rb
Normal file
39
tests/TestTempBitmapToWeb.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user