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(parent = nil, name) ⇒ Target

Returns a new instance of Target.

Parameters:

  • parent (Target) (defaults to: nil)

    reference to parent target

  • name (String)

    name of this target



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

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

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

  @default_styles = []
  @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:



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

def book
  @book
end

#nameString, Symbol (readonly)

Returns target name.

Returns:

  • (String, Symbol)

    target name



47
48
49
# File 'lib/epuber/book/target.rb', line 47

def name
  @name
end

#root_tocEpuber::Book::TocItem (readonly)



55
56
57
# File 'lib/epuber/book/target.rb', line 55

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)


227
228
229
230
231
232
233
# File 'lib/epuber/book/target.rb', line 227

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

#add_default_style(*file_paths) ⇒ void

This method returns an undefined value.

Parameters:

  • file_paths (Array<String>)


240
241
242
243
244
245
246
247
# File 'lib/epuber/book/target.rb', line 240

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>)


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

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/epuber/book/target.rb', line 193

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>)


215
216
217
218
219
220
# File 'lib/epuber/book/target.rb', line 215

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>)


111
112
113
# File 'lib/epuber/book/target.rb', line 111

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

#cover_imageFileRequest

Returns file request to cover image.

Returns:



170
171
172
173
# File 'lib/epuber/book/target.rb', line 170

attribute :cover_image,
types:        [FileRequest],
inherited:    true,
auto_convert: { [String] => ->(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



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

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)



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

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

#default_stylesArray<Epuber::Book::FileRequest>

Returns:



117
118
119
# File 'lib/epuber/book/target.rb', line 117

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



177
178
179
# File 'lib/epuber/book/target.rb', line 177

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

#epub_versionString

Returns version of result epub.

Returns:

  • (String)

    version of result epub



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

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

#filesArray<Epuber::Book::FileRequest>

Returns all files

Returns:



97
98
99
100
101
102
103
104
105
106
# File 'lib/epuber/book/target.rb', line 97

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

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

  all_files
end

#fixed_layoutBool

Returns whether the target uses fixed layout.

Returns:

  • (Bool)

    whether the target uses fixed layout



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

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

#freezeObject



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

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

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

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

  @root_toc.freeze
  @constants.freeze
end

#ibooks?Bool

Returns:

  • (Bool)


86
87
88
89
90
91
92
# File 'lib/epuber/book/target.rb', line 86

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



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

attribute :identifier,
inherited: true

#is_ibooksBool

Returns hint for compiler to add some iBooks related stuff.

Returns:

  • (Bool)

    hint for compiler to add some iBooks related stuff



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

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

#isbnString

Returns isbn of epub.

Returns:

  • (String)

    isbn of epub



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

attribute :isbn,
inherited: true

#pluginsArray<String>

Returns:

  • (Array<String>)


123
124
125
# File 'lib/epuber/book/target.rb', line 123

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

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

Create new sub_target with name

Parameters:

  • name (String)

Yields:

  • (child)

Returns:

  • (Target)

    new created sub target



74
75
76
77
78
79
80
# File 'lib/epuber/book/target.rb', line 74

def sub_target(name)
  child = create_child_item(name)

  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



271
272
273
# File 'lib/epuber/book/target.rb', line 271

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

#use(path) ⇒ nil

Parameters:

  • path (String)

    use some file/module/package

Returns:

  • (nil)


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

def use(path)
  @plugins << path
end