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 = {}, fonts = []) ⇒ Book

Returns a new instance of Book.



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

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

Dynamic Method Handling

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

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.



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

#fontsArray<String> (readonly)

Returns all the fonts referred to in the stylesheet.

Returns:

  • (Array<String>)

    all the fonts referred to in the stylesheet



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

def fonts
  @fonts
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.



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

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



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

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

#each(&block) ⇒ Object



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

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.



73
74
75
# File 'lib/rpub/book.rb', line 73

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

#has_cover?Boolean

Returns:

  • (Boolean)


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

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

#has_fonts?Boolean

Returns:

  • (Boolean)


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

def has_fonts?
  fonts.any?
end

#has_toc?Boolean

Returns:

  • (Boolean)


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

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

#imagesObject



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

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

#outlineObject



43
44
45
# File 'lib/rpub/book.rb', line 43

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.



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

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