Class: JADOF::Page

Inherits:
Object
  • Object
show all
Extended by:
PageAPI
Includes:
IndifferentVariableHash
Defined in:
lib/jadof/page.rb

Overview

A Page wraps a file on the filesystem.

If you set ‘Page.dir` to a directory, `Page.all` will give you all of the files inside that directory as Page objects.

For all of the available class methods, see PageAPI.

Direct Known Subclasses

Post

Constant Summary collapse

DEFAULT_DIR =
'./pages/'
DEFAULT_FORMATTERS =
{
  'markdown' => lambda { |text| require 'maruku';   Maruku.new(text).to_html      },
  'erb'      => lambda { |text| require 'erb';      ERB.new(text).result          },
  'haml'     => lambda { |text| require 'haml';     Haml::Engine.new(text).render },
  'textile'  => lambda { |text| require 'redcloth'; RedCloth.new(text).to_html    }
}

Instance Attribute Summary collapse

Attributes included from PageAPI

#cache, #dir, #formatters

Instance Method Summary collapse

Methods included from PageAPI

[], all, cache_for, count, first, from_path, get, inherited, last, matches_conditions?, where

Constructor Details

#initialize(options = nil) ⇒ Page

A page is a dumb object and doesn’t know how to load itself from a file on the filesystem. See ‘Page.from_path` to load a JADOF::Page from a file.

Parameters:

  • of (Hash)

    attributes



229
230
231
# File 'lib/jadof/page.rb', line 229

def initialize options = nil
  options.each {|attribute, value| send "#{attribute}=", value } if options
end

Instance Attribute Details

#bodyString

the top of the file (if YAML was included).

We strip out the YAML and use it to set variables on the Page. If you need the full text from the file, you should ‘File.read(@page.path)`.

Returns:

  • (String)

    The body of the file, without the YAML at



211
212
213
# File 'lib/jadof/page.rb', line 211

def body
  @body
end

#filenameString

Returns The filename (without a directory), eg. ‘foo.markdown`.

Returns:

  • (String)

    The filename (without a directory), eg. ‘foo.markdown`



203
204
205
# File 'lib/jadof/page.rb', line 203

def filename
  @filename
end

#nameString

If the filename is ‘foo.markdown`, the name will be `foo`

If the file is in a subdirectory below ‘Page.dir`, eg. `foo/bar.markdown`, the name will be `bar` but the #full_name will be `foo/bar`.

Returns:



197
198
199
# File 'lib/jadof/page.rb', line 197

def name
  @name
end

#parentString

in a subdirectory below ‘Page.dir`. This will be `“”` if it is in `Page.dir` or it will be `“sub/directories”` if it is in subdirectories.

Returns:

  • (String)

    The parent directory of this file, if this file is



217
218
219
# File 'lib/jadof/page.rb', line 217

def parent
  @parent
end

#pathString

Returns The full system path to this file.

Returns:

  • (String)

    The full system path to this file



200
201
202
# File 'lib/jadof/page.rb', line 200

def path
  @path
end

Instance Method Details

#==(other_page) ⇒ true, false

Returns If 2 pages have the same system path, they’re the same.

Returns:

  • (true, false)

    If 2 pages have the same system path, they’re the same.



253
254
255
256
# File 'lib/jadof/page.rb', line 253

def == other_page
  return false unless other_page.is_a? Page
  return other_page.path == path
end

#extensionsArray(String)

Returns This file’s extension(s).

Returns:

  • (Array(String))

    This file’s extension(s).



220
221
222
# File 'lib/jadof/page.rb', line 220

def extensions
  filename.scan(/\.([^\.]+)/).flatten
end

#full_nameString

Returns combines #name with #parent.

Returns:



234
235
236
# File 'lib/jadof/page.rb', line 234

def full_name
  parent == '' ? name : File.join(parent, name)
end

#renderObject Also known as: to_html

Returns the formatted #body of this JADOF::Page using ‘Page.formatters`.

The file extensions from the #filename are used to match formatters.

For ‘foo.markdown`, `Page.formatters` will be used. For `foo.markdown.erb`, `Page.formatters` and `Page.formatters` will be used.

With multiple extensions, the last extension is used first, and then the second-to-last, etc.



246
247
248
# File 'lib/jadof/page.rb', line 246

def render
  self.class.render self
end

#to_paramString

Returns A param representation of this page for us in web applications. Defaults to #full_name.

Returns:

  • (String)

    A param representation of this page for us in web applications. Defaults to #full_name.



264
265
266
# File 'lib/jadof/page.rb', line 264

def to_param
  full_name
end

#to_sString

Returns This page as a string. Defaults to #full_name.

Returns:

  • (String)

    This page as a string. Defaults to #full_name.



259
260
261
# File 'lib/jadof/page.rb', line 259

def to_s
  full_name
end