Class: Rpub::Chapter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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.



19
20
21
22
# File 'lib/rpub/chapter.rb', line 19

def initialize(content, number, layout)
  @content, @number, @layout = content, number, layout
  @document = Rpub.document_factory.call(content, layout)
end

Instance Attribute Details

#contentString (readonly)

Returns raw textual contents of this chapter.

Returns:

  • (String)

    raw textual contents of this chapter



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

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.



17
18
19
# File 'lib/rpub/chapter.rb', line 17

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



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

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



35
36
37
# File 'lib/rpub/chapter.rb', line 35

def filename
  @filename ||= xml_id.to_s + '-' + title.gsub(/[^\w\.]/i, '-').squeeze('-').downcase.chomp('-') + '.html'
end

#uidString

Returns Unique identifier for this chapter.

Returns:

  • (String)

    Unique identifier for this chapter.



25
26
27
# File 'lib/rpub/chapter.rb', line 25

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.



30
31
32
# File 'lib/rpub/chapter.rb', line 30

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