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

- (Book) initialize(context)

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

- (Array<Chapter>) chapters (readonly)

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

- (Object) add_chapter(content) 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

- (Object) each(&block)



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

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

- (String) filename

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

- (Boolean) has_cover?

Returns:

  • (Boolean)


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

def has_cover?
  !!config.cover_image
end

- (Boolean) has_fonts?

Returns:

  • (Boolean)


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

def has_fonts?
  fonts.any?
end

- (Boolean) has_toc?

Returns:

  • (Boolean)


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

def has_toc?
  !!config.toc
end

- (Object) images



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

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

- (Object) outline



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

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

- (String) uid

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