Class: PageViewer::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/page_viewer/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, name) ⇒ Page

Returns a new instance of Page.



10
11
12
13
14
# File 'lib/page_viewer/page.rb', line 10

def initialize(root, name)
  @root = root
  @name = name
  parse_page
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/page_viewer/page.rb', line 4

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'lib/page_viewer/page.rb', line 4

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/page_viewer/page.rb', line 4

def name
  @name
end

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/page_viewer/page.rb', line 4

def root
  @root
end

Class Method Details

.path(root, name) ⇒ Object



6
7
8
# File 'lib/page_viewer/page.rb', line 6

def self.path(root, name)
  File.join(root, name + '.md')
end

Instance Method Details

#contentsObject



26
27
28
29
30
# File 'lib/page_viewer/page.rb', line 26

def contents
  @contents ||= File.open(self.class.path(root, name), 'r:utf-8') do |file|
    file.read
  end
end

#parse_pageObject



16
17
18
19
20
21
22
23
24
# File 'lib/page_viewer/page.rb', line 16

def parse_page
  if contents =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m
    @headers = YAML.load($1)
    @body = $3
  else
    @headers = {}
    @body = contents
  end
end