| Module | Webgen::Output |
| In: |
lib/webgen/output.rb
lib/webgen/output/filesystem.rb |
Namespace for all classes that know how to write out node content.
Output classes know how to write rendered node data to an output location.
An output class must respond to three methods
It seems a bit odd that an output instance has to implement reading functionality. However, consider the case where you want webgen to render a website programmatically and use the output. In this case you need a way to get to content of the written files! This functionality is used, for example, in the webgui.
Following is a simple but actually used (by the webgui) output class which stores the written nodes in a hash in memory:
class MemoryOutput
include Webgen::WebsiteAccess
attr_reader :data
def initialize
@data = {}
end
def exists?(path)
@data.has_key?(path)
end
def delete(path)
@data.delete(path)
end
def write(path, io, type = :file)
@data[path] = [(io.kind_of?(String) ? io : io.data), type]
end
def read(path, mode = 'rb')
path = File.join('/', path)
raise "No such file #{path}" unless @data[path] && @data[path].last == :file
@data[path].first
end
end
WebsiteAccess.website.config.output(['MemoryOutput'])
The last line is used to tell webgen to use this new output class instead of the default one.
Returns an instance of the configured output class.
# File lib/webgen/output.rb, line 75
75: def self.instance
76: classes = (WebsiteAccess.website.cache.volatile[:classes] ||= {})
77: unless classes.has_key?(:output_instance)
78: klass, *args = WebsiteAccess.website.config['output']
79: classes[:output_instance] = Common.const_for_name(klass).new(*args)
80: end
81: classes[:output_instance]
82: end