Object
Represents a webgen website and is used to render it.
Normally, webgen is used from the command line via the webgen command or from Rakefiles via Webgen::WebgenTask. However, you can also easily use webgen as a library and this class provides the interface for this usage!
Since a webgen website is, basically, just a directory, the only parameter needed for creating a new Website object is the website directory. After that you can work with the website:
If you want to render the website, you just need to call Website#render which initializes the website and does all the rendering. When the method call returns, everything has been rendered.
If you want to remove the generated output, you just need to invoke Website#clean and it will be done.
Finally, if you want to retrieve data from the website, you first have to call Website#init to initialize the website. After that you can use the various accessors to retrieve the needed data. Note: This is generally only useful if the website has been rendered because otherwise there is no data to retrieve.
The blackboard used for inter-object communication. Can only be used after # has been called.
A cache to store information that should be available between runs. Can only be used after # has been called.
Create a new webgen website for the website in the directory dir. If dir is nil, the environment variable WEBGEN_WEBSITE or, if it is not set either, the current working directory is used. You can provide a block (has to take the configuration object as parameter) for adjusting the configuration values during the initialization.
# File lib/webgen/website.rb, line 232 232: def initialize(dir = nil, logger=Webgen::Logger.new($stdout, false), &block) 233: @blackboard = nil 234: @cache = nil 235: @config = nil 236: @logger = logger 237: @config_block = block 238: @directory = (dir.nil? ? (ENV['WEBGEN_WEBSITE'].to_s.empty? ? Dir.pwd : ENV['WEBGEN_WEBSITE']) : dir) 239: end
Define a service service_name provided by the instance of klass. The parameter method needs to define the method which should be invoked when the service is invoked. Can only be used after # has been called.
# File lib/webgen/website.rb, line 244 244: def autoload_service(service_name, klass, method = service_name) 245: blackboard.add_service(service_name) {|*args| cache.instance(klass).send(method, *args)} 246: end
Clean the website directory from all generated output files (including the cache file). If del_outdir is true, then the base output directory is also deleted. When a delete operation fails, the error is silently ignored and the clean operation continues.
Note: Uses the configured output instance for the operations!
# File lib/webgen/website.rb, line 292 292: def clean(del_outdir = false) 293: init 294: execute_in_env do 295: output = @blackboard.invoke(:output_instance) 296: @tree.node_access[:alcn].each do |name, node| 297: next if node.is_fragment? || node['no_output'] || node.path == '/' || node == @tree.dummy_root 298: output.delete(node.path) rescue nil 299: end 300: 301: if @config['website.cache'].first == :file 302: FileUtils.rm(File.join(@directory, @config['website.cache'].last)) rescue nil 303: end 304: 305: if del_outdir 306: output.delete('/') rescue nil 307: end 308: end 309: end
The provided block is executed within a proper environment sothat any object can access the Website object.
# File lib/webgen/website.rb, line 313 313: def execute_in_env 314: set_back = Thread.current[:webgen_website] 315: Thread.current[:webgen_website] = self 316: yield 317: ensure 318: Thread.current[:webgen_website] = set_back 319: end
Initialize the configuration, blackboard and cache objects and load the default configuration as well as website specific extension files. An already existing configuration/blackboard is deleted!
# File lib/webgen/website.rb, line 251 251: def init 252: execute_in_env do 253: @blackboard = Blackboard.new 254: @config = Configuration.new 255: 256: load 'webgen/default_config.rb' 257: Dir.glob(File.join(@directory, 'ext', '**/init.rb')) {|f| load(f)} 258: read_config_file 259: 260: @config_block.call(@config) if @config_block 261: restore_tree_and_cache 262: end 263: self 264: end
Render the website (after calling # if the website is not already initialized) and return a status code not equal to nil if rendering was successful.
# File lib/webgen/website.rb, line 268 268: def render 269: result = nil 270: execute_in_env do 271: init 272: 273: puts "Starting webgen..." 274: shm = SourceHandler::Main.new 275: result = shm.render 276: save_tree_and_cache if result 277: puts "Finished" 278: 279: if @logger && @logger.log_output.length > 0 280: puts "\nLog messages:" 281: puts @logger.log_output 282: end 283: end 284: result 285: end
Update the configuration object for the website with infos found in the configuration file.
# File lib/webgen/website.rb, line 354 354: def read_config_file 355: file = File.join(@directory, 'config.yaml') 356: if File.exists?(file) 357: begin 358: config = YAML::load(File.read(file)) || {} 359: raise 'Structure of config file is not valid, has to be a Hash' if !config.kind_of?(Hash) 360: config.each do |key, value| 361: case key 362: when *Webgen::Configuration::Helpers.public_instance_methods(false).map {|c| c.to_s} then @config.send(key, value) 363: else @config[key] = value 364: end 365: end 366: rescue RuntimeError, ArgumentError => e 367: raise ConfigFileInvalid, "Configuration invalid: " + e.message 368: end 369: elsif File.exists?(File.join(@directory, 'config.yml')) 370: log(:warn) { "No configuration file called config.yaml found (there is a config.yml - spelling error?)" } 371: end 372: end
Restore the tree and the cache from website.cache and returns the Tree object.
# File lib/webgen/website.rb, line 326 326: def restore_tree_and_cache 327: @cache = Cache.new 328: @tree = Tree.new 329: data = if config['website.cache'].first == :file 330: cache_file = File.join(@directory, config['website.cache'].last) 331: File.open(cache_file, 'rb') {|f| f.read} if File.exists?(cache_file) 332: else 333: config['website.cache'].last 334: end 335: cache_data, tree, version = Marshal.load(data) rescue nil 336: if cache_data && version == Webgen::VERSION 337: @cache.restore(cache_data) 338: @tree = tree 339: end 340: end
Save the tree and the cache to website.cache.
# File lib/webgen/website.rb, line 343 343: def save_tree_and_cache 344: cache_data = [@cache.dump, @tree, Webgen::VERSION] 345: if config['website.cache'].first == :file 346: cache_file = File.join(@directory, config['website.cache'].last) 347: File.open(cache_file, 'wb') {|f| Marshal.dump(cache_data, f)} 348: else 349: config['website.cache'][1] = Marshal.dump(cache_data) 350: end 351: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.