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

Returns a new instance of 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)

Returns raw textual contents of this chapter.

Returns:

  • (String)

    raw textual contents of this chapter



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

def content
  @content
end

#layoutString (readonly)

Returns filename of the layout to use, to be passed directly to the Kramdown gem.

Returns:

  • (String)

    filename of the layout to use, to be passed directly to the Kramdown gem.



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

def layout
  @layout
end

#numberFixnum (readonly)

Returns chapter number provided by its associated Book object.

Returns:

  • (Fixnum)

    chapter number provided by its associated Book object



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

def number
  @number
end

Instance Method Details

#filenameString

Returns name for the file in the zip to use, based on the title.

Returns:

  • (String)

    name for the file in the zip to use, based on the title



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>

Returns list of all image references.

Returns:

  • (Array<String>)

    list of all image references



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.

Returns:

  • (Array<#text,#level>)

    list of headers for this chapter



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

Returns Text of the first heading in this chapter.

Returns:

  • (String)

    Text of the first heading in this chapter



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

Returns content parsed to HTML by the markdown engine.

Returns:

  • (String)

    content parsed to HTML by the markdown engine.



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

def to_html
  @to_html ||= Typogruby.improve(@document.to_html)
end

#uidString

Returns Unique identifier for this chapter.

Returns:

  • (String)

    Unique identifier for this chapter.



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

Returns XML-friendly slug for this chapter based on its number.

Returns:

  • (String)

    XML-friendly slug for this chapter based on its number.



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

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