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<Chapter>
readonly
List of chapters, one for every input markdown file.
-
#config ⇒ Hash
readonly
The hash of configuration options read from the config.yml file.
-
#fonts ⇒ Array<String>
readonly
All the fonts referred to in the stylesheet.
-
#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 = {}, fonts = []) ⇒ 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 = {}, 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
#chapters ⇒ Array<Chapter> (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 |
#fonts ⇒ Array<String> (readonly)
Returns all the fonts referred to in the stylesheet.
18 19 20 |
# File 'lib/rpub/book.rb', line 18 def fonts @fonts end |
#layout ⇒ String (readonly)
Returns 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:
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 |
#filename ⇒ String
Returns 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
39 40 41 |
# File 'lib/rpub/book.rb', line 39 def has_cover? !!config.fetch('cover_image') { false } end |
#has_fonts? ⇒ Boolean
31 32 33 |
# File 'lib/rpub/book.rb', line 31 def has_fonts? fonts.any? end |
#has_toc? ⇒ Boolean
35 36 37 |
# File 'lib/rpub/book.rb', line 35 def has_toc? !!config.fetch('toc') { false } end |
#images ⇒ Object
47 48 49 |
# File 'lib/rpub/book.rb', line 47 def images map { |chapter| chapter.images }.flatten.uniq end |
#outline ⇒ Object
43 44 45 |
# File 'lib/rpub/book.rb', line 43 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.
67 68 69 |
# File 'lib/rpub/book.rb', line 67 def uid @uid ||= Digest::SHA1.hexdigest [config.inspect, map(&:uid)].join end |