class Webgen::Website
About¶ ↑
Represents a webgen website and provides the main interface for users.
Normally, webgen is used from the command line via the webgen
command or from Rakefiles via Webgen::RakeTask
. However, you can also easily use webgen as a library and this class provides the interface for this usage!
You may notice that this class doesn't have many methods. This is because webgen is designed from ground up to be extensible. Most of the 'magic' happens in extensions which are registered on the ext
OpenStruct object. The simple 'core' classes that are not extensions have separate accessor methods (config
for the Configuration
object, blackboard
for the Blackboard
and so on).
Since a webgen website is, basically, just a directory, the only parameter needed for creating a new Website
object is the website directory. Once created, the website is fully initialized and one can work with it:
-
If you want to generate the website, you just need to call
execute_task
with :generate_website as parameter. -
If you want to retrieve data from the website, you can use the various accessors on the
Website
object itself or useext
to access all available extensions.Note: This is generally only useful if the website has been generated before because otherwise there probably is no data to retrieve.
Constants
- CONFIG_FILENAME
The name of the configuration file webgen uses.
Attributes
The blackboard used for inter-object communication.
A cache to store information that should be available the next time the website gets generated.
The website configuration.
The website directory.
Access to all extension objects. An OpenStruct object.
The Webgen::Logger
used for logging.
The internal data structure used to store information about individual nodes.
See Tree
for more information
Public Class Methods
Create a new webgen Website
object for the website in the directory dir
.
If no logger is specified, a dummy logger that logs to a StringIO is created.
You can provide a block for modifying the Website
object in any way during the initialization:
-
If the block only takes one parameter, it is called with the
Website
object after the initialization is done but before the cache is restored. -
If it takes two parameters, the first one is the
Website
object and the second one is a boolean specifying whether the block is currently called any initialization (value istrue
) or after it (value is +false).
# File lib/webgen/website.rb 95 def initialize(dir, logger = nil, &block) 96 @directory = dir 97 @logger = logger || Webgen::Logger.new(StringIO.new) 98 @init_block = block 99 init 100 end
Public Instance Methods
Execute the given task.
See Webgen::Task
and the classes in its namespace for available classes.
# File lib/webgen/website.rb 208 def execute_task(task, *options) 209 @ext.task.execute(task, *options) 210 end
Save the cache
.
# File lib/webgen/website.rb 180 def save_cache 181 return if config['website.dry_run'] 182 cache_data = [@cache.dump, Webgen::VERSION] 183 if config['website.cache'].first == 'file' 184 File.open(cache_file(true), 'wb') {|f| Marshal.dump(cache_data, f)} 185 else 186 config['website.cache'][1] = Marshal.dump(cache_data) 187 end 188 end
Append the path to the website's temporary directory and return the full path to it.
Note that the temporary directory is only created if the create
parameter is set to true.
# File lib/webgen/website.rb 199 def tmpdir(path = '', create = false) 200 @_tmpdir = File.absolute_path(config['website.tmpdir'], @directory) unless defined?(@_tmpdir) 201 FileUtils.mkdir_p(@_tmpdir) if create 202 File.join(@_tmpdir, path) 203 end
Private Instance Methods
The full path of the cache filename.
# File lib/webgen/website.rb 191 def cache_file(create_dir = false) 192 tmpdir(config['website.cache'].last, create_dir) 193 end
Initialize the configuration, blackboard and cache objects and load the default configuration as well as all specified extensions.
# File lib/webgen/website.rb 104 def init 105 @tree = Tree.new(self) 106 @blackboard = Blackboard.new 107 @config = Configuration.new 108 @cache = nil 109 @ext = OpenStruct.new 110 111 @init_block.call(self, true) if @init_block && @init_block.arity == 2 112 loader = load_bundles 113 load_configuration(loader) 114 if @init_block 115 @init_block.arity == 1 ? @init_block.call(self) : @init_block.call(self, false) 116 end 117 @config.freeze 118 119 restore_cache 120 @blackboard.dispatch_msg(:website_initialized) 121 end
Load all extension bundles.
This loads the extension bundle for the built-in extensions as well as all website specific extension bundles.
# File lib/webgen/website.rb 128 def load_bundles 129 ext_dir = File.join(@directory, 'ext') 130 ext_loader = BundleLoader.new(self, ext_dir) 131 ext_loader.load('built-in') 132 ext_loader.load_autoload_bundles 133 Dir[File.join(ext_dir, '**/init.rb')].sort.each {|file| ext_loader.load(file[ext_dir.length..-1])} 134 ext_loader.load('init.rb') if File.file?(File.join(ext_dir, 'init.rb')) 135 ext_loader 136 end
Load the configuration file into the Configuration
object.
If it is a Ruby configuration file, the given bundle loader is used to load it.
# File lib/webgen/website.rb 145 def load_configuration(bundle_loader) 146 config_file = File.join(@directory, CONFIG_FILENAME) 147 return unless File.exist?(config_file) 148 149 first_line = File.open(config_file, 'r') {|f| f.gets} 150 if first_line =~ /^\s*#.*\bruby\b/i 151 begin 152 bundle_loader.load!(config_file) 153 rescue Exception => e 154 raise Webgen::Error.new("Couldn't load webgen configuration file (using Ruby syntax):\n#{e.message}") 155 end 156 else 157 unknown_options = @config.load_from_file(config_file) 158 @logger.vinfo { "Configuration data loaded from <#{config_file}>" } 159 if unknown_options.length > 0 160 @logger.debug { "Ignored following unknown options in configuration file: #{unknown_options.join(', ')}" } 161 end 162 end 163 end
Restore the cache using the website.cache
configuration option.
# File lib/webgen/website.rb 167 def restore_cache 168 @cache = Cache.new 169 data = if config['website.cache'].first == 'file' 170 File.binread(cache_file) if File.file?(cache_file) 171 else 172 config['website.cache'].last 173 end 174 cache_data, version = Marshal.load(data) rescue nil 175 @cache.restore(cache_data) if cache_data && version == Webgen::VERSION 176 end