Parent

Class/Module Index [+]

Quicksearch

Webgen::WebsiteManager

This class is used for managing webgen websites. It provides access to website bundles defined as resources and makes it easy to apply them to a webgen website.

General information

Currently, the following actions are supported:

Bundles are partial webgen websites that contain certain functionality. For example, most of the bundles shipped with webgen are style bundles that define the basic page layout or how image galleries should look like. So style bundles are basically used to change the appearance of parts (or the whole) website. This makes them a powerful tool as this plugin makes it easy to change to another style bundle later!

However, not all bundles have to be style bundles. For example, you could as easily create a bundle for a plugin or for a complete website (e.g. a blog template).

website bundle resource naming convention

The shipped bundles are defined using resources. Each such resource has to be a directory containing an optional README file in YAML format in which key-value pairs provide additional information about the bundle (e.g. copyright information, description, ...). All other files/directories in the directory are copied to the root of the destination webgen website when the bundle is used.

This class uses a special naming convention to recognize website bundles:

Website bundle names have to be unique!

Attributes

bundles[R]

A hash with the available website bundles (mapping name to infos).

website[R]

The used Website object.

Public Class Methods

new(dir) click to toggle source

Create a new WebsiteManager.

If dir is a String, then the website manager is created for the website in the directory dir.

If dir is a Website object, the website manager is created for the website represented by dir. If the website object is initialized if it isn't already.

# File lib/webgen/websitemanager.rb, line 58
def initialize(dir)
  if dir.kind_of?(Webgen::Website)
    @website = dir
    @website.init if @website.config.nil?
  else
    @website = Webgen::Website.new(dir)
    @website.init
  end
  @bundles = {}

  @website.execute_in_env do
    prefix = "webgen-website-bundle-"
    @website.config['resources'].select {|name, data| name =~ /^#{prefix}/}.each do |name, data|
      add_source(Webgen::Source::Resource.new(name), name.sub(prefix, ''))
    end
  end
end

Public Instance Methods

add_source(source, name) click to toggle source

Treat the source as a website bundle and make it available to the WebsiteManager under name.

# File lib/webgen/websitemanager.rb, line 78
def add_source(source, name)
  paths = source.paths.dup
  readme = paths.select {|path| path == '/README' }.first
  paths.delete(readme) if readme
  infos = OpenStruct.new(readme.nil? ? {} : YAML::load(readme.io.data))
  infos.paths = paths
  @bundles[name] = infos
end
apply_bundle(bundle) click to toggle source

Apply the given bundle to the website by copying the files.

# File lib/webgen/websitemanager.rb, line 94
def apply_bundle(bundle)
  raise ArgumentError.new("Invalid bundle name") if !@bundles.has_key?(bundle)
  raise "Directory <#{@website.directory}> does not exist!" unless File.exists?(@website.directory)
  write_paths(@bundles[bundle].paths)
end
create_website() click to toggle source

Create the basic website skeleton (without any bundle applied).

# File lib/webgen/websitemanager.rb, line 88
def create_website
  raise "Directory <#{@website.directory}> does already exist!" if File.exists?(@website.directory)
  @website.execute_in_env { write_paths(Webgen::Source::Resource.new('webgen-website-skeleton').paths) }
end

Private Instance Methods

write_paths(paths) click to toggle source

Write the paths to the website directory.

# File lib/webgen/websitemanager.rb, line 105
def write_paths(paths)
  paths.each do |path|
    output_path = File.join(@website.directory, path.path)
    if path.path =~ /\/$/
      FileUtils.mkdir_p(output_path)
    else
      FileUtils.mkdir_p(File.dirname(output_path))
      path.io.stream do |source|
        File.open(output_path, 'wb') {|f| FileUtils.copy_stream(source, f) }
      end
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.