Class: Rpub::Book

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
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

Constructor Details

#initialize(context) ⇒ Book

Returns a new instance of Book.



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

def initialize(context)
  @chapters = []
  @context = context
  @context.chapter_files.each(&method(:<<))
end

Instance Attribute Details

#chaptersArray<Chapter> (readonly)

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

Returns:

  • (Array<Chapter>)

    List of chapters, one for every input markdown file.



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

def chapters
  @chapters
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



52
53
54
55
# File 'lib/rpub/book.rb', line 52

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

#each(&block) ⇒ Object



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

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.



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

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

#has_cover?Boolean

Returns:

  • (Boolean)


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

def has_cover?
  !!config.cover_image
end

#has_fonts?Boolean

Returns:

  • (Boolean)


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

def has_fonts?
  fonts.any?
end

#has_toc?Boolean

Returns:

  • (Boolean)


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

def has_toc?
  !!config.toc
end

#imagesObject



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

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

#outlineObject



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

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.



60
61
62
# File 'lib/rpub/book.rb', line 60

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