Class: Epuber::Book

Inherits:
DSL::Object show all
Defined in:
lib/epuber/book.rb,
lib/epuber/book/target.rb,
lib/epuber/book/toc_item.rb,
lib/epuber/book/contributor.rb,
lib/epuber/book/file_request.rb

Defined Under Namespace

Classes: Contributor, FileRequest, NormalContributor, StandardError, Target, TocItem

Instance Attribute Summary collapse

Attributes inherited from DSL::Object

#file_path

Other methods collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DSL::Object

from_file, #from_file?, from_string, #to_s

Methods included from DSL::AttributeSupport

#attribute, #define_method_attr, #dsl_attributes, #find_root

Constructor Details

#initialize {|_self| ... } ⇒ Book

Returns a new instance of Book.

Yields:

  • (_self)

Yield Parameters:

  • _self (Epuber::Book)

    the object that the method was called on



14
15
16
17
18
19
20
21
# File 'lib/epuber/book.rb', line 14

def initialize
  super

  @default_target = Target.new(nil)
  @toc_blocks     = []

  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Epuber::DSL::Object

Instance Attribute Details

#default_targetEpuber::Book::Target (readonly)



49
50
51
# File 'lib/epuber/book.rb', line 49

def default_target
  @default_target
end

Class Method Details

.default_target_attribute(sym, readonly: false) ⇒ Void

Defines setter and getter for default target attribute

Parameters:

  • sym (Symbol)

    attribute name

Returns:

  • (Void)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/epuber/book.rb', line 58

def self.default_target_attribute(sym, readonly: false)
  # getter
  define_method(sym) do
    @default_target.send(sym)
  end

  return if readonly

  # setter
  setter_method = "#{sym}="
  define_method(setter_method) do |new_value|
    @default_target.send(setter_method, new_value)
  end
end

Instance Method Details

#abstract_target(name) ⇒ Object



113
114
115
116
117
118
# File 'lib/epuber/book.rb', line 113

def abstract_target(name)
  @default_target.sub_abstract_target(name) do |target|
    target.book = self
    yield target if block_given?
  end
end

#add_const(*args) ⇒ Object

Add constant to target, constants can be used within text files



256
257
258
# File 'lib/epuber/book.rb', line 256

def add_const(*args)
  @default_target.add_const(*args)
end

#add_default_script(*file_paths) ⇒ Object

Add default script to default target, default scripts will be automatically added to xhtml document



274
275
276
# File 'lib/epuber/book.rb', line 274

def add_default_script(*file_paths)
  @default_target.add_default_script(*file_paths)
end

#add_default_scripts(*file_paths) ⇒ Object

Add default scripts to default target, default scripts will be automatically added to xhtml document



280
281
282
# File 'lib/epuber/book.rb', line 280

def add_default_scripts(*file_paths)
  @default_target.add_default_scripts(*file_paths)
end

#add_default_style(*file_paths) ⇒ Object

Add default styles to default target, default styles will be automatically added to xhtml document



262
263
264
# File 'lib/epuber/book.rb', line 262

def add_default_style(*file_paths)
  @default_target.add_default_style(*file_paths)
end

#add_default_styles(*file_paths) ⇒ Object

Add default styles to default target, default styles will be automatically added to xhtml document



268
269
270
# File 'lib/epuber/book.rb', line 268

def add_default_styles(*file_paths)
  @default_target.add_default_styles(*file_paths)
end

#add_file(*args) ⇒ Object

Add file to book, see Epuber::Book::Target#add_file to more details



244
245
246
# File 'lib/epuber/book.rb', line 244

def add_file(*args)
  @default_target.add_file(*args)
end

#add_files(*file_paths) ⇒ Object

Add files to book, see Target#add_files to more details



250
251
252
# File 'lib/epuber/book.rb', line 250

def add_files(*file_paths)
  @default_target.add_files(*file_paths)
end

#all_targetsArray<Target>

All targets

Returns:



80
81
82
83
84
85
86
# File 'lib/epuber/book.rb', line 80

def all_targets
  if @default_target.sub_targets.empty?
    [@default_target]
  else
    @default_target.sub_targets
  end
end

#authorsArray<Contributor>

Returns authors of book.

Returns:



158
159
160
161
162
163
# File 'lib/epuber/book.rb', line 158

attribute :authors,
types: [Contributor, NormalContributor],
container: Array,
required: true,
singularize: true,
auto_convert: { [String, Hash] => ->(value) { Contributor.from_obj(value) } }

#build_versionString

Returns build version of book.

Returns:

  • (String)

    build version of book



232
233
234
# File 'lib/epuber/book.rb', line 232

attribute :build_version,
types: [Version],
auto_convert: { [String, Integer, Float] => Version }

#buildable_targetsObject



96
97
98
# File 'lib/epuber/book.rb', line 96

def buildable_targets
  flat_all_targets.reject(&:is_abstract)
end

#cover_imageString

Returns path or name of cover image.

Returns:

  • (String)

    path or name of cover image



196
# File 'lib/epuber/book.rb', line 196

default_target_attribute :cover_image

#create_mobiBool

Returns whether the target should create mobi.

Returns:

  • (Bool)

    whether the target should create mobi



204
# File 'lib/epuber/book.rb', line 204

default_target_attribute :create_mobi

#custom_fontsBool

Returns book uses custom fonts (used only for iBooks).

Returns:

  • (Bool)

    book uses custom fonts (used only for iBooks)



188
# File 'lib/epuber/book.rb', line 188

default_target_attribute :custom_fonts

#default_viewportSize

Returns default view port size.

Returns:

  • (Size)

    default view port size



200
# File 'lib/epuber/book.rb', line 200

default_target_attribute :default_viewport

#epub_versionString|Fixnum

Returns epub version.

Returns:

  • (String|Fixnum)

    epub version



184
# File 'lib/epuber/book.rb', line 184

default_target_attribute :epub_version

#finish_tocObject

Returns nil.

Returns:

  • nil



25
26
27
28
29
30
31
# File 'lib/epuber/book.rb', line 25

def finish_toc
  @toc_blocks.each do |block|
    flat_all_targets.each do |target|
      target.toc(&block)
    end
  end
end

#fixed_layoutBool

Returns whether the book uses fixed layout.

Returns:

  • (Bool)

    whether the book uses fixed layout



192
# File 'lib/epuber/book.rb', line 192

default_target_attribute :fixed_layout

#flat_all_targetsObject



88
89
90
91
92
93
94
# File 'lib/epuber/book.rb', line 88

def flat_all_targets
  if @default_target.sub_targets.empty?
    [@default_target]
  else
    @default_target.flat_sub_items
  end
end

#freezeObject

Returns nil.

Returns:

  • nil



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

def freeze
  super
  @default_target.freeze
end

#identifierString

Returns book identifier used in OPF file.

Returns:

  • (String)

    book identifier used in OPF file



208
# File 'lib/epuber/book.rb', line 208

default_target_attribute :identifier

#is_ibooksBool

Returns book is for iBooks.

Returns:

  • (Bool)

    book is for iBooks



176
# File 'lib/epuber/book.rb', line 176

default_target_attribute :is_ibooks

#isbnString

Returns isbn of this book.

Returns:

  • (String)

    isbn of this book



180
# File 'lib/epuber/book.rb', line 180

default_target_attribute :isbn

#languageString

Returns language of this book.

Returns:

  • (String)

    language of this book



172
# File 'lib/epuber/book.rb', line 172

attribute :language

#output_base_nameString

Returns base name for output epub file.

Returns:

  • (String)

    base name for output epub file



238
239
# File 'lib/epuber/book.rb', line 238

attribute :output_base_name,
inherited: true

Returns isbn of printed book.

Returns:

  • (String)

    isbn of printed book



213
# File 'lib/epuber/book.rb', line 213

attribute :print_isbn

#publishedDate

Returns date of book was published.

Returns:

  • (Date)

    date of book was published



217
218
219
# File 'lib/epuber/book.rb', line 217

attribute :published,
types: [Date],
auto_convert: { String => Date }

#publisherString

Returns publisher name.

Returns:

  • (String)

    publisher name



168
# File 'lib/epuber/book.rb', line 168

attribute :publisher

#subtitleString

Returns subtitle of book.

Returns:

  • (String)

    subtitle of book



154
# File 'lib/epuber/book.rb', line 154

attribute :subtitle

#target(name) ⇒ Target

Defines new target

Parameters:

  • name (String, Symbol)

Returns:



106
107
108
109
110
111
# File 'lib/epuber/book.rb', line 106

def target(name)
  @default_target.sub_target(name) do |target|
    target.book = self
    yield target if block_given?
  end
end

#target_named(target_name) ⇒ Epuber::Book::Target?

Finds target with name or nil when not found

Parameters:

Returns:



301
302
303
304
305
306
307
# File 'lib/epuber/book.rb', line 301

def target_named(target_name)
  return target_name if target_name.is_a?(Epuber::Book::Target)

  flat_all_targets.find do |target|
    target.name == target_name || target.name.to_s == target_name.to_s
  end
end

#targets(*names, &block) ⇒ Array<Target>

Defines several new targets with same configuration

Parameters:

  • names (Array<String, Symbol>)

Returns:

  • (Array<Target>)

    result target



126
127
128
129
130
131
132
133
134
# File 'lib/epuber/book.rb', line 126

def targets(*names, &block)
  if names.empty?
    UI.warning('Book#targets to get all targets is deprecated, use #all_targets instead',
               location: caller_locations.first)
    return all_targets
  end

  names.map { |name| target(name, &block) }
end

#titleString

Returns title of book.

Returns:

  • (String)

    title of book



149
150
# File 'lib/epuber/book.rb', line 149

attribute :title,
required: true

#toc(&block) ⇒ Object

Returns nil.

Returns:

  • nil



141
142
143
# File 'lib/epuber/book.rb', line 141

def toc(&block)
  @toc_blocks << block
end

#use(path) ⇒ Object

Method to add plugin, that should be used while building book



286
287
288
# File 'lib/epuber/book.rb', line 286

def use(path)
  @default_target.use(path)
end

#validateObject

Returns nil.

Returns:

  • nil



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

def validate
  super
  @default_target.validate
end

#versionVersion

Note:

Is used only for ibooks versions

Returns book version.

Returns:



225
226
227
228
# File 'lib/epuber/book.rb', line 225

attribute :version,
inherited: true,
types: [Version],
auto_convert: { [String, Integer, Float] => Version }