Class: Rpub::Book
- Inherits:
-
Object
- Object
- Rpub::Book
- 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
-
#chapters ⇒ Array
readonly
List of chapters, one for every input markdown file.
-
#config ⇒ Hash
readonly
The hash of configuration options read from the config.yml file.
-
#layout ⇒ String
readonly
The path the layout HTML file to use to wrap the chapter in.
Instance Method Summary collapse
-
#add_chapter(content) ⇒ Object
(also: #<<)
Add textual content as a new Chapter to this book.
- #each(&block) ⇒ Object
-
#filename ⇒ String
Output filename for epub, based on the book title and version number.
- #has_cover? ⇒ Boolean
- #has_fonts? ⇒ Boolean
- #has_toc? ⇒ Boolean
- #images ⇒ Object
-
#initialize(layout, config = {}) ⇒ Book
constructor
A new instance of Book.
- #outline ⇒ Object
-
#uid ⇒ String
Unique identifier for this entire book to be used in the epub manifest files.
Methods included from HashDelegation
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
#chapters ⇒ Array (readonly)
Returns List of chapters, one for every input markdown file.
15 16 17 |
# File 'lib/rpub/book.rb', line 15 def chapters @chapters end |
#config ⇒ Hash (readonly)
Returns 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 |
#layout ⇒ String (readonly)
Returns 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:
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 |
#filename ⇒ String
Returns 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
37 38 39 |
# File 'lib/rpub/book.rb', line 37 def has_cover? !!config.fetch('cover_image') { false } end |
#has_fonts? ⇒ 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
33 34 35 |
# File 'lib/rpub/book.rb', line 33 def has_toc? !!config.fetch('toc') { false } end |
#images ⇒ Object
45 46 47 |
# File 'lib/rpub/book.rb', line 45 def images map { |chapter| chapter.images }.flatten.uniq end |
#outline ⇒ Object
41 42 43 |
# File 'lib/rpub/book.rb', line 41 def outline inject([]) { |all, chapter| all << [chapter.filename, chapter.outline] } end |
#uid ⇒ String
Returns 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 |