Class: Monograph::Chapter
- Inherits:
-
Object
- Object
- Monograph::Chapter
- Defined in:
- lib/monograph/chapter.rb
Instance Attribute Summary collapse
-
#book ⇒ Object
readonly
Returns the value of attribute book.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #html ⇒ Object
-
#initialize(book, path) ⇒ Chapter
constructor
A new instance of Chapter.
- #number ⇒ Object
- #permalink ⇒ Object
- #raw ⇒ Object
- #sections(level = 2) ⇒ Object
- #template_context ⇒ Object
- #title ⇒ Object
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
#book ⇒ Object (readonly)
Returns the value of attribute book.
4 5 6 |
# File 'lib/monograph/chapter.rb', line 4 def book @book end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/monograph/chapter.rb', line 5 def path @path end |
Instance Method Details
#html ⇒ Object
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 |
#number ⇒ Object
12 13 14 |
# File 'lib/monograph/chapter.rb', line 12 def number @number ||= permalink.split('-').first.to_i end |
#permalink ⇒ Object
16 17 18 |
# File 'lib/monograph/chapter.rb', line 16 def permalink @permalink ||= @path.split('/').last.gsub(/\.md\z/, '') end |
#raw ⇒ Object
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_context ⇒ Object
35 36 37 |
# File 'lib/monograph/chapter.rb', line 35 def template_context @template_context ||= ChapterTemplateContext.new(self) end |
#title ⇒ Object
31 32 33 |
# File 'lib/monograph/chapter.rb', line 31 def title @title ||= raw =~ /\A\# ([a-z0-9 ]+)$/i ? $1 : 'Unknown' end |