Class: Webby::Renderer

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
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 filer. 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.

Instance Method Summary collapse

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.



32
33
34
35
36
37
38
39
40
41
# File 'lib/webby/renderer.rb', line 32

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
end

Instance Method Details

#erb_filter(str) ⇒ Object

Render text via ERB using the built in ERB library.



90
91
92
93
# File 'lib/webby/renderer.rb', line 90

def erb_filter( str )
  b = binding
  ERB.new(str, nil, '-').result(b)
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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/webby/renderer.rb', line 51

def layout_page
  layouts = Resource.layouts
  obj = @page
  str = @page.render

  loop do
    lyt = layouts.find_by_name obj.layout
    break if lyt.nil?

    @content, str = str, ::Webby::File.read(lyt.path)

    lyt.filter.to_a.each do |filter|
      str = self.send(filter + '_filter', str)
    end

    @content, obj = nil, lyt
  end

  str
end

#markdown_filter(str) ⇒ Object

Render text via markdown using the BlueCloth library.



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

def markdown_filter( str )
  BlueCloth.new(str).to_html
rescue NameError
  $stderr.puts 'ERROR: markdown filter failed (BlueCloth not installed?)'
  exit
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.



78
79
80
81
82
83
84
85
86
# File 'lib/webby/renderer.rb', line 78

def render_page
  str = ::Webby::File.read(@page.path)

  @page.filter.to_a.each do |filter|
    str = self.send(filter + '_filter', str)
  end

  str
end

#textile_filter(str) ⇒ Object

Render text via textile using the RedCloth library.



106
107
108
109
110
111
# File 'lib/webby/renderer.rb', line 106

def textile_filter( str )
  RedCloth.new(str).to_html
rescue NameError
  $stderr.puts 'ERROR: textile filter failed (RedCloth not installed?)'
  exit
end