Class: Pekky::Context

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

Overview

For every page instance in the site there is a corresponding context instance. The context is used when rendering the layout and views, encapsulating the page content and providing the built-in helpers.

It also hosts any helpers provided in the helpers.rb file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Context

Initialize stores the reference to the page instance so that it can expose it to the view when rendering.



15
16
17
18
# File 'lib/pekky/context.rb', line 15

def initialize(page)
  @page = page
  @is_layout = false
end

Instance Attribute Details

#is_layout=(value) ⇒ Object (writeonly)

Some attribute writers for storing information in contexts attached to layouts. This is because the layout needs to have access to the rendered view in order to inject it into itself.



11
12
13
# File 'lib/pekky/context.rb', line 11

def is_layout=(value)
  @is_layout = value
end

#view_output=(value) ⇒ Object (writeonly)

Some attribute writers for storing information in contexts attached to layouts. This is because the layout needs to have access to the rendered view in order to inject it into itself.



11
12
13
# File 'lib/pekky/context.rb', line 11

def view_output=(value)
  @view_output = value
end

Instance Method Details

#ancestorsObject

The pages which sit above this page in the hierarchy i.e. parent, grant-parent etc.



184
185
186
# File 'lib/pekky/context.rb', line 184

def ancestors
  collect_ancestors(@page)
end

#attrs(opts) ⇒ Object

A utility method for converting a hash into a string of attributes.



140
141
142
143
144
145
146
147
# File 'lib/pekky/context.rb', line 140

def attrs(opts)
  out = opts.inject([]) do |a, o|
    a << "#{o[0]}=\"#{o[1]}\""
    a
  end
  
  out.join(" ").chomp
end

#child?Boolean

Returns a boolean specifying if this page is the child of another.

Returns:

  • (Boolean)


194
195
196
# File 'lib/pekky/context.rb', line 194

def child?
  @page.child?
end

#childrenObject

The children of the current page.



178
179
180
# File 'lib/pekky/context.rb', line 178

def children
  @page.children
end

#collect_ancestors(page, ancestors = []) ⇒ Object

A helper method used to recursively collect the ancestors of the current page. It should never be called directly.



205
206
207
208
209
210
211
212
# File 'lib/pekky/context.rb', line 205

def collect_ancestors(page, ancestors = [])
  if page.parent
    collect_ancestors(page.parent, ancestors)
    ancestors.unshift(page.parent)
  else
    ancestors
  end
end

#configObject

A shortcut to the site configuration. This is here to allow users to store and access common, re-used details like the site title.



22
23
24
# File 'lib/pekky/context.rb', line 22

def config
  Config
end

#contentObject

The content assigned to the current page.



168
169
170
# File 'lib/pekky/context.rb', line 168

def content
  @page.content
end

#filterObject

Shortcut to the filters module. Filters accept HTML markup and manipulate it in some way. For example, extracting an modifying links. They’re called in the same way as the formatters.

filter.links(markup)


43
44
45
# File 'lib/pekky/context.rb', line 43

def filter
  Format::Filters
end

#formatObject

A shortcut to the Formatters module. Users call methods on the module, which then calls the relevant formatter.

format.textile(content[:body])

Formatters accept a format like textile or haml and transform it into HTML markup.



33
34
35
# File 'lib/pekky/context.rb', line 33

def format
  Format::Formatters
end

#get(path) ⇒ Object

Makes a call to Content#get and returns the content specified by the path argument. An argument of a directory name will return an array of content objects. The path to a single YAML file will return just the one object.



50
51
52
# File 'lib/pekky/context.rb', line 50

def get(path)
  Content.get(path)
end

#hidden?Boolean

Returns a boolean specifying if this page is hidden.

Returns:

  • (Boolean)


199
200
201
# File 'lib/pekky/context.rb', line 199

def hidden?
  @page.hidden?
end

#image(path, opts = {}) ⇒ Object

Generates an image tag. As a bare minimum, only the name is required, but you can optionally provide additional attributes.

image('pix.jpg', :alt => 'Wicked Cool')


100
101
102
103
# File 'lib/pekky/context.rb', line 100

def image(path, opts = {})
  opts['src'] = Format::Util.qualify_path("#{Config[:images_path]}/#{path}")
  self_closing_tag('img', opts)
end

#javascript(name) ⇒ Object

Generates a javascript tag.



91
92
93
# File 'lib/pekky/context.rb', line 91

def javascript(name)
  tag('script', '', :src => "#{Format::Util.qualify_path("#{Config[:javascripts_path]}/#{name}")}.js", :type => 'text/javascript')
end

Creates an anchor tag with the specified URL. It will also accept additional options for the anchor attributes. If you pass in a link with the ‘pekky://’ protocol, the string will be used as a name look up for an internal page and the URL will be replaced with a real path to the page.

link('ZOMG', 'pekky://news_archive') #=> '<a href="/news/archive">ZOMG</a>'


112
113
114
115
116
117
118
# File 'lib/pekky/context.rb', line 112

def link(text, url, opts = {})
  if url.index("pekky://") == 0
    url = Format::Util.look_up_page(url)
  end
  opts['href'] = Format::Util.qualify_path(url)
  tag('a', text, opts)
end

A convenience method which returns the top-level pages in the site. It excludes any what have been set as hidden.



151
152
153
# File 'lib/pekky/context.rb', line 151

def nav
  Pekky.tree.pages.reject {|p| p.hidden? }
end

#page(name) ⇒ Object

Returns the page with the specified name. This name is set in the site.rb or is derived from a sanitised version of the page’s path.



157
158
159
# File 'lib/pekky/context.rb', line 157

def page(name)
  Pekky.tree[name]
end

#pagesObject

Returns the tree of all pages in the site. You can recurse through the entire tree by accessing the #children method on each page.



163
164
165
# File 'lib/pekky/context.rb', line 163

def pages
  Pekky.tree.pages
end

#parentObject

The current page’s parent.



189
190
191
# File 'lib/pekky/context.rb', line 189

def parent
  @page.parent
end

#partial(path) ⇒ Object

Renders the specified partial. The partial should only have it’s name specified, not it’s entire extension i.e. no ‘.html.haml’

partial('profiles/full')


59
60
61
62
63
# File 'lib/pekky/context.rb', line 59

def partial(path)
  file_name = "#{path}.html.#{Config[:templating]}"
  path = File.join(Config[:partials_dir], file_name)
  Tilt.new(path).render(self)
end

#pathObject

The path to the current page.



173
174
175
# File 'lib/pekky/context.rb', line 173

def path
  Format::Util.qualify_path(@page.path)
end

#self_closing_tag(type, opts = nil) ⇒ Object

Generates a self closing tag of the specified type.



131
132
133
134
135
136
137
# File 'lib/pekky/context.rb', line 131

def self_closing_tag(type, opts = nil)
  if opts
    "<#{type} #{attrs(opts)}/>"
  else
    "<#{type}/>"
  end
end

#stylesheet(name, opts = {}) ⇒ Object

Generates a link to a stylesheet. At it’s most basic you only need to specify the name, but you can also optionally specify the media, name etc.

stylesheet('print', :media => 'print')


81
82
83
84
85
86
87
88
# File 'lib/pekky/context.rb', line 81

def stylesheet(name, opts = {})
  opts['media'] ||= 'screen'
  opts['rel'] ||= 'stylesheet'
  opts['type'] = 'text/css'
  opts['href'] = "#{Format::Util.qualify_path("#{Config[:stylesheets_path]}/#{name}")}.css"
  
  self_closing_tag('link', opts)
end

#tag(type, child, opts = nil) ⇒ Object

Generates a content tag of the specified type. The child can either be text or HTML markup.



122
123
124
125
126
127
128
# File 'lib/pekky/context.rb', line 122

def tag(type, child, opts = nil)
  if opts
    "<#{type} #{attrs(opts)}>#{child}</#{type}>"
  else
    "<#{type}>#{child}</#{type}>"
  end
end

#viewObject

A helper for injecting views into layouts. This method should be called at least once inside a layout. If you try calling it inside a view it will output a warning.



68
69
70
71
72
73
74
# File 'lib/pekky/context.rb', line 68

def view
  if @is_layout
    @view_output
  else
    tag('p', "You can't call #view within a view! This will break things. Tsk.")
  end
end