class Webgen::PathHandler

Namespace for all path handlers.

About

A path handler is a webgen extension that uses source Path objects to create Node objects and that provides methods for rendering these nodes. The nodes are stored in a hierarchy, the root of which is a Tree object. Path handlers can do simple things, like copying a path from the source to the destination, or a complex things, like generating a whole set of nodes from one input path (e.g. generating a whole image gallery)!

The paths that are handled by a path handler are generally specified via path patterns. The create_nodes method of a path handler is called for each source path that should be handled. And when it is time to write out a node, the content method on the path handler associated with the node is called to retrieve the rendered content of the node.

Tree creation

The method populate_tree is used for creating the initial node tree, the internal representation of all paths. It is only the initial tree because it is possible that additional, secondary nodes are created during the rendering phase by using the create_secondary_nodes method.

Tree creation works like this:

  1. All path handlers on the invocation list are used in turn. The order is important; it allows avoiding unnecessary write phases and it makes sure that, for example, directory nodes are created before their file nodes.

  2. When a path handler is used for creating nodes, all source paths (retrieved by using Webgen::Source#paths method) that match one of the associated patterns and/or all path with the 'handler' meta information set to the path handler are used.

  3. The meta information of a used source path is then updated with the meta information applied by methods registered for the :apply_meta_info_to_path blackboard message.

    After that the source path is given to the parse_meta_info! method of the path handler so that meta information of the path can be updated with meta information stored in the content of the path itself.

    Then the meta information 'versions' is used to determine if multiple version of the path should be used for creating nodes and each path version is then given to the create_nodes method of the path handler so that it can create one or more nodes.

  4. Nodes returned by creates_nodes of a path handler are assumed to have the Node#node_info keys :path and :path_handler and the meta info key 'modified_at' correctly set (this is automatically done if the Webgen::PathHandler::Base#create_node method is used).

Path Patterns and Invocation order

Path patterns define which paths are handled by a specific path handler. These patterns are specified when a path handler is registered using register method. The patterns need to have a format that Dir.glob can handle. Note that a user can always associate any path with a path handler through a meta information path and the 'handler' meta information key.

In addition to specifying the patterns a path handler uses, one can also specify the place in the invocation list which the path handler should use. The invocation list is used from the front to the back when the Tree is created.

Implementing a path handler

A path handler must take the website as the only parameter on initialization and needs to define the following methods:

parse_meta_info!(path)

Update path.meta_info with meta information found in the content of the path. The return values of this method are given to the create_nodes method as additional parameters!

This allows one to use a single pass for reading the meta information and the normal content of the path.

create_nodes(path, …)

Create one or more nodes from the path and return them. If parse_meta_info! returns one or more values, these values are provided as additional parameters to this method.

It is a good idead to use the helper method Webgen::PathHandler::Base#create_node for actually creating a node.

content(node)

Return the content of the given node. This method is only called for nodes that have been created by the path handler.

Also note that a path handler does not need to reside under the Webgen::PathHandler namespace but all built-in ones do so that auto-loading of the path handlers works.

The Webgen::PathHandler::Base module provides default implementations of the needed methods (except for create_nodes) and should be used by all path handlers! If a path handler processes paths in Webgen Page Format, it should probably also use Webgen::PathHandler::PageUtils.

Information that is used by a path handler only for processing purposes should be stored in the node_info hash of a node as the meta_info hash is reserved for user provided node meta information.

Following is a simple path handler class example which copies paths from the source to the destination and modifies the extension in the process:

class SimpleCopy

  include Webgen::PathHandler::Base

  def create_nodes(path)
    path.ext += '.copied'
    create_node(path)
  end

  def content(node)
    node.node_info[:path]
  end

end

website.ext.path_handler.register(SimpleCopy, patterns: ['**/*.jpg', '**/*.png'])

Attributes

current_dest_node[R]

The destination node if one is currently written (only during the invocation of write_tree) or nil otherwise.

Public Class Methods

new(website)

Create a new path handler object for the given website.

Calls superclass method Webgen::ExtensionManager::new

Public Instance Methods

create_secondary_nodes(path, content = '', source_alcn = nil)

Create nodes for the given path (a Path object which must not be a source path).

The content of the path also needs to be specified. Note that if an IO block is associated with the path, it is discarded!

If the parameter handler is present, nodes from the given path are only created with the specified handler.

If the secondary nodes are created during the rendering phase (and not during node creation, ie. in a create_nodes method of a path handler), the source_alcn has to be set to the node alcn from which these nodes are created!

instance(handler)

Return the instance of the path handler class with the given name.

populate_tree()

Populate the website tree with nodes.

Can only be called once because the tree can only be populated once!

register(klass, options={}, &block)

Register a path handler.

The parameter klass has to contain the name of the path handler class or the class object itself. If the class is located under this namespace, only the class name without the hierarchy part is needed, otherwise the full class name including parent module/class names is needed.

Options:

:name

The name for the path handler. If not set, it defaults to the snake-case version of the class name (without the hierarchy part). It should only contain letters.

:patterns

A list of path patterns for which the path handler should be used. If not specified, defaults to an empty list.

:insert_at

Specifies the position in the invocation list. If not specified or if :end is specified, the handler is added to the end of the list. If :front is specified, it is added to the beginning of the list. Otherwise the value is expected to be a position number and the path handler is added at the specified position in the list.

Examples:

path_handler.register('Template')     # registers Webgen::PathHandler::Template

path_handler.register('::Template')   # registers Template !!!

path_handler.register('MyModule::Doit', name: 'template', patterns: ['**/*.template'])
write_tree()

Write all changed nodes of the website tree to their respective destination using the Destination object at website.ext.destination.

Returns the number of passes needed for correctly writing out all paths.