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.



76
77
78
79
80
81
82
83
# File 'lib/webgen/website.rb', line 76

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.



58
59
60
# File 'lib/webgen/website.rb', line 58

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.



62
63
64
# File 'lib/webgen/website.rb', line 62

def cache
  @cache
end

#configObject (readonly)

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



54
55
56
# File 'lib/webgen/website.rb', line 54

def config
  @config
end

#directoryObject (readonly)

The website directory.



71
72
73
# File 'lib/webgen/website.rb', line 71

def directory
  @directory
end

#loggerObject

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



68
69
70
# File 'lib/webgen/website.rb', line 68

def logger
  @logger
end

#treeObject (readonly)

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



65
66
67
# File 'lib/webgen/website.rb', line 65

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.



88
89
90
# File 'lib/webgen/website.rb', line 88

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!



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/webgen/website.rb', line 133

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.



154
155
156
157
158
159
160
# File 'lib/webgen/website.rb', line 154

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!



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/webgen/website.rb', line 95

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).



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/webgen/website.rb', line 111

def render
  execute_in_env do
    init unless @config

    puts "Starting webgen..."
    shm = SourceHandler::Main.new
    shm.render(@tree)
    save_tree_and_cache
    puts "Finished"

    if @logger && @logger.log_output.length > 0
      puts "\nLog messages:"
      puts @logger.log_output
    end
  end
end