Class: Daisy::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/daisy/book.rb

Overview

A whole book.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_dir, target_dir, re_encode: false) ⇒ Book

Creates a new Book:

  • input mp3 files are in source_dir

  • output mp3 files & daisy files will go to target_dir

  • if re_encode is true, each mp3 will be re-encoded using lame

Raises an error if no mp3 file is found.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/daisy/book.rb', line 56

def initialize(source_dir, target_dir, re_encode: false)
  @source_dir = source_dir
  @target_dir = target_dir
  @re_encode = re_encode

  @chapters = Dir["#{File.expand_path(source_dir)}/*.mp3"].map { |path| Chapter.new(self, path) }
  @chapters.empty? and raise Error, "no mp3 file in #{source_dir.inspect}"
  @chapters.sort_by!(&:file)
  @chapters.each.with_index(1) { |c, i| c.position = i }

  @identifier = SecureRandom.uuid
  read_book_info
end

Instance Attribute Details

#chaptersObject (readonly)

array of Chapter objects, one by input mp3 file



21
22
23
# File 'lib/daisy/book.rb', line 21

def chapters
  @chapters
end

#creatorObject (readonly)

book author



30
31
32
# File 'lib/daisy/book.rb', line 30

def creator
  @creator
end

#identifierObject (readonly)

generated unique identifier



24
25
26
# File 'lib/daisy/book.rb', line 24

def identifier
  @identifier
end

#languageObject (readonly)

book language



36
37
38
# File 'lib/daisy/book.rb', line 36

def language
  @language
end

#narratorObject (readonly)

book narrator



33
34
35
# File 'lib/daisy/book.rb', line 33

def narrator
  @narrator
end

#publisherObject (readonly)

book publisher



39
40
41
# File 'lib/daisy/book.rb', line 39

def publisher
  @publisher
end

#re_encodeObject (readonly)

re-encode with lame?



18
19
20
# File 'lib/daisy/book.rb', line 18

def re_encode
  @re_encode
end

#rightsObject (readonly)

book rights



42
43
44
# File 'lib/daisy/book.rb', line 42

def rights
  @rights
end

#source_dirObject (readonly)

directory for source mp3 files



12
13
14
# File 'lib/daisy/book.rb', line 12

def source_dir
  @source_dir
end

#source_editionObject (readonly)

book source edition



48
49
50
# File 'lib/daisy/book.rb', line 48

def source_edition
  @source_edition
end

#source_publisherObject (readonly)

book source publisher



45
46
47
# File 'lib/daisy/book.rb', line 45

def source_publisher
  @source_publisher
end

#target_dirObject (readonly)

directory where the DAISY files will be stored



15
16
17
# File 'lib/daisy/book.rb', line 15

def target_dir
  @target_dir
end

#titleObject (readonly)

book title



27
28
29
# File 'lib/daisy/book.rb', line 27

def title
  @title
end

Instance Method Details

#create_daisyObject

Create all the DAISY metadata for the book.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/daisy/book.rb', line 132

def create_daisy
  puts "Creating DAISY format in #{target_dir}"
  puts "Title: #{title.inspect}"
  FileUtils.mkpath target_dir unless File.directory?(target_dir)
  write_ncc_html
  write_title_smil
  puts "Contents:"
  elapsed_time = 0.0
  chapters.each do |c|
    puts "- #{c.position}. #{c.chapter_title}"
    c.copy_mp3 re_encode: re_encode
    c.write_smil elapsed_time
    elapsed_time += c.duration
  end
  write_master_smil
end

#total_durationObject

total book duration from chapter durations



127
128
129
# File 'lib/daisy/book.rb', line 127

def total_duration
  chapters.map(&:duration).sum
end