Class: Slimdown::Page

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

Overview

The model representing a page

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(absolute_path) ⇒ Page

Get new page object

Parameters:

  • absolute_path (String)

    The absolute path to this document, including extension.



15
16
17
18
19
20
21
22
# File 'lib/slimdown/page.rb', line 15

def initialize(absolute_path)
  # Open the markdown file.
  @absolute_path = absolute_path

  @parsed_page = Slimdown::PageParser.new @absolute_path

  load_headers
end

Instance Attribute Details

#headersObject (readonly)

All the document headers



9
10
11
# File 'lib/slimdown/page.rb', line 9

def headers
  @headers
end

#templateObject (readonly)

The template from the document headers



7
8
9
# File 'lib/slimdown/page.rb', line 7

def template
  @template
end

#titleObject (readonly)

The title from the document headers



5
6
7
# File 'lib/slimdown/page.rb', line 5

def title
  @title
end

Class Method Details

.find(path) ⇒ Slimdown::Page

Get page object by relative path.

Example:

Slimdown::Page.find('about/contact')

Parameters:

  • path (String)

    relative path to page. Doesn’t include extension.

Returns:



31
32
33
34
35
36
37
# File 'lib/slimdown/page.rb', line 31

def self.find(path)
  # Finds the relative page.
  config = Slimdown.config
  loc = config.location

  self.new("#{loc}/#{path}.md")
end

Instance Method Details

#bodyKramdown::Document

Get the parsed body

Returns:

  • (Kramdown::Document)

    the parsed Markdown body.



42
43
44
# File 'lib/slimdown/page.rb', line 42

def body
  @parsed_page.body
end

#childrenArray<Slimdown::Page>

The children of this document.

Returns:



61
62
63
64
# File 'lib/slimdown/page.rb', line 61

def children
  # Check to see whether dir exists.
  Slimdown::Folder.new(@absolute_path.chomp('.md')).pages
end

#parent<Slimdown::Page>|nil

The parent of this document

Returns:



69
70
71
72
73
74
75
# File 'lib/slimdown/page.rb', line 69

def parent
  parent = File.expand_path('..', @absolute_path).concat('.md')

  return nil unless File.file?(parent)

  Slimdown::Page.new(parent)
end

#pathString

The relative path for this document.

Returns:

  • (String)

    the relative path, e.g. ‘about/contact’



80
81
82
# File 'lib/slimdown/page.rb', line 80

def path
  @absolute_path.sub(%r{^#{Slimdown.config.location}/(.*)\.md}, '\1')
end

#siblingsArray<Slimdown::Page>

The sibling pages to this document.

Returns:



49
50
51
52
53
54
55
56
# File 'lib/slimdown/page.rb', line 49

def siblings
  # List other markdown files in the same folder.

  # Sibling folder.
  folder = File.dirname @absolute_path

  Slimdown::Folder.new(folder).pages
end