Class: Rpub::Chapter
- Inherits:
-
Object
- Object
- Rpub::Chapter
- Defined in:
- lib/rpub/chapter.rb
Overview
Representation of a chapter in the book, from a single input file from the project directory. The Chapter object knows how to turn itself into HTML suitable for writing to the epub archive with the appropriate identifiers to be listed in the epub manifest files.
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
Raw textual contents of this chapter.
-
#layout ⇒ String
readonly
Filename of the layout to use, to be passed directly to the Kramdown gem.
-
#number ⇒ Fixnum
readonly
Chapter number provided by its associated Book object.
Instance Method Summary collapse
-
#filename ⇒ String
Name for the file in the zip to use, based on the title.
-
#images ⇒ Array<String>
List of all image references.
-
#initialize(content, number, layout) ⇒ Chapter
constructor
A new instance of Chapter.
-
#outline ⇒ Array<#text,#level>
Ordered headers for this chapter, each header as an object responding to #level and #text.
-
#title ⇒ String
Text of the first heading in this chapter.
-
#to_html ⇒ String
Content parsed to HTML by the markdown engine.
-
#uid ⇒ String
Unique identifier for this chapter.
-
#xml_id ⇒ String
XML-friendly slug for this chapter based on its number.
Constructor Details
#initialize(content, number, layout) ⇒ Chapter
16 17 18 19 |
# File 'lib/rpub/chapter.rb', line 16 def initialize(content, number, layout) @content, @number, @layout = content, number, layout @document = Kramdown::Document.new(content, KRAMDOWN_OPTIONS.merge(:template => layout)) end |
Instance Attribute Details
#content ⇒ String (readonly)
8 9 10 |
# File 'lib/rpub/chapter.rb', line 8 def content @content end |
#layout ⇒ String (readonly)
14 15 16 |
# File 'lib/rpub/chapter.rb', line 14 def layout @layout end |
#number ⇒ Fixnum (readonly)
11 12 13 |
# File 'lib/rpub/chapter.rb', line 11 def number @number end |
Instance Method Details
#filename ⇒ String
37 38 39 |
# File 'lib/rpub/chapter.rb', line 37 def filename @filename ||= xml_id.to_s + '-' + title.gsub(/[^\w\.]/i, '-').squeeze('-').downcase.chomp('-') + '.html' end |
#images ⇒ Array<String>
56 57 58 |
# File 'lib/rpub/chapter.rb', line 56 def images @images ||= elements(:img).map { |e| e.attr['src'] } end |
#outline ⇒ Array<#text,#level>
Ordered headers for this chapter, each header as an object responding to #level and #text.
45 46 47 48 49 50 51 52 53 |
# File 'lib/rpub/chapter.rb', line 45 def outline @outline ||= elements(:header).map do |element| OpenStruct.new({ :level => element.[:level], :text => element_text(element), :html_id => Kramdown::Converter::Html.send(:new, @document, { :auto_id_prefix => '' }).generate_id(element.[:raw_text]) }) end end |
#title ⇒ String
61 62 63 64 65 |
# File 'lib/rpub/chapter.rb', line 61 def title @title ||= begin (heading = outline.first) ? heading.text : 'untitled' end end |
#to_html ⇒ String
32 33 34 |
# File 'lib/rpub/chapter.rb', line 32 def to_html @to_html ||= @document.to_html end |
#uid ⇒ String
22 23 24 |
# File 'lib/rpub/chapter.rb', line 22 def uid @uid ||= Digest::SHA1.hexdigest([content, xml_id.to_s, layout].join) end |
#xml_id ⇒ String
27 28 29 |
# File 'lib/rpub/chapter.rb', line 27 def xml_id @id ||= "chapter-#{number}" end |