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.

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.

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.



201
202
203
204
205
206
207
208
# File 'lib/webgen/website.rb', line 201

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.



183
184
185
# File 'lib/webgen/website.rb', line 183

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.



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

def cache
  @cache
end

#configObject (readonly)

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



179
180
181
# File 'lib/webgen/website.rb', line 179

def config
  @config
end

#directoryObject (readonly)

The website directory.



196
197
198
# File 'lib/webgen/website.rb', line 196

def directory
  @directory
end

#loggerObject

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



193
194
195
# File 'lib/webgen/website.rb', line 193

def logger
  @logger
end

#treeObject (readonly)

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



190
191
192
# File 'lib/webgen/website.rb', line 190

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.



213
214
215
# File 'lib/webgen/website.rb', line 213

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!



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/webgen/website.rb', line 261

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.



282
283
284
285
286
287
288
# File 'lib/webgen/website.rb', line 282

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!



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/webgen/website.rb', line 220

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.



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

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