Class: Pekky::Page
- Inherits:
-
Object
- Object
- Pekky::Page
- 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
-
#children ⇒ Object
readonly
Various attribute accessors for storing all the values we reuse across the different methods.
-
#content ⇒ Object
readonly
Various attribute accessors for storing all the values we reuse across the different methods.
-
#directory ⇒ Object
readonly
Various attribute accessors for storing all the values we reuse across the different methods.
-
#file_name ⇒ Object
readonly
Various attribute accessors for storing all the values we reuse across the different methods.
-
#name ⇒ Object
readonly
Various attribute accessors for storing all the values we reuse across the different methods.
-
#opts ⇒ Object
readonly
Various attribute accessors for storing all the values we reuse across the different methods.
-
#parent ⇒ Object
This accessor will be used to store a reference to the parent page – if there is one.
-
#path ⇒ Object
readonly
Various attribute accessors for storing all the values we reuse across the different methods.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares one page instance with another.
-
#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.
-
#hidden? ⇒ Boolean
Checks to see if the page is hidden i.e.
-
#initialize(name, path, content, opts) ⇒ Page
constructor
The Page class is initialized with the path, the content – either a Content instance or a hash – and a hash of options.
-
#render ⇒ Object
The business part of the page.
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
#children ⇒ Object (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 |
#content ⇒ Object (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 |
#directory ⇒ Object (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_name ⇒ Object (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 |
#name ⇒ Object (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 |
#opts ⇒ Object (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 |
#parent ⇒ Object
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 |
#path ⇒ Object (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 ;)
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
51 52 53 |
# File 'lib/pekky/pages.rb', line 51 def hidden? !!opts[:hidden] end |
#render ⇒ Object
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 |