Class: Rpub::Chapter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#contentString (readonly)



8
9
10
# File 'lib/rpub/chapter.rb', line 8

def content
  @content
end

#layoutString (readonly)



14
15
16
# File 'lib/rpub/chapter.rb', line 14

def layout
  @layout
end

#numberFixnum (readonly)



11
12
13
# File 'lib/rpub/chapter.rb', line 11

def number
  @number
end

Instance Method Details

#filenameString



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

#imagesArray<String>



56
57
58
# File 'lib/rpub/chapter.rb', line 56

def images
  @images ||= elements(:img).map { |e| e.attr['src'] }
end

#outlineArray<#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.options[:level],
      :text    => element_text(element),
      :html_id => Kramdown::Converter::Html.send(:new, @document, { :auto_id_prefix => '' }).generate_id(element.options[:raw_text])
    })
  end
end

#titleString



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_htmlString



32
33
34
# File 'lib/rpub/chapter.rb', line 32

def to_html
  @to_html ||= @document.to_html
end

#uidString



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_idString



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

def xml_id
  @id ||= "chapter-#{number}"
end