164 lines
4.6 KiB
Ruby
Executable File
164 lines
4.6 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require 'thor'
|
|
require 'digest/md5'
|
|
require 'parallel'
|
|
|
|
require_relative '../lib/svggvs'
|
|
|
|
module SVGGVS
|
|
class Cli < Thor
|
|
include Thor::Actions
|
|
|
|
def self.source_root
|
|
::File.expand_path('../../skel', __FILE__)
|
|
end
|
|
|
|
class_option :cardfile, default: 'Cardfile'
|
|
|
|
PREREQ_MESSAGES = {
|
|
'inkscape' => (<<-MSG),
|
|
sudo apt-get install inkscape or, on Mac OS X, download from http://www.inkscape.org/ and follow
|
|
the instructions to set up a shell alias:
|
|
|
|
http://wiki.inkscape.org/wiki/index.php/MacOS_X#Inkscape_command_line
|
|
MSG
|
|
'gs' => "sudo apt-get install ghostscript or brew install ghostscript",
|
|
'convert' => "sudo apt-get install imagemagick or brew install imagemagick",
|
|
'montage' => "sudo apt-get install imagemagick or brew install imagemagick"
|
|
}.freeze
|
|
|
|
desc "prereqs", "Check your system for necessary pre-requisites"
|
|
def prereqs
|
|
[ 'inkscape -v', 'gs -v', 'montage -version', 'convert -version' ].each do |command|
|
|
name = command.split(' ').first
|
|
puts "Checking #{name}..."
|
|
|
|
begin
|
|
output = %x{#{command}}
|
|
|
|
if $?.exitstatus != 0
|
|
puts "Problem running #{name}! Output below:"
|
|
puts output
|
|
end
|
|
rescue Errno::ENOENT
|
|
puts "#{name} not installed! To install:"
|
|
|
|
puts PREREQ_MESSAGES[name]
|
|
end
|
|
end
|
|
end
|
|
|
|
desc "install [ dir ]", "Install a Cardfile and sample template"
|
|
def install(dir = '.')
|
|
directory '.', dir
|
|
|
|
Dir.chdir dir do
|
|
system %{bundle install}
|
|
end
|
|
end
|
|
|
|
desc "merged_file", "Write out a merged file"
|
|
def merged_file
|
|
context.write_merged_file
|
|
end
|
|
|
|
desc "svgs", "Write out individual SVG files"
|
|
def svgs
|
|
write_svgs
|
|
end
|
|
|
|
desc "pngs", "Write out individual PNG files"
|
|
def pngs
|
|
write_svgs
|
|
ensure_tmp
|
|
|
|
@exported_pngs = Parallel.map(context.individual_files.each_with_index) do |svg_file, index|
|
|
target = Pathname(context.session.png_files_path % index)
|
|
target.parent.mkpath
|
|
|
|
system %{inkscape --export-area-page --export-png "#{target.expand_path}" --export-width #{context.session.png_export_width} --export-background="#ffffffff" "#{svg_file.expand_path}"}
|
|
|
|
target
|
|
end
|
|
end
|
|
|
|
desc "pdf", "Create PDF of card images"
|
|
def pdf
|
|
pngs
|
|
|
|
pdf_obj = context.session.pdf_class.new(card_size: context.session.pdf_card_size)
|
|
|
|
trimmed_pngs = Parallel.map(@exported_pngs) do |png|
|
|
tmp_target = tmp_target_for(png)
|
|
|
|
system %{convert #{png} -gravity Center -crop #{context.session.pdf_card_size}+0+0 +repage #{tmp_target}}
|
|
|
|
tmp_target
|
|
end
|
|
|
|
png_slices = trimmed_pngs.each_slice(pdf_obj.cards_per_page)
|
|
|
|
page_count = trimmed_pngs.length / pdf_obj.cards_per_page
|
|
|
|
placeholder = tmp_target_for("placeholder.png")
|
|
system %{convert -size #{context.session.pdf_card_size} xc:white #{placeholder}}
|
|
|
|
pages = Parallel.map(png_slices.each_with_index) do |files, page_index|
|
|
tmp_pdf_target = tmp_path.join("page%05d.pdf" % page_index)
|
|
|
|
files += Array.new(pdf_obj.cards_per_page - files.length, placeholder)
|
|
|
|
system %{montage -density #{context.session.pdf_dpi} -tile #{pdf_obj.montage_tiling} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_target}}
|
|
|
|
tmp_pdf_target
|
|
end
|
|
|
|
if context.session.card_back
|
|
tmp_target = tmp_target_for(context.session.card_back)
|
|
tmp_pdf_target = tmp_path.join("backs.pdf")
|
|
|
|
system %{convert #{context.session.card_back} -gravity Center -crop #{context.session.pdf_card_size}+0+0 +repage #{tmp_target}}
|
|
system %{montage -density #{context.session.pdf_dpi} -geometry +0+0 #{Array.new(pdf_obj.cards_per_page, tmp_target).join(' ')} #{tmp_pdf_target}}
|
|
|
|
pages.length.times do |page|
|
|
pages << tmp_pdf_target
|
|
end
|
|
end
|
|
|
|
Pathname(context.session.pdf_target).parent.mkpath
|
|
|
|
if context.session.prepend_pdf
|
|
pages.unshift context.session.prepend_pdf
|
|
end
|
|
|
|
system "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=#{context.session.pdf_target} -dBATCH #{pages.join(" ")}"
|
|
end
|
|
|
|
no_tasks do
|
|
def tmp_target_for(file)
|
|
tmp_path.join(Digest::MD5.hexdigest(file.to_s) + '.png')
|
|
end
|
|
|
|
def tmp_path
|
|
@tmp_path ||= Pathname(".tmp")
|
|
end
|
|
|
|
def ensure_tmp
|
|
tmp_path.rmtree if tmp_path.directory?
|
|
tmp_path.mkpath
|
|
end
|
|
|
|
def context
|
|
@context ||= SVGGVS::Context.load(options[:cardfile])
|
|
end
|
|
|
|
def write_svgs
|
|
context.write_individual_files
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
SVGGVS::Cli.start
|