Permalink
Browse files

added import command

  • Loading branch information...
skoji committed Mar 4, 2012
1 parent 20b1bc5 commit e3a5e60b64dd07b387e610313de797e3569f91c1
Showing with 37 additions and 0 deletions.
  1. +11 −0 lib/gepub/resource_builder.rb
  2. +24 −0 spec/builder_spec.rb
  3. +2 −0 spec/fixtures/builder/text/localresource.conf
@@ -23,6 +23,7 @@ def method_missing(name, *args)
def initialize(book, attributes = {}, &block)
@last_defined_item = nil
@book = book
+ @dir_prefix = ""
@file_postprocess = {}
@file_preprocess = {}
@files_postprocess = {}
@@ -74,6 +75,15 @@ def glob(arg)
files(*Dir.glob(arg))
end
+ def import(conf, args = {})
+ dir_prefix_org = @dir_prefix
+ @dir_prefix = args[:dir_prefix] || ""
+ Dir.chdir(File.dirname(conf)) {
+ instance_eval(File.new(File.basename(conf)).read)
+ }
+ @dir_prefix = dir_prefix_org
+ end
+
def cover_image(val)
file(val)
@last_defined_item.cover_image
@@ -144,6 +154,7 @@ def create_one_file(val)
name = val[0]
io = val[1]
end
+ name = "#{@dir_prefix}/#{name}" if !@dir_prefix.nil? && @dir_prefix.size > 0
@book.add_item(name, io)
end
end
View
@@ -183,6 +183,30 @@
builder.instance_eval{ @book.item_by_href('text/cover.xhtml') }.should_not be_nil
end
+ it 'should add files to book with import with prefix' do
+ workdir = File.join(File.dirname(__FILE__),'fixtures', 'builder')
+ builder = GEPUB::Builder.new {
+ resources(:workdir => workdir) {
+ import 'text/localresource.conf'
+ }
+ }
+ builder.instance_eval{ @book.item_by_href('memo.txt') }.should_not be_nil
+ builder.instance_eval{ @book.item_by_href('memo.txt').content.chomp }.should == 'just a plain text.'
+ builder.instance_eval{ @book.item_by_href('cover.xhtml') }.should_not be_nil
+ end
+
+ it 'should add files to book with import with prefix' do
+ workdir = File.join(File.dirname(__FILE__),'fixtures', 'builder')
+ builder = GEPUB::Builder.new {
+ resources(:workdir => workdir) {
+ import 'text/localresource.conf', :dir_prefix => 'text'
+ }
+ }
+ builder.instance_eval{ @book.item_by_href('text/memo.txt') }.should_not be_nil
+ builder.instance_eval{ @book.item_by_href('text/memo.txt').content.chomp }.should == 'just a plain text.'
+ builder.instance_eval{ @book.item_by_href('text/cover.xhtml') }.should_not be_nil
+ end
+
it 'should add files to book from IO object' do
io = File.new(File.join(File.dirname(__FILE__),'fixtures', 'builder', 'text', 'memo.txt'))
builder = GEPUB::Builder.new {
@@ -0,0 +1,2 @@
+file 'memo.txt'
+file 'cover.xhtml'

0 comments on commit e3a5e60

Please sign in to comment.