Class: Epuber::Book::Target

Inherits:
DSL::TreeObject show all
Defined in:
lib/epuber/book/target.rb

Instance Attribute Summary collapse

Attributes inherited from DSL::TreeObject

#parent, #sub_items

Attributes inherited from DSL::Object

#file_path

Instance Method Summary collapse

Methods inherited from DSL::TreeObject

#create_child_item, #create_child_items, #flat_sub_items, #root?, #validate

Methods inherited from DSL::Object

from_file, #from_file?, from_string, #to_s, #validate

Methods included from DSL::AttributeSupport

#attribute, #define_method_attr, #dsl_attributes, #find_root

Constructor Details

#initialize(name, parent: nil) ⇒ Target

Returns a new instance of Target.

Parameters:

  • parent (Target) (defaults to: nil)

    reference to parent target

  • name (String)

    name of this target



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/epuber/book/target.rb', line 15

def initialize(name, parent: nil)
  super(parent)

  @name      = name
  @is_ibooks = nil
  @book      = nil
  @files     = []
  @constants = {}
  @root_toc  = TocItem.new

  @default_styles = []
  @default_scripts = []
  @plugins = []
end

Dynamic Method Handling

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

Instance Attribute Details

#bookEpuber::Book

Returns reference to book.

Returns:



66
67
68
# File 'lib/epuber/book/target.rb', line 66

def book
  @book || parent&.book
end

#nameString, Symbol (readonly)

Returns target name.

Returns:

  • (String, Symbol)

    target name



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

def name
  @name
end

#root_tocEpuber::Book::TocItem (readonly)



58
59
60
# File 'lib/epuber/book/target.rb', line 58

def root_toc
  @root_toc
end

Instance Method Details

#add_const(key, value = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • key (String)
  • value (String) (defaults to: nil)


257
258
259
260
261
262
263
# File 'lib/epuber/book/target.rb', line 257

def add_const(key, value = nil)
  if key.is_a?(Hash) && value.nil?
    @constants.merge!(key)
  else
    @constants[key] = value
  end
end

#add_default_script(*file_paths) ⇒ void

This method returns an undefined value.

Parameters:

  • file_paths (Array<String>)


298
299
300
301
302
303
304
305
# File 'lib/epuber/book/target.rb', line 298

def add_default_script(*file_paths)
  file_paths.map do |file_path|
    file_obj          = add_file(file_path, group: :script)
    file_obj.only_one = true

    @default_scripts << file_obj unless @default_scripts.include?(file_obj)
  end
end

#add_default_scripts(*file_paths) ⇒ void

This method returns an undefined value.

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

Only difference with #add_default_script is it adds multiple files with one pattern

Parameters:

  • file_paths (Array<String>)


314
315
316
317
318
319
320
321
# File 'lib/epuber/book/target.rb', line 314

def add_default_scripts(*file_paths)
  file_paths.map do |file_path|
    file_obj          = add_file(file_path, group: :script)
    file_obj.only_one = false

    @default_scripts << file_obj unless @default_scripts.include?(file_obj)
  end
end

#add_default_style(*file_paths) ⇒ void

This method returns an undefined value.

Parameters:

  • file_paths (Array<String>)


269
270
271
272
273
274
275
276
# File 'lib/epuber/book/target.rb', line 269

def add_default_style(*file_paths)
  file_paths.map do |file_path|
    file_obj          = add_file(file_path, group: :style)
    file_obj.only_one = true

    @default_styles << file_obj unless @default_styles.include?(file_obj)
  end
end

#add_default_styles(*file_paths) ⇒ void

This method returns an undefined value.

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

Only difference with #add_default_style is it adds multiple files with one pattern

Parameters:

  • file_paths (Array<String>)


285
286
287
288
289
290
291
292
# File 'lib/epuber/book/target.rb', line 285

def add_default_styles(*file_paths)
  file_paths.map do |file_path|
    file_obj          = add_file(file_path, group: :style)
    file_obj.only_one = false

    @default_styles << file_obj unless @default_styles.include?(file_obj)
  end
end

#add_file(file_path, group: nil) ⇒ Epuber::Book::File

Returns created file.

Parameters:

  • file_path (String | Epuber::Book::File)
  • group (Symbol) (defaults to: nil)

Returns:

  • (Epuber::Book::File)

    created file



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/epuber/book/target.rb', line 223

def add_file(file_path, group: nil)
  file = if file_path.is_a?(FileRequest)
           file_path
         else
           FileRequest.new(file_path, group: group)
         end


  old_file = @files.find { |f| f == file }

  if old_file.nil?
    @files << file
    file
  else
    old_file
  end
end

#add_files(*file_paths) ⇒ void

This method returns an undefined value.

Parameters:

  • file_paths (Array<String>)


245
246
247
248
249
250
# File 'lib/epuber/book/target.rb', line 245

def add_files(*file_paths)
  file_paths.each do |file_path|
    file_obj          = add_file(file_path)
    file_obj.only_one = false
  end
end

#constantsHash<String, Object>

Returns all constants

Returns:

  • (Hash<String, Object>)


128
129
130
# File 'lib/epuber/book/target.rb', line 128

def constants
  (parent&.constants || {}).merge(@constants)
end

#cover_imageFileRequest

Returns file request to cover image.

Returns:



192
193
194
195
196
197
# File 'lib/epuber/book/target.rb', line 192

attribute :cover_image,
                types: [FileRequest],
                inherited: true,
                auto_convert: { [String] => lambda { |value|
  FileRequest.new(value, group: :image, properties: [:cover_image])
} }

#create_mobiBool

Returns whether the target should create mobi.

Returns:

  • (Bool)

    whether the target should create mobi



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

attribute :create_mobi,
types: [TrueClass, FalseClass],
inherited: true

#custom_fontsString

Returns target will use custom font (for iBooks only).

Returns:

  • (String)

    target will use custom font (for iBooks only)



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

attribute :custom_fonts,
types: [TrueClass, FalseClass],
inherited: true

#default_scriptsArray<Epuber::Book::FileRequest>

Returns:



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

def default_scripts
  ((parent && parent.default_scripts) || []) + @default_scripts
end

#default_stylesArray<Epuber::Book::FileRequest>

Returns:



134
135
136
# File 'lib/epuber/book/target.rb', line 134

def default_styles
  ((parent && parent.default_styles) || []) + @default_styles
end

#default_viewportSize

Returns size of view port, mainly this is used for fixed layout.

Returns:

  • (Size)

    size of view port, mainly this is used for fixed layout



201
202
203
# File 'lib/epuber/book/target.rb', line 201

attribute :default_viewport,
types: [Size],
inherited: true

#epub_versionString

Returns version of result epub.

Returns:

  • (String)

    version of result epub



156
157
158
159
160
# File 'lib/epuber/book/target.rb', line 156

attribute :epub_version,
required: true,
inherited: true,
auto_convert: { [String, Integer, Float] => Version },
default_value: 3.0

#filesArray<Epuber::Book::FileRequest>

Returns all files

Returns:



116
117
118
119
120
121
122
123
# File 'lib/epuber/book/target.rb', line 116

def files
  # parent files plus our files
  all_files = ((parent && parent.files) || []) + @files + @default_styles + @default_scripts

  all_files << @attributes_values[:cover_image] unless @attributes_values[:cover_image].nil?

  all_files
end

#fixed_layoutBool

Returns whether the target uses fixed layout.

Returns:

  • (Bool)

    whether the target uses fixed layout



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

attribute :fixed_layout,
types: [TrueClass, FalseClass],
inherited: true

#freezeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/epuber/book/target.rb', line 30

def freeze
  super
  @files.freeze
  @files.each(&:freeze)

  @default_styles.freeze
  @default_styles.each(&:freeze)

  @default_scripts.freeze
  @default_scripts.each(&:freeze)

  @plugins.freeze
  @plugins.each(&:freeze)

  @root_toc.freeze
  @constants.freeze
end

#ibooks?Bool

Returns:

  • (Bool)


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

def ibooks?
  if is_ibooks.nil?
    name.to_s.include?('ibooks')
  else
    is_ibooks
  end
end

#identifierString

Returns book identifier used in OPF file.

Returns:

  • (String)

    book identifier used in OPF file



169
170
# File 'lib/epuber/book/target.rb', line 169

attribute :identifier,
inherited: true

#is_abstractBool

Returns allows to create abstract targets that are only used as template for other targets.

Returns:

  • (Bool)

    allows to create abstract targets that are only used as template for other targets



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

attribute :is_abstract,
types: [TrueClass, FalseClass],
inherited: false

#is_ibooksBool

Returns hint for compiler to add some iBooks related stuff.

Returns:

  • (Bool)

    hint for compiler to add some iBooks related stuff



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

attribute :is_ibooks,
types: [TrueClass, FalseClass],
inherited: true

#isbnString

Returns isbn of epub.

Returns:

  • (String)

    isbn of epub



164
165
# File 'lib/epuber/book/target.rb', line 164

attribute :isbn,
inherited: true

#pluginsArray<String>

Returns:

  • (Array<String>)


146
147
148
# File 'lib/epuber/book/target.rb', line 146

def plugins
  ((parent && parent.plugins) || []) + @plugins
end

#sub_abstract_target(name) {|child| ... } ⇒ Target

Create new sub_target with name

Parameters:

  • name (String)

Yields:

  • (child)

Returns:

  • (Target)

    new created sub target



91
92
93
94
95
96
97
98
99
# File 'lib/epuber/book/target.rb', line 91

def sub_abstract_target(name)
  child = create_child_item(name)
  child.book = book
  child.is_abstract = true

  yield child if block_given?

  child
end

#sub_target(name) {|child| ... } ⇒ Target

Create new sub_target with name

Parameters:

  • name (String)

Yields:

  • (child)

Returns:

  • (Target)

    new created sub target



76
77
78
79
80
81
82
83
# File 'lib/epuber/book/target.rb', line 76

def sub_target(name)
  child = create_child_item(name)
  child.book = book

  yield child if block_given?

  child
end

#toc {|toc_item, target| ... } ⇒ Object

Returns nil.

Yields:

Yield Parameters:

  • toc_item (TocItem)

    root toc item

  • target (self)

    current target

Returns:

  • nil



329
330
331
# File 'lib/epuber/book/target.rb', line 329

def toc
  yield(@root_toc, self) if block_given?
end

#use(path) ⇒ nil

Parameters:

  • path (String)

    use some file/module/package

Returns:

  • (nil)


337
338
339
# File 'lib/epuber/book/target.rb', line 337

def use(path)
  @plugins << path
end