Compare commits

..

No commits in common. "gh-pages" and "master" have entirely different histories.

31 changed files with 1773 additions and 1 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*~
.loadpath
.project
.DS_Store
coverage/

1
README Normal file
View File

@ -0,0 +1 @@
minicomic-backend.rb builds Web- and print-ready comic images from SVG source files.

27
Rakefile Normal file
View File

@ -0,0 +1,27 @@
desc "Run Test::Unit Cases"
task :test do
system("testrb test/*_test.rb")
end
#namespace :test do
# desc "Run RCov analysis"
# task :rcov do
# system(%{rcov -x '.rvm' -x '/test/data/' test/*_test.rb})
# end
#end
begin
require 'jeweler'
Jeweler::Tasks.new do |s|
s.name = "minicomic-backend"
s.executables = "minicomic-backend"
s.summary = "Generate Web- and print-ready collections of comics from SVG files"
s.email = "john@coswellproductions.com"
s.homepage = "http://github.com/johnbintz/minicomic-backend"
s.description = "Generate Web- and print-ready collections of comics from SVG files"
s.authors = ["John Bintz"]
s.files = FileList["{bin,lib,test}/**/*"]
end
rescue LoadError
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
end

36
bin/minicomic-backend Executable file
View File

@ -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__)} <path to YAML 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!

View File

@ -1 +0,0 @@
/var/data/github/releases/20090402205336/app/views/layouts/pages_template.html

0
lib/minicomic-backend.rb Normal file
View File

View File

@ -0,0 +1,85 @@
require 'minicomic-backend/scheduler'
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

View File

@ -0,0 +1,198 @@
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 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

View File

@ -0,0 +1,60 @@
require 'fileutils'
class Filter
@config = {}
@cleanup = []
attr_accessor :config, :cleanup
def initialize
@config = {}
@cleanup = []
end
def cleanup
@cleanup.each do |f|
FileUtils.rm(f) if File.file?(f)
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

View File

@ -0,0 +1,61 @@
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

View File

@ -0,0 +1,10 @@
require 'minicomic-backend/filter'
class InputFilter < Filter
attr_accessor :output_filename
def initialize
super
@output_filename = "tmp.png"
end
end

View File

@ -0,0 +1,17 @@
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

View 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

View File

@ -0,0 +1,70 @@
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

View File

@ -0,0 +1,99 @@
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

View File

@ -0,0 +1,120 @@
require 'minicomic-backend/print_handling'
require 'minicomic-backend/image_processing'
require 'minicomic-backend/input_filter'
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

View File

@ -0,0 +1,47 @@
require 'minicomic-backend/output_filter'
require 'minicomic-backend/print_handling'
require 'minicomic-backend/pagination'
require 'minicomic-backend/image_processing'
#
# Convert bitmap files to a paginated print-ready file
#
class TempBitmapToPaginatedPrint < OutputFilter
include PrintHandling, Pagination, ImageProcessing
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

View File

@ -0,0 +1,17 @@
require 'minicomic-backend/output_filter'
require 'minicomic-backend/print_handling'
#
# 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

View File

@ -0,0 +1,31 @@
require 'minicomic-backend/output_filter'
require 'minicomic-backend/image_processing'
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

206
test/config_loader_test.rb Normal file
View File

@ -0,0 +1,206 @@
require 'test_helper'
require 'minicomic-backend/config_loader'
require 'minicomic-backend/scheduler'
class TestConfigLoader < Test::Unit::TestCase
def setup
@config_loader = ConfigLoader.new
end
def test_load
[
[ {}, false, nil, nil ],
[ {'Global' => {}}, false, nil, nil ],
[
{
'Global' => {
'path' => 'test/*.svg'
}
},
{
'Global' => {
'path' => 'test/*.svg',
'page_index_format' => '%02d',
'files' => [],
'fileinfo_by_file' => {}
}
},
[
{ :expects => :count_pattern, :with => 'test/*.svg', :returns => '%02d' }
],
nil
],
[
{
'Global' => {
'path' => '.',
'match' => '.*\.svg',
'page_index_format' => '%02d'
},
'Web' => {
'schedule' => 'schedule'
}
},
{
'Global' => {
'path' => '.',
'match' => '.*\.svg',
'page_index_format' => '%02d',
'files' => [ Dir.pwd + '/test1.svg', Dir.pwd + '/test2.svg' ],
'fileinfo_by_file' => {}
},
'Web' => {
'schedule' => 'schedule',
'publish_dates' => 'schedule'
}
},
[],
[ 'test1.svg', 'test2.svg', 'test3.png' ]
],
[
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ 'test.svg' ]
}
},
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ 'test.svg' ],
'files' => [ 'test/test.svg' ],
'fileinfo_by_file' => {}
}
}
],
[
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ 'blank' ]
}
},
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ 'blank' ],
'files' => [ 'test/blank' ],
'fileinfo_by_file' => { 'test/blank' => { 'file' => 'blank' } }
}
}
],
[
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ {} ]
}
},
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ {} ],
'files' => [ {} ],
'fileinfo_by_file' => {}
}
}
],
[
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ { 'file' => 'test', 'param' => 'test2' } ]
}
},
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ { 'file' => 'test', 'param' => 'test2' } ],
'files' => [ 'test/test' ],
'fileinfo_by_file' => { 'test/test' => { 'file' => 'test', 'param' => 'test2' } }
}
}
],
[
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ { 'file' => [ 'test', 'test2' ], 'param' => 'test2' } ]
}
},
{
'Global' => {
'path' => 'test/',
'page_index_format' => '%02d',
'pages' => [ { 'file' => [ 'test', 'test2' ], 'param' => 'test2' } ],
'files' => [ [ 'test/test', 'test/test2' ] ],
'fileinfo_by_file' => { 'test/test,test/test2' => { 'file' => [ 'test', 'test2' ], 'param' => 'test2' } }
}
}
],
].each do |yaml, expected_result, expectations, files|
@config_loader.expects(:load_yaml).with('file').returns(yaml)
yaml.each do |type, info|
if type != "Global"
if info['schedule']
Scheduler.any_instance.expects(:schedule).with(info['schedule'], expected_result['Global']['files'].length).returns('schedule')
end
end
end
if expectations
expectations.each do |expectation|
e = @config_loader.expects(expectation[:expects])
if expectation[:with]
e.with(expectation[:with])
end
if expectation[:returns]
e.returns(expectation[:returns])
end
end
end
FakeFS do
if files
FileUtils.touch files
end
assert_equal expected_result, @config_loader.load('file')
if files
files.each { |f| FileUtils.rm f }
end
end
end
end
def test_load_yaml
FakeFS do
File.open('test', 'w') do |fh|
fh.puts("- one\n- two\n- three")
end
assert_equal %w(one two three), @config_loader.load_yaml('test')
FileUtils.rm 'test'
end
end
def test_count_pattern
FakeFS do
FileUtils.touch [ 'test', 'test2', 'test3' ]
assert_equal '%01d', @config_loader.count_pattern('*')
[ 'test', 'test2', 'test3' ].each { |f| FileUtils.rm f }
end
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 230 B

177
test/file_processor_test.rb Normal file
View File

@ -0,0 +1,177 @@
require 'test_helper'
require 'minicomic-backend/file_processor'
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
match_data = 'test'
[
[
'test.svg',
{ 'target' => 'test.png' },
'test',
[ SVGToTempBitmap, TempBitmapToWeb, 'target' ]
],
[
'test.svg',
{ 'target' => 'test.pdf' },
'test',
[ SVGToTempBitmap, TempBitmapToPrint, 'target' ]
],
[
'test.svg',
{ 'target' => 'test.pdf', 'is_paginated' => true },
'test',
[ SVGToTempBitmap, TempBitmapToPaginatedPrint, 'target' ]
],
].each do |filename, info, match_data, expected_return|
file_processor = FileProcessor.new({})
file_processor.expects(:build_filename_parts).with(match_data).returns('parts')
expected_return[0..1].each do |m|
m.any_instance.stubs(:recalc_pixels)
end
expected_return[1].any_instance.expects(:targets).with('parts').returns('target')
file_processor.construct_filters_and_targets(filename, info, match_data)
end
end
def test_determine_rebuild
[
[ [], 'source', false ],
[ [ 'r-test3' ], 'source', true ],
[ [ 'r-test' ], 'source', true ],
[ [ 'r-test2' ], 'source', false ],
[ [ 'r-test2' ], 'blank', false ],
].each do |targets, filename, expected_return|
file_processor = FileProcessor.new({})
class << file_processor
def file_mtime(file)
case file
when 'r-test'
return 1
when 'source'
return 2
when 'r-test2'
return 3
end
end
end
FakeFS do
FileUtils.touch [ 'r-test', 'r-test2', 'source' ]
assert_equal expected_return, file_processor.determine_rebuild(targets, filename)
[ 'r-test', 'r-test2', 'source' ].each { |f| FileUtils.rm f }
end
end
end
def test_do_build
[
[ 'target', 'input', 'return' ],
[ [ 'target1', 'target2' ], 'input', [ 'return1', 'return2' ] ]
].each do |targets, filename, input_obj_build_return|
input = Class.new do
def build(filename); end
end.new
output = Class.new do
def build(filename); end
end.new
input.expects(:build).with(filename).returns(input_obj_build_return)
case input_obj_build_return.class.to_s
when "String"
output.expects(:build).with(input_obj_build_return, targets)
when "Array"
[0, 1].each do |i|
output.expects(:build).with(input_obj_build_return[i], targets[i], (i == 0) ? "left" : "right")
end
end
input.expects(:cleanup)
file_processor = FileProcessor.new({})
file_processor.do_build(targets, filename, input, output)
end
end
end

74
test/filter_test.rb Normal file
View File

@ -0,0 +1,74 @@
require 'test_helper'
require 'minicomic-backend/filter'
class TestFilter < Test::Unit::TestCase
def setup
@filter = Filter.new
end
def test_recalc_pixels
@filter.config = {}
assert_raise RuntimeError do
@filter.config = {'print' => true}
end
assert_raise RuntimeError do
@filter.config = {'print' => true,
'dpi' => 'test'}
end
assert_raise RuntimeError do
@filter.config = {
'print' => true,
'dpi' => 10
}
end
[ 'width', 'height' ].each do |dim|
assert_raise RuntimeError do
@filter.config = {
'print' => true,
'dpi' => 10,
'width' => 10
}
end
@filter.config = {
'width' => 10
}
assert_equal(10, @filter.config['width'])
end
@filter.config = {
'print' => true,
'dpi' => 10,
'width_inches' => 1,
'height_inches' => 2
}
assert_equal(10, @filter.config['width'])
assert_equal(20, @filter.config['height'])
end
def test_cleanup
@filter.cleanup = [ 'test', 'test2', 'test3' ]
FakeFS do
FileUtils.touch [ 'test', 'test3', 'test4' ]
@filter.cleanup
[ 'test', 'test2', 'test3' ].each do |file|
assert !(File.exists? file)
end
[ 'test4' ].each do |file|
assert File.exists? file
end
[ 'test', 'test3', 'test4' ].each { |f| FileUtils.rm f }
end
end
def test_get_dimensions
assert_equal [ 50, 75 ], @filter.get_dimensions(File.dirname(__FILE__) + '/data/test_dimensions.png')
end
end

View File

@ -0,0 +1,58 @@
require 'minicomic-backend/image_processing'
class TestImageProcessing < Test::Unit::TestCase
def setup
@instance = Class.new do
include ImageProcessing
attr_accessor :config
end.new
end
def test_setup_inkscape
[
[ {}, [], 'target' ],
[ { 'width' => 'test' }, [], 'target' ],
[ { 'width' => 200 }, ['-w 200'], 'target' ],
[ { 'height' => 'test' }, [], 'target' ],
[ { 'height' => 200 }, ['-h 200'], 'target' ],
[ { 'rotate' => 0 }, [], 'target' ],
[ { 'rotate' => 90 }, [], 'target-pre.png' ],
[ { 'rotate' => 90, 'width' => 50, 'height' => 75 }, ['-w 75', '-h 50'], 'target-pre.png' ],
].each do |config, expected_params, expected_target|
@instance.config = config
params, inkscape_target = @instance.setup_inkscape('target')
assert_equal expected_params, params
assert_equal inkscape_target, expected_target
end
end
def test_handle_inkscape_rotation
@instance.config = { 'rotate' => 90 }
@instance.expects(:convert).with(['"target-pre.png"', '-rotate 90', '"target"'])
FakeFS do
FileUtils.touch('target-pre.png')
@instance.handle_inkscape_rotation('target-pre.png', 'target')
assert !(File.exists? 'target-pre.png')
end
end
def test_convert
[ false, true ].each do |verbose|
@instance.expects(:call_system).with('convert ' + (verbose ? '-verbose ' : '') + 'test')
@instance.convert(['test'], verbose)
end
end
def test_inkscape
@instance.expects(:call_system).with('inkscape -e "new-target" -y 1.0 -w 200 "input"')
@instance.expects(:handle_inkscape_rotation).with('new-target', 'target')
@instance.expects(:setup_inkscape).returns([['-w 200'], 'new-target'])
@instance.inkscape('input', 'target')
end
end

View File

@ -0,0 +1,15 @@
require 'test_helper'
require 'minicomic-backend/output_filter'
class TestOutputFilter < Test::Unit::TestCase
def setup
@of = OutputFilter.new
@of.stubs(:recalc_pixels)
end
def test_filename
@of.config = { 'target' => 'test{test}{test2}test3' }
assert_equal 'testtest4test5test3', @of.filename({ 'test' => 'test4', 'test2' => 'test5' })
end
end

26
test/pagination_test.rb Normal file
View File

@ -0,0 +1,26 @@
require 'test_helper'
require 'minicomic-backend/pagination'
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

View File

@ -0,0 +1,52 @@
require 'test_helper'
require 'minicomic-backend/print_handling'
class TestPrintHandling < Test::Unit::TestCase
def setup
@instance = Class.new do
include PrintHandling
attr_accessor :config
end.new
end
def test_calculate_page_size
[
[ { 'page_width' => 10, 'page_height' => 20 }, [ 10, 20 ] ],
[ { 'dpi' => 100 , 'page_width' => 10, 'page_height' => 20 }, [ 10, 20 ] ],
[ { 'dpi' => 100 , 'page_width_inches' => 1, 'page_height_inches' => 2 }, [ 100, 200 ] ],
[ { 'dpi' => 1 , 'page_size' => 'letter' }, [ 8.5, 11 ] ],
[ { 'dpi' => 1 , 'page_size' => 'letter_landscape' }, [ 11, 8.5 ] ],
[ { 'dpi' => 1 , 'page_size' => 'half_letter_landscape' }, [ 5.5, 8.5 ] ],
].each do |config, expected_return|
@instance.config = config
assert_equal expected_return, @instance.calculate_page_size
end
end
def test_build_for_print
@instance.stubs(:calculate_page_size).returns([10, 20])
@instance.config = { 'dpi' => 100 }
[
[ 'none', 'Center' ],
[ 'left', 'East' ],
[ 'right', 'West' ]
].each do |side, gravity|
[
[ '|pipe', 'pipe' ],
[ 'nopipe', '"nopipe"' ]
].each do |output, expected_filename|
@instance.expects(:convert).with([
'-density 100',
'-size 10x20',
'xc:white',
"-gravity #{gravity}",
"-draw 'image Over 0,0 0,0 \"input\"'",
expected_filename
])
@instance.build_for_print('input', output, side)
end
end
end
end

71
test/scheduler_test.rb Normal file
View File

@ -0,0 +1,71 @@
require 'test_helper'
require 'minicomic-backend/scheduler'
class TestScheduler < Test::Unit::TestCase
def setup
@scheduler = Scheduler.instance
end
def test_schedule
[
[
{
'start' => DateTime.parse('2010-01-01'),
'interval' => Scheduler::WEEKLY
},
2,
[ DateTime.parse('2010-01-01'), DateTime.parse('2010-01-08') ]
],
[
{
'start' => DateTime.parse('2010-01-01'),
'interval' => [ 'monday' ]
},
2,
[ DateTime.parse('2010-01-04'), DateTime.parse('2010-01-11') ]
],
[
{
'start' => DateTime.parse('2010-01-01'),
'interval' => Scheduler::DAILY,
'breaks' => [
{ 'from' => DateTime.parse('2010-01-03'), 'to' => DateTime.parse('2010-01-05') }
]
},
4,
[ DateTime.parse('2010-01-01'), DateTime.parse('2010-01-02'), DateTime.parse('2010-01-06'), DateTime.parse('2010-01-07') ]
],
[
{
'start' => DateTime.parse('2010-01-02'),
'interval' => [ 'monday', 'wednesday', 'friday' ],
'breaks' => [
{ 'at_index' => 3, 'for_days' => 7 }
]
},
6,
[
DateTime.parse('2010-01-04'), DateTime.parse('2010-01-06'), DateTime.parse('2010-01-08'),
DateTime.parse('2010-01-18'), DateTime.parse('2010-01-20'), DateTime.parse('2010-01-22')
]
],
[
{
'start' => DateTime.parse('2010-01-06'),
'interval' => [ 'monday', 'wednesday', 'friday' ],
},
3,
[
DateTime.parse('2010-01-06'), DateTime.parse('2010-01-08'), DateTime.parse('2010-01-11'),
]
],
].each do |parameters, to_produce, expected_results|
assert_equal expected_results, @scheduler.schedule(parameters, to_produce)
end
end
def test_skip_to_dow
assert_equal DateTime.parse('2010-01-02'), @scheduler.skip_to_dow(DateTime.parse('2010-01-01'), 6)
assert_equal DateTime.parse('2010-01-02'), @scheduler.skip_to_dow(DateTime.parse('2010-01-01'), 'saturday')
end
end

View File

@ -0,0 +1,120 @@
require 'test_helper'
require 'minicomic-backend/svg_to_temp_bitmap'
class TestSVGToTempBitmap < Test::Unit::TestCase
def setup
@filter = SVGToTempBitmap.new
end
def test_build
[
[ "", :single ],
[ [], :multiple ]
].each do |input, expected_method|
@filter.expects(expected_method).with(input)
@filter.build(input)
end
end
def test_build_spread
@filter.stubs(:get_dimensions).with('filename').returns([50, 75])
@filter.expects(:convert).with(['"filename"', '-gravity Northwest', '-crop 25x75+0+0', '+repage', '"filename-left.png"'])
@filter.expects(:convert).with(['"filename"', '-gravity Northwest', '-crop 25x75+25+0', '+repage', '"filename-right.png"'])
assert_equal [ 'filename-left.png', 'filename-right.png' ], @filter.build_spread('filename')
end
def test_join_files
@filter.output_filename = 'test'
[
[ [], 1, [] ],
[ ['file'], 1, [
[ 'test-0.png', 0, 0 ]
] ],
[ ['file', 'file2'], 1, [
[ 'test-0.png', 0, 0 ],
[ 'test-1.png', 0, 1 ],
] ],
[ ['file', 'file2'], 2, [
[ 'test-0.png', 0, 0 ],
[ 'test-1.png', 1, 0 ],
] ],
[ ['blank', 'file2'], 2, [
[ 'test-1.png', 1, 0 ],
] ],
].each do |files, width, expected_output|
@filter.stubs(:inkscape)
assert_equal expected_output, @filter.join_files(files, width)
end
end
def test_generate_joined_files_command
[
[ [], 100, 50, [], [] ],
[
[
[ 'file', 0, 0 ]
], 100, 50, [
[ 50, 25 ]
], [
'"file" -geometry +25+12 -composite'
]
],
].each do |files, grid_width, grid_height, image_size_returns, expected_command|
@filter.stubs(:get_dimensions).returns(*image_size_returns)
assert_equal expected_command, @filter.generate_joined_files_command(files, grid_width, grid_height)
end
end
def test_single_no_spread
@filter.output_filename = 'test'
@filter.expects(:inkscape).with(Dir.pwd + '/file', Dir.pwd + '/test')
assert_equal Dir.pwd + '/test', @filter.single('file')
assert_equal [ Dir.pwd + '/test' ], @filter.cleanup
end
def test_single_spread
@filter.stubs(:recalc_pixels)
@filter.output_filename = 'test'
@filter.config = {
'spread' => true
}
@filter.expects(:inkscape).with(Dir.pwd + '/file', Dir.pwd + '/test')
@filter.expects(:build_spread).with(Dir.pwd + '/test').returns(['target1', 'target2'])
@filter.single('file')
end
def test_multiple
@filter.stubs(:recalc_pixels)
@filter.output_filename = 'test'
@filter.config = {
'grid' => '2x1'
}
@filter.stubs(:calculate_page_size).returns([100,50])
joined_files = [
[ 'test-0.png', 0, 0 ],
[ 'test-1.png', 1, 0 ]
]
@filter.expects(:join_files).with(['file1', 'file2'], 2).returns(joined_files)
@filter.expects(:generate_joined_files_command).with(joined_files, 50, 50).returns([
'command-1', 'command-2'
])
@filter.expects(:convert).with([
'-size 100x50',
'xc:white',
'command-1',
'command-2',
'"test"'
])
assert_equal 'test', @filter.multiple(['file1', 'file2'])
assert_equal [ 'test-0.png', 'test-1.png', 'test' ], @filter.cleanup
end
end

View File

@ -0,0 +1,31 @@
require 'test_helper'
require 'minicomic-backend/temp_bitmap_to_web'
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_filename
@filter.config = {
'target' => 'test{date}',
'date_format' => '%Y-%m-%d',
'publish_dates' => [ Date.parse('2010-01-01') ]
}
assert_equal 'test2010-01-01', @filter.filename({'index' => 0})
end
end

11
test/test_helper.rb Normal file
View File

@ -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