Class: Monograph::Chapter

Inherits:
Object
  • Object
show all
Defined in:
lib/monograph/chapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book, path) ⇒ Chapter

Returns a new instance of Chapter.



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

def initialize(book, path)
  @book = book
  @path = path
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



4
5
6
# File 'lib/monograph/chapter.rb', line 4

def book
  @book
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/monograph/chapter.rb', line 5

def path
  @path
end

Instance Method Details

#htmlObject



24
25
26
27
28
29
# File 'lib/monograph/chapter.rb', line 24

def html
  @html ||= begin
    rc = Redcarpet::Markdown.new(MarkdownRenderer.new(:with_toc_data => true), :fenced_code_blocks => true)
    rc.render(self.raw)
  end
end

#numberObject



12
13
14
# File 'lib/monograph/chapter.rb', line 12

def number
  @number ||= permalink.split('-').first.to_i
end


16
17
18
# File 'lib/monograph/chapter.rb', line 16

def permalink
  @permalink ||= @path.split('/').last.gsub(/\.md\z/, '')
end

#rawObject



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

def raw
  @raw ||= File.open(File.join(@path), 'rb', &:read)
end

#sections(level = 2) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/monograph/chapter.rb', line 39

def sections(level = 2)
  items = self.html.scan(/<h#{level} id=\"([a-z0-9\-\_]+)\">(.*?)<\/h#{level}>/m)
  items.inject({}) do |hash, match|
    hash[match[0]] = match[1]
    hash
  end
end

#template_contextObject



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

def template_context
  @template_context ||= ChapterTemplateContext.new(self)
end

#titleObject



31
32
33
# File 'lib/monograph/chapter.rb', line 31

def title
  @title ||= raw =~ /\A\# ([a-z0-9 ]+)$/i ? $1 : 'Unknown'
end