Class: Pekky::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/pekky/pages.rb

Overview

The page class encapsulates everything needed to render the actual content to disk – the destination path, content and templates. It also contains the meta-data for a page – parents, children, name etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, content, opts) ⇒ Page

The Page class is initialized with the path, the content – either a Content instance or a hash – and a hash of options. It then determines the name of the output file and it’s path – pages without extensions default to ‘index.html’. It also determines the format if specified.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pekky/pages.rb', line 19

def initialize(name, path, content, opts)
  if content.is_a?(Pekky::Content)
    @content = content
  else
    opts = content
  end

  @path = path
  @opts = opts
  @children = []
  @opts[:name] = name
  
  if match = @path.match(/^([\S+]?\/)(\S+)\.(\S+)$/)
    @directory = match[1]
    @format = match[3]
    @file_name = "#{match[2]}.#{@format}"
  else
    @directory = path
    @format = 'html'
    @file_name = "index.html"
  end
end

Instance Attribute Details

#childrenObject (readonly)

Various attribute accessors for storing all the values we reuse across the different methods.



8
9
10
# File 'lib/pekky/pages.rb', line 8

def children
  @children
end

#contentObject (readonly)

Various attribute accessors for storing all the values we reuse across the different methods.



8
9
10
# File 'lib/pekky/pages.rb', line 8

def content
  @content
end

#directoryObject (readonly)

Various attribute accessors for storing all the values we reuse across the different methods.



8
9
10
# File 'lib/pekky/pages.rb', line 8

def directory
  @directory
end

#file_nameObject (readonly)

Various attribute accessors for storing all the values we reuse across the different methods.



8
9
10
# File 'lib/pekky/pages.rb', line 8

def file_name
  @file_name
end

#nameObject (readonly)

Various attribute accessors for storing all the values we reuse across the different methods.



8
9
10
# File 'lib/pekky/pages.rb', line 8

def name
  @name
end

#optsObject (readonly)

Various attribute accessors for storing all the values we reuse across the different methods.



8
9
10
# File 'lib/pekky/pages.rb', line 8

def opts
  @opts
end

#parentObject

This accessor will be used to store a reference to the parent page – if there is one. The parent page will be updated when the tree gets built. See the Builder class for more info.



13
14
15
# File 'lib/pekky/pages.rb', line 13

def parent
  @parent
end

#pathObject (readonly)

Various attribute accessors for storing all the values we reuse across the different methods.



8
9
10
# File 'lib/pekky/pages.rb', line 8

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object

Compares one page instance with another. It does this by comparing the name – which will be unique.



44
45
46
# File 'lib/pekky/pages.rb', line 44

def ==(other)
  name == other.name
end

#child?Boolean

Is this page a child or does it sit at the top level of the site? It just checks to see if there is only one leading slash. Cheaty, but it works ;)

Returns:

  • (Boolean)


57
58
59
# File 'lib/pekky/pages.rb', line 57

def child?
  @path.count("/") == 1
end

#hidden?Boolean

Checks to see if the page is hidden i.e. it doesn’t appear in the main navigation. The user can specify this when declaring a page in the site.rb

Returns:

  • (Boolean)


51
52
53
# File 'lib/pekky/pages.rb', line 51

def hidden?
  !!opts[:hidden]
end

#renderObject

The business part of the page. It renders the page and outputs it to disk. Firstly it asks the Builder to ensure the target directory is on disk. Then it sets up the context – encapsulating the content – and the view and layout. Finally it renders the page and writes it to disk.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pekky/pages.rb', line 65

def render
  Builder.directory(@directory)
  
  @content.reload if @content
  context = Context.new(self)
  view_output = view(:view, @opts).render(context)
  
  File.open(File.join(Config[:output_dir], @directory, @file_name), 'w') do |f|
    if @opts[:layout] == false
      f.write(view_output)
    else
      layout = view(:layout, opts)
      context.view_output = view_output
      context.is_layout = true
      f.write(layout.render(context))
    end
  end
end