Class: Webgen::Website

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/webgen/website.rb

Overview

Represents a webgen website and is used to render it.

Defined Under Namespace

Classes: ConfigFileInvalid

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log, #puts

Constructor Details

#initialize(dir, logger = Webgen::Logger.new($stdout, false), &block) ⇒ Website

Create a new webgen website for the website in the directory dir. You can provide a block (has to take the configuration object as parameter) for adjusting the configuration values during the initialization.



175
176
177
178
179
180
181
182
# File 'lib/webgen/website.rb', line 175

def initialize(dir, logger=Webgen::Logger.new($stdout, false), &block)
  @blackboard = nil
  @cache = nil
  @config = nil
  @logger = logger
  @config_block = block
  @directory = dir
end

Instance Attribute Details

#blackboardObject (readonly)

The blackboard used for inter-object communication. Can only be used after #init has been called.



157
158
159
# File 'lib/webgen/website.rb', line 157

def blackboard
  @blackboard
end

#cacheObject (readonly)

A cache to store information that should be available between runs. Can only be used after #init has been called.



161
162
163
# File 'lib/webgen/website.rb', line 161

def cache
  @cache
end

#configObject (readonly)

The website configuration. Can only be used after #init has been called (which is automatically done in #render).



153
154
155
# File 'lib/webgen/website.rb', line 153

def config
  @config
end

#directoryObject (readonly)

The website directory.



170
171
172
# File 'lib/webgen/website.rb', line 170

def directory
  @directory
end

#loggerObject

The logger used for logging. If set to nil, logging is disabled.



167
168
169
# File 'lib/webgen/website.rb', line 167

def logger
  @logger
end

#treeObject (readonly)

The internal data structure used to store information about individual nodes.



164
165
166
# File 'lib/webgen/website.rb', line 164

def tree
  @tree
end

Instance Method Details

#autoload_service(service_name, klass, method = service_name) ⇒ Object

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.



187
188
189
# File 'lib/webgen/website.rb', line 187

def autoload_service(service_name, klass, method = service_name)
  blackboard.add_service(service_name) {|*args| cache.instance(klass).send(method, *args)}
end

#clean(del_outdir = false) ⇒ Object

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!



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/webgen/website.rb', line 235

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

#execute_in_envObject

The provided block is executed within a proper environment sothat any object can access the Website object.



256
257
258
259
260
261
262
# File 'lib/webgen/website.rb', line 256

def execute_in_env
  set_back = Thread.current[:webgen_website].nil?
  Thread.current[:webgen_website] = self
  yield
ensure
  Thread.current[:webgen_website] = nil if set_back
end

#initObject

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!



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/webgen/website.rb', line 194

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

#renderObject

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.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/webgen/website.rb', line 211

def render
  result = nil
  execute_in_env do
    init unless @config

    puts "Starting webgen..."
    shm = SourceHandler::Main.new
    result = shm.render(@tree)
    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