module Webgen::Task::CreateWebsite

About

Creates the website.

This task assumes that the website directory does not exist and populates it from a specified template. webgen extensions can provide additional website templates, see below.

For each created file a verbose log message is recorded in the format used when webgen generates a website (because, actually, webgen's website generating facilities are used to create the website structure).

Creating a website template

A website template is just a directory holding all the files necessary for a webgen website and therefore looks very similar to an actual webgen website directory. However, the template is not just copied but processed ('generated') by webgen itself.

What this means is:

Thus one can use Erb or any other supported content processor to customize the generated files!

Once a website template has been created, it needs to be registered with a template name, like this:

website.ext.task.data(:create_website)[:templates][TEMPLATE_NAME] = ABSOLUTE_DIR_PATH

Public Class Methods

call(website, template = nil) click to toggle source

Create the website from the given template.

This actually uses webgen's file copying/generating facilities to populate the website directory. Kind of bootstrapping really.

Returns true if the website has been created.

   # File lib/webgen/task/create_website.rb
56 def self.call(website, template = nil)
57   if File.exist?(website.directory)
58     raise Error.new("Directory <#{website.directory}> does already exist!")
59   end
60   if template && !website.ext.task.data(:create_website)[:templates].has_key?(template)
61     raise Error.new("Unknown template '#{template}' specified!")
62   end
63 
64   begin
65     Dir.mktmpdir do |tmpdir|
66       ws = Webgen::Website.new(tmpdir) do |iws|
67         iws.config['sources'] = [['/', :file_system, File.join(Webgen::Utils.data_dir, 'basic_website_template')]]
68         if template
69           iws.config['sources'].unshift(['/', :file_system, website.ext.task.data(:create_website)[:templates][template]])
70         end
71         iws.config['destination'] = [:file_system, File.expand_path(website.directory)]
72         iws.ext.path_handler.registered_extensions.each do |_name, data|
73           data.patterns = []
74         end
75         iws.ext.path_handler.registered_extensions[:copy].patterns = ['**/*', '**/']
76         iws.logger.level = ::Logger::INFO
77         iws.logger.formatter = Proc.new do |_severity, _timestamp, _progname, msg|
78           website.logger.vinfo(msg) if msg =~ /\[create\]/
79         end
80       end
81       ws.execute_task(:generate_website)
82     end
83   rescue Webgen::Error => e
84     raise Error.new("Could not create website from template: #{e.message}")
85   end
86 
87   true
88 end