Class: Webby::Renderer

Inherits:
Object
  • Object
show all
Includes:
ERB::Util, Helpers::TagHelper, Helpers::UrlHelper
Defined in:
lib/webby/renderer.rb

Overview

The Webby::Renderer is used to filter and layout the text found in the resource page files in the content directory.

A page is filtered based on the settings of the ‘filter’ option in the page’s meta-data information. For example, if ‘textile’ is specified as a filter, then the page will be run through the RedCloth markup filter. More than one filter can be used on a page; they will be run in the order specified in the meta-data.

A page is rendered into a layout specified by the ‘layout’ option in the page’s meta-data information.

Defined Under Namespace

Classes: Error

Constant Summary

Constants included from Helpers::TagHelper

Helpers::TagHelper::BOOLEAN_ATTRIBUTES

Constants included from ERB::Util

ERB::Util::HTML_ESCAPE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::UrlHelper

#link_to, #link_to_page, #link_to_page_unless_current, #url_for, #url_for_page

Methods included from Helpers::TagHelper

#escape_once

Methods included from ERB::Util

#html_escape

Constructor Details

#initialize(page) ⇒ Renderer

call-seq:

Renderer.new( page )

Create a new renderer for the given page. The renderer will apply the desired filters to the page (from the page’s meta-data) and then render the filtered page into the desired layout.



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

def initialize( page )
  unless page.is_page?
    raise ArgumentError,
          "only page resources can be rendered '#{page.path}'"
  end

  @page = page
  @pages = Resource.pages
  @content = nil

  @log = Logging::Logger[self]
end

Class Method Details

.write(page) ⇒ Object

call-seq:

Renderer.write( page )

Render the given page and write the resulting output to the page’s destination. If the page uses pagination, then multiple destination files will be created – one for each paginated data set in the page.



37
38
39
40
41
42
43
44
45
46
# File 'lib/webby/renderer.rb', line 37

def self.write( page )
  renderer = self.new(page)

  loop {
    ::File.open(page.destination, 'w') do |fd|
      fd.write renderer.layout_page
    end
    break unless renderer.__send__(:next_page)
  }
end

Instance Method Details

#get_bindingObject

call-seq:

get_binding    => binding

Returns the current binding for the renderer.



130
131
132
# File 'lib/webby/renderer.rb', line 130

def get_binding
  binding
end

#layout_pageObject

call-seq:

layout_page    => string

Apply the desired filters to the page and then render the filtered page into the desired layout. The filters to apply to the page are determined from the page’s meta-data. The layout to use is also determined from the page’s meta-data.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/webby/renderer.rb', line 76

def layout_page
  layouts = Resource.layouts
  obj = @page
  str = @page.render(self)

  loop do
    lyt = layouts.find :filename => obj.layout
    break if lyt.nil?

    @content, str = str, ::Webby::File.read(lyt.path)
    str = Filters.process(self, lyt, str)
    @content, obj = nil, lyt
  end

  str
end

#paginate(items, count, &block) ⇒ Object

call-seq:

paginate( items, per_page ) {|item| block}

Iterate the given block for each item selected from the items array using the given number of items per_page. The first time the page is rendered, the items passed to the block are selected using the range (0…per_page). The next rendering selects (per_page…2*per_page). This continues until all items have been paginated.

Calling this method creates a @pager object that can be accessed from the page. The @pager contains information about the next page, the current page number, the previous page, and the number of items in the current page.



117
118
119
120
121
122
123
# File 'lib/webby/renderer.rb', line 117

def paginate( items, count, &block )
  @pager ||= Paginator.new(items.length, count, @page) do |offset, per_page|
    items[offset,per_page]
  end.first

  @pager.each &block
end

#render_pageObject

call-seq:

render_page    => string

Apply the desired filters to the page. The filters to apply are determined from the page’s meta-data.



99
100
101
# File 'lib/webby/renderer.rb', line 99

def render_page
  Filters.process(self, @page, ::Webby::File.read(@page.path))
end