Class: Rpub::Book

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HashDelegation
Defined in:
lib/rpub/book.rb

Overview

The Book object wraps a collection of chapter objects and knows about its ordering, the book metadata from the configuration file and the book output filename.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashDelegation

#method_missing, #respond_to?

Constructor Details

#initialize(layout, config = {}) ⇒ Book

Returns a new instance of Book.



20
21
22
# File 'lib/rpub/book.rb', line 20

def initialize(layout, config = {})
  @chapters, @config, @layout = [], config, layout
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rpub::HashDelegation

Instance Attribute Details

#chaptersArray (readonly)

Returns List of chapters, one for every input markdown file.

Returns:

  • (Array)

    List of chapters, one for every input markdown file.



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

def chapters
  @chapters
end

#configHash (readonly)

Returns The hash of configuration options read from the config.yml file.

Returns:

  • (Hash)

    The hash of configuration options read from the config.yml file.



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

def config
  @config
end

#layoutString (readonly)

Returns the path the layout HTML file to use to wrap the chapter in.

Returns:

  • (String)

    the path the layout HTML file to use to wrap the chapter in.



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

def layout
  @layout
end

Instance Method Details

#add_chapter(content) ⇒ Object Also known as: <<

Add textual content as a new Chapter to this book.

This method returns the ‘Book` object iself, so you can chain multiple calls:

Examples:

Chaining mutliple calls

book << 'foo' << 'bar'

Parameters:

  • content (String)

    is chapter text to add



57
58
59
60
# File 'lib/rpub/book.rb', line 57

def add_chapter(content)
  chapters << Chapter.new(content, chapters.size, layout)
  self
end

#each(&block) ⇒ Object



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

def each(&block)
  chapters.each(&block)
end

#filenameString

Returns output filename for epub, based on the book title and version number.

Returns:

  • (String)

    output filename for epub, based on the book title and version number.



71
72
73
# File 'lib/rpub/book.rb', line 71

def filename
  @filename ||= [config['title'], config['version']].join('-').gsub(/[^\w\.]/i, '-').squeeze('-').downcase.chomp('-') + '.epub'
end

#has_cover?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rpub/book.rb', line 37

def has_cover?
  !!config.fetch('cover_image') { false }
end

#has_fonts?Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/rpub/book.rb', line 28

def has_fonts?
  fonts = config.fetch('fonts') { [] }
  fonts.respond_to?(:any?) && fonts.any?
end

#has_toc?Boolean

Returns:

  • (Boolean)


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

def has_toc?
  !!config.fetch('toc') { false }
end

#imagesObject



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

def images
  map { |chapter| chapter.images }.flatten.uniq
end

#outlineObject



41
42
43
# File 'lib/rpub/book.rb', line 41

def outline
  inject([]) { |all, chapter| all << [chapter.filename, chapter.outline] }
end

#uidString

Returns Unique identifier for this entire book to be used in the epub manifest files.

Returns:

  • (String)

    Unique identifier for this entire book to be used in the epub manifest files.



65
66
67
# File 'lib/rpub/book.rb', line 65

def uid
  @uid ||= Digest::SHA1.hexdigest [config.inspect, map(&:uid)].join
end