Class: EeePub::Maker

Inherits:
Object
  • Object
show all
Defined in:
lib/eeepub/maker.rb

Overview

The class to make ePub easily

Examples:

epub = EeePub.make do
  title       'sample'
  creator     'jugyo'
  publisher   'jugyo.org'
  date        '2010-05-06'
  identifier  'http://example.com/book/foo', :scheme => 'URL'
  uid         'http://example.com/book/foo'

  files ['/path/to/foo.html', '/path/to/bar.html']
  nav [
    {:label => '1. foo', :content => 'foo.html', :nav => [
      {:label => '1.1 foo-1', :content => 'foo.html#foo-1'}
    ]},
    {:label => '1. bar', :content => 'bar.html'}
  ]
end
epub.save('sample.epub')

Direct Known Subclasses

Easy

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Maker

Returns a new instance of Maker.

Parameters:

  • block (Proc)

    the block for initialize



63
64
65
66
67
68
69
70
# File 'lib/eeepub/maker.rb', line 63

def initialize(&block)
  @files ||= []
  @nav ||= []
  @ncx_file ||= 'toc.ncx'
  @opf_file ||= 'content.opf'

  instance_eval(&block) if block_given?
end

Instance Method Details

#identifier(id, options) ⇒ Object



57
58
59
60
# File 'lib/eeepub/maker.rb', line 57

def identifier(id, options)
  @identifiers ||= []
  @identifiers << {:value => id, :scheme => options[:scheme]}
end

#save(filename) ⇒ Object

Save as ePub file

Parameters:

  • filename (String)

    the ePub file name to save



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/eeepub/maker.rb', line 75

def save(filename)
  Dir.mktmpdir do |dir|
    @files.each do |file|
      case file
      when String
        FileUtils.cp(file, dir)
      when Hash
        file_path, dir_path = *file.first
        dest_dir = File.join(dir, dir_path)
        FileUtils.mkdir_p(dest_dir)
        FileUtils.cp(file_path, dest_dir)
      end
    end

    NCX.new(
      :uid => @uid,
      :title => @titles[0],
      :nav => @nav
    ).save(File.join(dir, @ncx_file))

    OPF.new(
      :title => @titles,
      :identifier => @identifiers,
      :creator => @creators,
      :publisher => @publishers,
      :date => @dates,
      :language => @languages,
      :subject => @subjects,
      :description => @descriptions,
      :rights => @rightss,
      :relation => @relations,
      :manifest => @files.map{|file|
        case file
        when String
          File.basename(file)
        when Hash
          file_path, dir_path = *file.first
          File.join(dir_path, File.basename(file_path))
        end
      },
      :ncx => @ncx_file
    ).save(File.join(dir, @opf_file))

    OCF.new(
      :dir => dir,
      :container => @opf_file
    ).save(filename)
  end
end