Class: Rpub::Book
- Inherits:
-
Object
- Object
- Rpub::Book
- 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
-
#chapters ⇒ Array<Chapter>
readonly
List of chapters, one for every input markdown file.
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(context) ⇒ 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.
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
#chapters ⇒ Array<Chapter> (readonly)
Returns 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:
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 |
#filename ⇒ String
Returns 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
32 33 34 |
# File 'lib/rpub/book.rb', line 32 def has_cover? !!config.cover_image end |
#has_fonts? ⇒ Boolean
24 25 26 |
# File 'lib/rpub/book.rb', line 24 def has_fonts? fonts.any? end |
#has_toc? ⇒ Boolean
28 29 30 |
# File 'lib/rpub/book.rb', line 28 def has_toc? !!config.toc end |
#images ⇒ Object
40 41 42 |
# File 'lib/rpub/book.rb', line 40 def images map { |chapter| chapter.images }.flatten.uniq end |
#outline ⇒ Object
36 37 38 |
# File 'lib/rpub/book.rb', line 36 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.
60 61 62 |
# File 'lib/rpub/book.rb', line 60 def uid @uid ||= Digest::SHA1.hexdigest [config.inspect, map(&:uid)].join end |