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 init has been called.
A cache to store information that should be available between runs. Can only be used after init 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 231 def initialize(dir = nil, logger=Webgen::Logger.new($stdout, false), &block) @blackboard = nil @cache = nil @config = nil @logger = logger @config_block = block @directory = (dir.nil? ? (ENV['WEBGEN_WEBSITE'].to_s.empty? ? Dir.pwd : ENV['WEBGEN_WEBSITE']) : dir) 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 init has been called.
# File lib/webgen/website.rb, line 243 def autoload_service(service_name, klass, method = service_name) blackboard.add_service(service_name) {|*args| cache.instance(klass).send(method, *args)} 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 291 def clean(del_outdir = false) init execute_in_env do output = @blackboard.invoke(:output_instance) @tree.node_access[:alcn].each do |name, node| next if node.is_fragment? || node['no_output'] || node.path == '/' || node == @tree.dummy_root output.delete(node.path) rescue nil end if @config['website.cache'].first == :file FileUtils.rm(File.join(@directory, @config['website.cache'].last)) rescue nil end if del_outdir output.delete('/') rescue nil end end end
The provided block is executed within a proper environment sothat any object can access the Website object.
# File lib/webgen/website.rb, line 312 def execute_in_env set_back = Thread.current[:webgen_website] Thread.current[:webgen_website] = self yield ensure Thread.current[:webgen_website] = set_back 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 250 def init execute_in_env do @blackboard = Blackboard.new @config = Configuration.new load 'webgen/default_config.rb' Dir.glob(File.join(@directory, 'ext', '**/init.rb')) {|f| load(f)} read_config_file @config_block.call(@config) if @config_block restore_tree_and_cache end self end
Render the website (after calling init 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 267 def render result = nil execute_in_env do init puts "Starting webgen..." shm = SourceHandler::Main.new result = shm.render save_tree_and_cache if result puts "Finished" if @logger && @logger.log_output.length > 0 puts "\nLog messages:" puts @logger.log_output end end result end
Update the configuration object for the website with infos found in the configuration file.
# File lib/webgen/website.rb, line 353 def read_config_file file = File.join(@directory, 'config.yaml') if File.exists?(file) begin config = YAML::load(File.read(file)) || {} raise 'Structure of config file is not valid, has to be a Hash' if !config.kind_of?(Hash) config.each do |key, value| case key when *Webgen::Configuration::Helpers.public_instance_methods(false).map {|c| c.to_s} then @config.send(key, value) else @config[key] = value end end rescue RuntimeError, ArgumentError => e raise ConfigFileInvalid, "Configuration invalid: " + e.message end elsif File.exists?(File.join(@directory, 'config.yml')) log(:warn) { "No configuration file called config.yaml found (there is a config.yml - spelling error?)" } end end
Restore the tree and the cache from website.cache and returns the Tree object.
# File lib/webgen/website.rb, line 325 def restore_tree_and_cache @cache = Cache.new @tree = Tree.new data = if config['website.cache'].first == :file cache_file = File.join(@directory, config['website.cache'].last) File.open(cache_file, 'rb') {|f| f.read} if File.exists?(cache_file) else config['website.cache'].last end cache_data, tree, version = Marshal.load(data) rescue nil if cache_data && version == Webgen::VERSION @cache.restore(cache_data) @tree = tree end end
Save the tree and the cache to website.cache.
# File lib/webgen/website.rb, line 342 def save_tree_and_cache cache_data = [@cache.dump, @tree, Webgen::VERSION] if config['website.cache'].first == :file cache_file = File.join(@directory, config['website.cache'].last) File.open(cache_file, 'wb') {|f| Marshal.dump(cache_data, f)} else config['website.cache'][1] = Marshal.dump(cache_data) end end
Generated with the Darkfish Rdoc Generator 2.