lib/epub/maker.rb in epub-maker-0.0.1 vs lib/epub/maker.rb in epub-maker-0.0.2
- old
+ new
@@ -1,5 +1,6 @@
+require 'English'
require 'pathname'
require 'pathname/common_prefix'
require 'fileutils'
require 'tmpdir'
require 'time'
@@ -14,67 +15,78 @@
require 'epub/maker/publication'
require 'epub/maker/content_document'
module EPUB
module Maker
+ class Error < StandardError; end
+
class << self
# @param path [Pathname|#to_path|String]
# @todo Add option whether mv blocks or not when file locked already
# @todo Timeout when file shared-locked long time
def make(path)
path = Pathname(path) unless path.kind_of? Pathname
book = EPUB::Book.new
- Pathname.mktmpdir 'epub-maker' do |dir|
- temp_path = dir/path.basename
- mimetype = dir/'mimetype'
- mimetype.write EPUB::MediaType::EPUB
- Archive::Zip.open temp_path.to_path, :w do |archive|
- file = Archive::Zip::Entry.from_file(mimetype.to_path, compression_codec: Archive::Zip::Codec::Store)
- archive.add_entry file
- end
+ dir = Pathname.mktmpdir 'epub-maker'
+ temp_path = dir/path.basename
+ mimetype = dir/'mimetype'
+ mimetype.write EPUB::MediaType::EPUB
+ Archive::Zip.open temp_path.to_path, :w do |archive|
+ file = Archive::Zip::Entry.from_file(mimetype.to_path, compression_codec: Archive::Zip::Codec::Store)
+ archive.add_entry file
+ end
- Zip::Archive.open temp_path.to_path do |archive|
- yield book if block_given?
- book.save archive
- end
+ Zip::Archive.open temp_path.to_path do |archive|
+ yield book if block_given?
+ book.save archive
+ end
- path.open 'wb' do |file|
- raise "File locked by other process: #{path}" unless file.flock File::LOCK_SH|File::LOCK_NB
- ($VERBOSE ? ::FileUtils::Verbose : ::FileUtils).move temp_path.to_path, path.to_path
- end
+ path.open 'wb' do |file|
+ raise Error, "File locked by other process: #{path}" unless file.flock File::LOCK_SH|File::LOCK_NB
+ ($VERBOSE ? ::FileUtils::Verbose : ::FileUtils).move temp_path.to_path, path.to_path
end
+ dir.remove_entry_secure
book
# validate
# build_xml
# archive
+ rescue => error
+ backtrace = error.backtrace
+ error = error.exception([
+ error.message,
+ "[#{self}]Working directory remained at: #{dir}"
+ ].join($RS))
+ backtrace.unshift("#{__FILE__}:#{__LINE__}:in `rescue in #{__method__}'"); error.set_backtrace backtrace; raise error
end
end
end
- def make_ocf
- self.ocf = OCF.new
- ocf.make do |ocf|
- yield ocf if block_given?
+ module Book::Features
+ def make_ocf
+ self.ocf = OCF.new
+ ocf.make do |ocf|
+ yield ocf if block_given?
+ end
+ ocf
end
- ocf
- end
- def make_package
- self.package = Publication::Package.new
- package.make do |package|
- yield package if block_given?
+ def make_package
+ self.package = Publication::Package.new
+ package.make do |package|
+ yield package if block_given?
+ end
+ package
end
- package
- end
- # @param archive [Zip::Archive]
- def save(archive)
- ocf.save archive
- package.save archive
- resources.each do |item|
- item.save archive
+ # @param archive [Zip::Archive]
+ def save(archive)
+ ocf.save archive
+ package.save archive
+ resources.each do |item|
+ item.save archive
+ end
end
end
end
class Pathname
@@ -97,17 +109,21 @@
new(Dir.mktmpdir(prefix_suffix, tmpdir))
end
end
end
- def write(string, mode='w', perm=0666)
- open mode, perm do |file|
- file << string
- end
- end
-
def remove_entry_secure
FileUtils.remove_entry_secure to_path
end
- alias / +
+ unless method_defined? :write
+ def write(string, mode='w', perm=0666)
+ open mode, perm do |file|
+ file << string
+ end
+ end
+ end
+
+ unless method_defined? :/
+ alias / +
+ end
end