Class Webgen::Website
In: lib/webgen/website.rb
Parent: Object
Error RenderError CommandNotFoundError LoadError NodeCreationError ::Rake::TaskLib WebgenTask Helpers Configuration Node Context\n[lib/webgen/context.rb\nlib/webgen/context/nodes.rb\nlib/webgen/context/render.rb\nlib/webgen/context/tags.rb] Tree FileSystem Sitemap Copy Feed Virtual Sitemap Directory Page Fragment Template Metainfo Memory Coderay Sitemap IncludeFile BreadcrumbTrail Langbar TikZ Menu Tags Fragments Resource Website Tidy Head Kramdown Less Xmllint Blocks Comparable Language Path StandardError CmdParse::CommandParser CommandParser CmdParse::Command RunCommand WebguiCommand CreateCommand ApplyCommand WebsiteAccess Main Loggable OutputPathHelpers ExecuteCommand Link Date Relocatable Metainfo ::Kramdown::Converter::Html KramdownHtmlConverter Cache Blackboard WebsiteManager Logger Page ProxyNode Utils Scss RDoc Sass Erb RDiscount Erubis Haml Maruku Builder RedCloth AccessHash TarArchive Stacked FileSystem lib/webgen/cache.rb lib/webgen/error.rb lib/webgen/languages.rb lib/webgen/context/render.rb lib/webgen/website.rb lib/webgen/blackboard.rb lib/webgen/tree.rb lib/webgen/websitemanager.rb lib/webgen/logger.rb lib/webgen/configuration.rb lib/webgen/path.rb lib/webgen/webgentask.rb lib/webgen/page.rb lib/webgen/node.rb ClassMethods WebsiteAccess lib/webgen/cli/run_command.rb lib/webgen/cli/utils.rb lib/webgen/cli/apply_command.rb lib/webgen/cli/webgui_command.rb lib/webgen/cli.rb lib/webgen/cli/create_command.rb Color CLI LanguageManager lib/webgen/output/filesystem.rb Output lib/webgen/common/sitemap.rb Common lib/webgen/sourcehandler/memory.rb lib/webgen/sourcehandler/metainfo.rb lib/webgen/sourcehandler/copy.rb lib/webgen/sourcehandler/directory.rb lib/webgen/sourcehandler.rb lib/webgen/sourcehandler/page.rb lib/webgen/sourcehandler/template.rb lib/webgen/sourcehandler/fragment.rb lib/webgen/sourcehandler/sitemap.rb lib/webgen/sourcehandler/virtual.rb lib/webgen/sourcehandler/feed.rb OutputPathHelpers Base SourceHandler lib/webgen/tag/coderay.rb lib/webgen/tag/relocatable.rb lib/webgen/tag/menu.rb lib/webgen/tag/langbar.rb lib/webgen/tag/executecommand.rb lib/webgen/tag/breadcrumbtrail.rb lib/webgen/tag/metainfo.rb lib/webgen/tag/includefile.rb lib/webgen/tag/link.rb lib/webgen/tag/date.rb lib/webgen/tag/tikz.rb lib/webgen/tag/sitemap.rb Base Tag lib/webgen/contentprocessor/less.rb lib/webgen/contentprocessor/scss.rb lib/webgen/contentprocessor/blocks.rb lib/webgen/contentprocessor/rdoc.rb lib/webgen/contentprocessor/sass.rb lib/webgen/contentprocessor/erb.rb lib/webgen/contentprocessor/rdiscount.rb lib/webgen/contentprocessor/tags.rb lib/webgen/contentprocessor/erubis.rb lib/webgen/contentprocessor/kramdown/html.rb lib/webgen/contentprocessor/haml.rb lib/webgen/contentprocessor/maruku.rb lib/webgen/contentprocessor/xmllint.rb lib/webgen/contentprocessor/kramdown.rb lib/webgen/contentprocessor/head.rb lib/webgen/contentprocessor/builder.rb lib/webgen/contentprocessor/tidy.rb lib/webgen/contentprocessor/redcloth.rb lib/webgen/contentprocessor/fragments.rb lib/webgen/contentprocessor.rb ContentProcessor lib/webgen/source/tararchive.rb lib/webgen/source/stacked.rb lib/webgen/source/resource.rb lib/webgen/source/filesystem.rb Source Loggable Webgen dot/m_82_0.png

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.

Methods

Included Modules

Loggable

Classes and Modules

Class Webgen::Website::ConfigFileInvalid

Attributes

blackboard  [R]  The blackboard used for inter-object communication. Can only be used after init has been called.
cache  [R]  A cache to store information that should be available between runs. Can only be used after init has been called.
config  [R]  The website configuration. Can only be used after init has been called (which is automatically done in render).
directory  [R]  The website directory.
logger  [RW]  The logger used for logging. If set to nil, logging is disabled.
tree  [R]  The internal data structure used to store information about individual nodes.

Public Class methods

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.

[Source]

     # File lib/webgen/website.rb, line 232
232:     def initialize(dir = nil, logger=Webgen::Logger.new($stdout, false), &block)
233:       @blackboard = nil
234:       @cache = nil
235:       @config = nil
236:       @logger = logger
237:       @config_block = block
238:       @directory = (dir.nil? ? (ENV['WEBGEN_WEBSITE'].to_s.empty? ? Dir.pwd : ENV['WEBGEN_WEBSITE']) : dir)
239:     end

Public Instance methods

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.

[Source]

     # File lib/webgen/website.rb, line 244
244:     def autoload_service(service_name, klass, method = service_name)
245:       blackboard.add_service(service_name) {|*args| cache.instance(klass).send(method, *args)}
246:     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!

[Source]

     # File lib/webgen/website.rb, line 292
292:     def clean(del_outdir = false)
293:       init
294:       execute_in_env do
295:         output = @blackboard.invoke(:output_instance)
296:         @tree.node_access[:alcn].each do |name, node|
297:           next if node.is_fragment? || node['no_output'] || node.path == '/' || node == @tree.dummy_root
298:           output.delete(node.path) rescue nil
299:         end
300: 
301:         if @config['website.cache'].first == :file
302:           FileUtils.rm(File.join(@directory, @config['website.cache'].last)) rescue nil
303:         end
304: 
305:         if del_outdir
306:           output.delete('/') rescue nil
307:         end
308:       end
309:     end

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

[Source]

     # File lib/webgen/website.rb, line 313
313:     def execute_in_env
314:       set_back = Thread.current[:webgen_website]
315:       Thread.current[:webgen_website] = self
316:       yield
317:     ensure
318:       Thread.current[:webgen_website] = set_back
319:     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!

[Source]

     # File lib/webgen/website.rb, line 251
251:     def init
252:       execute_in_env do
253:         @blackboard = Blackboard.new
254:         @config = Configuration.new
255: 
256:         load 'webgen/default_config.rb'
257:         Dir.glob(File.join(@directory, 'ext', '**/init.rb')) {|f| load(f)}
258:         read_config_file
259: 
260:         @config_block.call(@config) if @config_block
261:         restore_tree_and_cache
262:       end
263:       self
264:     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.

[Source]

     # File lib/webgen/website.rb, line 268
268:     def render
269:       result = nil
270:       execute_in_env do
271:         init
272: 
273:         puts "Starting webgen..."
274:         shm = SourceHandler::Main.new
275:         result = shm.render
276:         save_tree_and_cache if result
277:         puts "Finished"
278: 
279:         if @logger && @logger.log_output.length > 0
280:           puts "\nLog messages:"
281:           puts @logger.log_output
282:         end
283:       end
284:       result
285:     end

Private Instance methods

Update the configuration object for the website with infos found in the configuration file.

[Source]

     # File lib/webgen/website.rb, line 354
354:     def read_config_file
355:       file = File.join(@directory, 'config.yaml')
356:       if File.exists?(file)
357:         begin
358:           config = YAML::load(File.read(file)) || {}
359:           raise 'Structure of config file is not valid, has to be a Hash' if !config.kind_of?(Hash)
360:           config.each do |key, value|
361:             case key
362:             when *Webgen::Configuration::Helpers.public_instance_methods(false).map {|c| c.to_s} then @config.send(key, value)
363:             else @config[key] = value
364:             end
365:           end
366:         rescue RuntimeError, ArgumentError => e
367:           raise ConfigFileInvalid, "Configuration invalid: " + e.message
368:         end
369:       elsif File.exists?(File.join(@directory, 'config.yml'))
370:         log(:warn) { "No configuration file called config.yaml found (there is a config.yml - spelling error?)" }
371:       end
372:     end

Restore the tree and the cache from +website.cache+ and returns the Tree object.

[Source]

     # File lib/webgen/website.rb, line 326
326:     def restore_tree_and_cache
327:       @cache = Cache.new
328:       @tree = Tree.new
329:       data = if config['website.cache'].first == :file
330:                cache_file = File.join(@directory, config['website.cache'].last)
331:                File.open(cache_file, 'rb') {|f| f.read} if File.exists?(cache_file)
332:              else
333:                config['website.cache'].last
334:              end
335:       cache_data, tree, version = Marshal.load(data) rescue nil
336:       if cache_data && version == Webgen::VERSION
337:         @cache.restore(cache_data)
338:         @tree = tree
339:       end
340:     end

Save the tree and the cache to +website.cache+.

[Source]

     # File lib/webgen/website.rb, line 343
343:     def save_tree_and_cache
344:       cache_data = [@cache.dump, @tree, Webgen::VERSION]
345:       if config['website.cache'].first == :file
346:         cache_file = File.join(@directory, config['website.cache'].last)
347:         File.open(cache_file, 'wb') {|f| Marshal.dump(cache_data, f)}
348:       else
349:         config['website.cache'][1] = Marshal.dump(cache_data)
350:       end
351:     end

[Validate]