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



42
43
44
# File 'lib/rpub/chapter.rb', line 42

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



61
62
63
# File 'lib/rpub/chapter.rb', line 61

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



50
51
52
53
54
55
56
57
58
# File 'lib/rpub/chapter.rb', line 50

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



66
67
68
69
70
# File 'lib/rpub/chapter.rb', line 66

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.



37
38
39
# File 'lib/rpub/chapter.rb', line 37

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

#tocKramdown::Element

Returns Toc elements hierarchy.

Returns:

  • (Kramdown::Element)

    Toc elements hierarchy



22
23
24
# File 'lib/rpub/chapter.rb', line 22

def toc
  Kramdown::Converter::Toc.convert(@document.root).first
end

#uidString

Returns Unique identifier for this chapter.

Returns:

  • (String)

    Unique identifier for this chapter.



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

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.



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

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