Class: Webby::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/webby/resource.rb

Overview

A Webby::Resource is any file that can be found in the content directory or in the layout directory. This class contains information about the resources available to Webby. This information includes the resource type (static, page, layout), if the resource is dirty (it needs to be rendered), the output location of the rendered resource, etc.

A resource is a “layout” if the resource is found in the layout directory. Static and page resources are found in the content directory.

A resource is considered static only if it *does not* contain a YAML meta-data header at the top of the file. These resources will be copied as-is from the content directory to the output directory.

If a resouce does have meta-data, then it will be processed (i.e. rendered/filtered) by Webby, and the rendered results will be written to the output directory.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn) ⇒ Resource

call-seq:

Resource.new( filename )    => resource

Creates a new resource object given the filename.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/webby/resource.rb', line 70

def initialize( fn )
  @path     = fn.sub(%r/\A(?:\.\/|\/)/o, '').freeze
  @dir      = Webby::File.dirname(@path)
  @filename = Webby::File.basename(@path)
  @ext      = Webby::File.extname(@path)
  @mtime    = ::File.mtime @path

  @number = nil
  @rendering = false

  # deal with the meta-data
  @mdata = ::Webby::File.(@path)
  @have_mdata = !@mdata.nil?

  @mdata ||= {}
  @mdata = ::Webby.site.page_defaults.merge(@mdata) if is_page?
  @mdata.sanitize!

  self.class.pages << self if is_page? or is_static?
  self.class.layouts << self if is_layout?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *a, &b) ⇒ Object

call-seq:

method_missing( symbol [, *args, &block] )    => result

Invoked by Ruby when a message is sent to the resource that it cannot handle. The default behavior is to convert symbol to a string and search for that string in the resource’s meta-data. If found, the meta-data item is returned; otherwise, nil is returned.



286
287
288
# File 'lib/webby/resource.rb', line 286

def method_missing( name, *a, &b )
  @mdata[name.to_s]
end

Instance Attribute Details

#dirObject (readonly)

The directory of the resource excluding the content directory



51
52
53
# File 'lib/webby/resource.rb', line 51

def dir
  @dir
end

#extObject (readonly)

Extesion of the resource file



57
58
59
# File 'lib/webby/resource.rb', line 57

def ext
  @ext
end

#filenameObject (readonly)

The resource filename excluding path and extension



54
55
56
# File 'lib/webby/resource.rb', line 54

def filename
  @filename
end

#mtimeObject (readonly)

Resource file modification time



60
61
62
# File 'lib/webby/resource.rb', line 60

def mtime
  @mtime
end

#numberObject

Resource page number (if needed)



63
64
65
# File 'lib/webby/resource.rb', line 63

def number
  @number
end

#pathObject (readonly)

The full path to the resource file



48
49
50
# File 'lib/webby/resource.rb', line 48

def path
  @path
end

Class Method Details

.clearObject

Clear the contents of the layouts and the pages hash objects.



41
42
43
44
# File 'lib/webby/resource.rb', line 41

def clear
  self.pages.clear
  self.layouts.clear
end

.layoutsObject

Returns the layouts hash object.



36
37
38
# File 'lib/webby/resource.rb', line 36

def layouts
  @layouts ||= PagesDB.new
end

.pagesObject

Returns the pages hash object.



31
32
33
# File 'lib/webby/resource.rb', line 31

def pages
  @pages ||= PagesDB.new
end

Instance Method Details

#<=>(other) ⇒ Object

call-seq:

resource <=> other    => -1, 0, +1, or nil

Resource comparison operates on the full path of the resource objects and uses the standard String comparison operator. Returns nil if other is not a Resource instance.



112
113
114
115
# File 'lib/webby/resource.rb', line 112

def <=>( other )
  return unless self.class == other.class
  @path <=> other.path
end

#destinationObject

call-seq:

destination    => string

Returns the path in the output directory where the results of rendering this resource should be stored. This path is used to determine if the resource is dirty and in need of rendering.

The destination for any resource can be overridden by explicitly setting the ‘destination’ property in the resource’s meta-data.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/webby/resource.rb', line 149

def destination
  return @dest if defined? @dest and @dest
  return @dest = ::Webby.cairn if is_layout?

  @dest = if @mdata.has_key? 'destination' then @mdata['destination']
          else ::File.join(dir, filename) end

  @dest = ::File.join(::Webby.site.output_dir, @dest)
  @dest << @number.to_s if @number

  ext = extension
  unless ext.nil? or ext.empty?
    @dest << '.'
    @dest << ext
  end
  @dest
end

#dirty?Boolean

call-seq:

dirty?    => true or false

Returns true if this resource is newer than its corresponding output product. The resource needs to be rendered (if a page or layout) or copied (if a static file) to the output directory.

Returns:

  • (Boolean)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/webby/resource.rb', line 254

def dirty?
  return @mdata['dirty'] if @mdata.has_key? 'dirty'

  # if the destination file does not exist, then we are dirty
  return true unless test ?e, destination

  # if this file's mtime is larger than the destination file's
  # mtime, then we are dirty
  dirty = @mtime > ::File.mtime(destination)
  return dirty if is_static? or dirty

  # check to see if the layout is dirty, and it it is then we
  # are dirty, too
  if @mdata.has_key? 'layout'
    lyt = self.class.layouts.find :filename => @mdata['layout']
    unless lyt.nil?
      return true if lyt.dirty?
    end
  end

  # if we got here, then we are not dirty
  false
end

#equal?(other) ⇒ Boolean Also known as: ==, eql?

call-seq:

equal?( other )    => true or false

Returns true if the path of this resource is equivalent to the path of the other resource. Returns false if this is not the case.

Returns:

  • (Boolean)


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

def equal?( other )
  return false unless self.class == other.class
  @path == other.path
end

#extensionObject

call-seq:

extension    => string

Returns the extension that will be appended to the output destination filename. The extension is determined by looking at the following:

  • this resource’s meta-data for an ‘extension’ property

  • the meta-data of this resource’s layout for an ‘extension’ property

  • the extension of this resource file



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/webby/resource.rb', line 127

def extension
  return @mdata['extension'] if @mdata.has_key? 'extension'

  if @mdata.has_key? 'layout'
    lyt = self.class.layouts.find :filename => @mdata['layout']
    ext = lyt ? lyt.extension : nil
    return ext if ext
  end

  return (is_layout? ? nil : @ext)
end

#is_layout?Boolean

call-seq:

is_layout?    => true or false

Returns true if this resource is a layout.

Returns:

  • (Boolean)


224
225
226
227
# File 'lib/webby/resource.rb', line 224

def is_layout?
  @is_layout ||=
      !(%r/\A(?:\.\/|\/)?#{::Webby.site.layout_dir}\//o =~ @path).nil?
end

#is_page?Boolean

call-seq:

is_page?    => true or false

Returns true if this resource is a page suitable for rendering.

Returns:

  • (Boolean)


243
244
245
# File 'lib/webby/resource.rb', line 243

def is_page?
  @have_mdata and !is_layout?
end

#is_static?Boolean

call-seq:

is_static?    => true or false

Returns true if this resource is a static file.

Returns:

  • (Boolean)


234
235
236
# File 'lib/webby/resource.rb', line 234

def is_static?
  !@have_mdata
end

#render(renderer = nil) ⇒ Object

call-seq:

render   => string

Creates a new Webby::Renderer instance and uses that instance to render the resource contents using the configured filter(s). The filter(s) to use is defined in the resource’s meta-data as the ‘filter’ key.

Note, this only renders this resource. The returned string does not include any layout rendering.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/webby/resource.rb', line 204

def render( renderer = nil )
  raise Error, "page '#@path' is in a rendering loop" if @rendering

  @rendering = true
  renderer ||= Renderer.new(self)
  content = renderer.render_page
  @rendering = false

  return content

rescue
  @rendering = false
  raise
end

#urlObject

call-seq

url    => string or nil

Returns a string suitable for use as a URL linking to this page. Nil is returned for layouts.



173
174
175
176
177
178
179
180
# File 'lib/webby/resource.rb', line 173

def url
  return nil if is_layout?
  return @url if defined? @url and @url

  @url = destination.sub(::Webby.site.output_dir, '')
  @url = File.dirname(@url) if filename == 'index'
  @url
end