Class: Nanoc2::Page

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

Overview

A Nanoc2::Page represents a page in a nanoc site. It has content and attributes, as well as a path. It can also store the modification time to speed up compilation.

Each page has a list of page representations or reps (Nanoc2::PageRep); compiling a page actually compiles all of its representations.

Constant Summary collapse

DEFAULTS =

Default values for pages.

{
  :custom_path  => nil,
  :extension    => 'html',
  :filename     => 'index',
  :filters_pre  => [],
  :filters_post => [],
  :layout       => 'default',
  :skip_output  => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, attributes, path, mtime = nil) ⇒ Page

Creates a new page.

content

This page’s unprocessed content.

attributes

A hash containing this page’s attributes.

path

This page’s path.

mtime

The time when this page was last modified.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nanoc2/base/page.rb', line 55

def initialize(content, attributes, path, mtime=nil)
  # Set primary attributes
  @attributes     = attributes.clean
  @content        = content
  @path           = path.cleaned_path
  @mtime          = mtime

  # Start disconnected
  @parent         = nil
  @children       = []
  @reps           = []
end

Instance Attribute Details

#attributesObject

A hash containing this page’s attributes.



35
36
37
# File 'lib/nanoc2/base/page.rb', line 35

def attributes
  @attributes
end

#childrenObject

The child pages of this page.



29
30
31
# File 'lib/nanoc2/base/page.rb', line 29

def children
  @children
end

#contentObject (readonly)

This page’s raw, uncompiled content.



32
33
34
# File 'lib/nanoc2/base/page.rb', line 32

def content
  @content
end

#mtimeObject (readonly)

The time when this page was last modified.



41
42
43
# File 'lib/nanoc2/base/page.rb', line 41

def mtime
  @mtime
end

#parentObject

The parent page of this page. This can be nil even for non-root pages.



26
27
28
# File 'lib/nanoc2/base/page.rb', line 26

def parent
  @parent
end

#pathObject (readonly)

This page’s path.



38
39
40
# File 'lib/nanoc2/base/page.rb', line 38

def path
  @path
end

#repsObject (readonly)

This page’s list of page representations.



44
45
46
# File 'lib/nanoc2/base/page.rb', line 44

def reps
  @reps
end

#siteObject

The Nanoc2::Site this page belongs to.



23
24
25
# File 'lib/nanoc2/base/page.rb', line 23

def site
  @site
end

Instance Method Details

#attribute_named(name) ⇒ Object

Returns the attribute with the given name.



96
97
98
99
100
# File 'lib/nanoc2/base/page.rb', line 96

def attribute_named(name)
  return @attributes[name] if @attributes.has_key?(name)
  return @site.page_defaults.attributes[name] if @site.page_defaults.attributes.has_key?(name)
  return DEFAULTS[name]
end

#build_repsObject

Builds the individual page representations (Nanoc2::PageRep) for this page.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/nanoc2/base/page.rb', line 70

def build_reps
  # Get list of rep names
  rep_names_default = (@site.page_defaults.attributes[:reps] || {}).keys
  rep_names_this    = (@attributes[:reps] || {}).keys + [ :default ]
  rep_names         = rep_names_default | rep_names_this

  # Get list of reps
  reps = rep_names.inject({}) do |memo, rep_name|
    rep = (@attributes[:reps] || {})[rep_name]
    is_bad = (@attributes[:reps] || {}).has_key?(rep_name) && rep.nil?
    is_bad ? memo : memo.merge(rep_name => rep || {})
  end

  # Build reps
  @reps = []
  reps.each_pair do |name, attrs|
    @reps << PageRep.new(self, attrs, name)
  end
end

#deleteObject

Deletes the page. Tells the site’s data source to delete the page.



120
121
122
123
124
# File 'lib/nanoc2/base/page.rb', line 120

def delete
  @site.data_source.loading do
    @site.data_source.delete_page(self)
  end
end

#inspectObject



126
127
128
# File 'lib/nanoc2/base/page.rb', line 126

def inspect
  "<#{self.class} path=#{self.path}>"
end

#move_to(new_path) ⇒ Object

Moves the page to a new path. Tells the site’s data source to move the page.



113
114
115
116
117
# File 'lib/nanoc2/base/page.rb', line 113

def move_to(new_path)
  @site.data_source.loading do
    @site.data_source.move_page(self, new_path)
  end
end

#saveObject

Saves the page in the database, creating it if it doesn’t exist yet or updating it if it already exists. Tells the site’s data source to save the page.



105
106
107
108
109
# File 'lib/nanoc2/base/page.rb', line 105

def save
  @site.data_source.loading do
    @site.data_source.save_page(self)
  end
end

#to_proxyObject

Returns a proxy (Nanoc2::PageProxy) for this page.



91
92
93
# File 'lib/nanoc2/base/page.rb', line 91

def to_proxy
  @proxy ||= PageProxy.new(self)
end