class Kindai::Publisher

Attributes

book[RW]
root_path[RW]

Public Class Methods

new_from_path(root_path) click to toggle source
# File lib/kindai/publisher.rb, line 6
def self.new_from_path(root_path)
  me = self.new
  me.root_path = root_path
  me
end

Public Instance Methods

divide() click to toggle source
# File lib/kindai/publisher.rb, line 32
def divide
  config(:divide, true)
  self
end
empty(glob) click to toggle source
# File lib/kindai/publisher.rb, line 37
def empty(glob)
  FileUtils.rm_r(Dir.glob(File.join(self.root_path, glob)))
end
ensure_book() click to toggle source
# File lib/kindai/publisher.rb, line 85
def ensure_book
  @book ||= Kindai::Book.new_from_local_directory(root_path)
  true
end
name(n) click to toggle source
# File lib/kindai/publisher.rb, line 12
def name(n)
  config(:name, n)
  self
end
publish() click to toggle source
# File lib/kindai/publisher.rb, line 41
def publish
  Kindai::Util.logger.info("publish #{root_path}, #{config(:name)}")
  raise "no name" unless config(:name)
  self.ensure_book
  if seems_finished?
    Kindai::Util.logger.info("already published")
    return
  end
  create_directory

  path = original_path

  path = trim!(path) if trim?
  path = divide!(path) if divide?
  path = resize!(path) if resize?
  path = zip!(path) if zip?
end
publish_auto() click to toggle source
# File lib/kindai/publisher.rb, line 59
def publish_auto
  self.ensure_book
  self.clone.trim.resize(1280, 960).trim.zip.name('iphone').publish
  self.clone.trim.resize(600, 800).divide.zip.name('kindle').publish
end
publish_default() click to toggle source
# File lib/kindai/publisher.rb, line 65
def publish_default
  self.ensure_book
  self.clone.trim.name('default').publish
end
publish_for_ipad() click to toggle source
# File lib/kindai/publisher.rb, line 75
def publish_for_ipad
  self.ensure_book
  self.clone.trim.resize(768*2, 1024).trim.zip.name('ipad').publish
end
publish_for_iphone() click to toggle source
# File lib/kindai/publisher.rb, line 70
def publish_for_iphone
  self.ensure_book
  self.clone.trim.resize(1280, 960).trim.zip.name('iphone').publish
end
publish_for_kindle() click to toggle source
# File lib/kindai/publisher.rb, line 80
def publish_for_kindle
  self.ensure_book
  self.clone.trim.resize(600, 800).divide.zip.name('kindle').publish
end
resize(width, height) click to toggle source
# File lib/kindai/publisher.rb, line 17
def resize(width, height)
  config(:resize, {:width => width, :height => height})
  self
end
trim(geometry = true) click to toggle source
# File lib/kindai/publisher.rb, line 22
def trim(geometry = true)
  config(:trim, geometry) unless config(:trim)
  self
end
zip() click to toggle source
# File lib/kindai/publisher.rb, line 27
def zip
  config(:zip, true)
  self
end

Protected Instance Methods

config(k, v = nil) click to toggle source

# File lib/kindai/publisher.rb, line 93
def config(k, v = nil)
  @config ||= {}
  return @config[k] unless v
  @config[k] = v
  @config
end
create_directory() click to toggle source

———util————

# File lib/kindai/publisher.rb, line 155
def create_directory
  Dir.mkdir(trim_path) unless File.directory?(trim_path)
  Dir.mkdir(output_path) unless File.directory?(output_path)
end
divide!(source_path) click to toggle source
# File lib/kindai/publisher.rb, line 139
def divide!(source_path)
  files(source_path).each{|file|
    Kindai::Util.divide_43(file, output_path)
    GC.start
  }
  return output_path
end
divide?() click to toggle source
# File lib/kindai/publisher.rb, line 112
def divide?
  config(:divide)
end
files(path) click to toggle source
# File lib/kindai/publisher.rb, line 176
def files(path)
  Dir.glob(File.join(path, '*jpg'))
end
original_files() click to toggle source
# File lib/kindai/publisher.rb, line 172
def original_files
  files(original_path)
end
original_path() click to toggle source
# File lib/kindai/publisher.rb, line 164
def original_path
  File.join(root_path, 'original')
end
output_path() click to toggle source
# File lib/kindai/publisher.rb, line 168
def output_path
  File.join(root_path, File.basename(root_path) + '_' + config(:name))
end
resize!(source_path) click to toggle source
# File lib/kindai/publisher.rb, line 130
def resize!(source_path)
  files(source_path).each{|file|
    dst = File.join(output_path, File.basename(file))
    Kindai::Util.resize_file_to(file, dst, config(:resize))
    GC.start
  }
  return output_path
end
resize?() click to toggle source
# File lib/kindai/publisher.rb, line 104
def resize?
  config(:resize)
end
seems_finished?() click to toggle source
# File lib/kindai/publisher.rb, line 180
def seems_finished?
  zip? ? File.exists?(output_path + '.zip') : File.directory?(output_path)
end
trim!(source_path) click to toggle source

———- aciton ————–

# File lib/kindai/publisher.rb, line 118
def trim!(source_path)
  return trim_path if files(source_path).length == files(trim_path).length
  info = config(:trim).kind_of?(Hash) ? config(:trim) : Kindai::Util.trim_info_auto(book, original_files)
  Kindai::Util.logger.info "trim position: #{info}"
  files(source_path).each{|file|
    dst = File.join(trim_path, File.basename(file))
    Kindai::Util.trim_file_to(file, dst, info)
    GC.start
  }
  return trim_path
end
trim?() click to toggle source
# File lib/kindai/publisher.rb, line 100
def trim?
  config(:trim)
end
trim_path() click to toggle source
# File lib/kindai/publisher.rb, line 160
def trim_path
  File.join(root_path, 'trim')
end
zip!(source_path) click to toggle source
# File lib/kindai/publisher.rb, line 147
def zip!(source_path)
  Kindai::Util.generate_zip(source_path)
  FileUtils.rm_r(self.output_path)
  return source_path
end
zip?() click to toggle source
# File lib/kindai/publisher.rb, line 108
def zip?
  config(:zip)
end