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 = nil, logger = Webgen::Logger.new($stdout, false), &block) ⇒ Website

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.



206
207
208
209
210
211
212
213
# File 'lib/webgen/website.rb', line 206

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

Instance Attribute Details

#blackboardObject (readonly)

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



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

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.



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

def cache
  @cache
end

#configObject (readonly)

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



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

def config
  @config
end

#directoryObject (readonly)

The website directory.



200
201
202
# File 'lib/webgen/website.rb', line 200

def directory
  @directory
end

#loggerObject

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



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

def logger
  @logger
end

#treeObject (readonly)

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



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

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.



218
219
220
# File 'lib/webgen/website.rb', line 218

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!



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/webgen/website.rb', line 266

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.



287
288
289
290
291
292
293
# File 'lib/webgen/website.rb', line 287

def execute_in_env
  set_back = Thread.current[:webgen_website]
  Thread.current[:webgen_website] = self
  yield
ensure
  Thread.current[:webgen_website] = 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!



225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/webgen/website.rb', line 225

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.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/webgen/website.rb', line 242

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