class Webgen::Node

Represents a file, a directory or a fragment. A node always belongs to a Tree.

All needed meta and processing information is associated with the node itself. The meta information is available through the [] and meta_info accessors, the internal processing information through the node_info accessor.

This class is not directly used. Instead path handlers define sub-classes that provide handler specific methods. See the basic sub-class Webgen::PathHandler::Base::Node.

Attributes

acn[R]

The absolute canonical name of this node.

alcn[R]

The absolute localized canonical name of this node.

children[R]

The child nodes of this node.

cn[R]

The canonical name of this node.

dest_path[R]

The full destination path of this node.

lang[R]

The language of this node.

lcn[R]

The localized canonical name of this node.

level[R]

The level of the node. The level specifies how deep the node is in the hierarchy.

meta_info[R]

Meta information associated with the node. If you need just a value for a meta information key, use the [] method.

node_info[R]

Return the node information hash which contains information for processing the node.

parent[R]

The parent node. This is in all but one case a Node object. The one exception is that the parent of the Tree#dummy_node is a Tree object.

tree[R]

The tree to which this node belongs.

Public Class Methods

new(parent, cn, dest_path, meta_info = {}) click to toggle source

Create a new Node instance.

parent (immutable)

The parent node under which this nodes should be created.

cn (immutable)

The canonical name for this node. Needs to be of the form 'basename.ext' or 'basename' where basename does not contain any dots. Also, the basename must not include a language part!

dest_path (immutable)

The full output path for this node. If this node is a directory, the path must have a trailing slash ('dir/'). If it is a fragment, it has to include a hash sign. This can also be an absolute path like example.com.

meta_info

A hash with meta information for the new node.

The language of a node is taken from the meta information lang and the entry is deleted from the meta information hash. The language cannot be changed afterwards! If no lang key is found, the node is unlocalized.

    # File lib/webgen/node.rb
 78 def initialize(parent, cn, dest_path, meta_info = {})
 79   @parent = parent
 80   @children = []
 81   @cn = cn.freeze
 82   @dest_path = dest_path.freeze
 83 
 84   @lang = Webgen::LanguageManager.language_for_code(meta_info.delete('lang'))
 85   @lang = nil unless is_file?
 86 
 87   @lcn = Webgen::Path.lcn(@cn, @lang).freeze
 88   @acn = (@parent.kind_of?(Webgen::Node) ? @parent.acn.sub(/#.*$/, '') + @cn : '').freeze
 89   @alcn = (@parent.kind_of?(Webgen::Node) ? @parent.alcn.sub(/#.*$/, '') + @lcn : '').freeze
 90 
 91   @meta_info = meta_info
 92   @node_info = {}
 93 
 94   @level = -1
 95   @tree = @parent
 96   (@level += 1; @tree = @tree.parent) while @tree.kind_of?(Webgen::Node)
 97 
 98   @tree.register_node(self)
 99   @parent.children << self unless @parent == @tree
100 end

Public Instance Methods

=~(pattern) click to toggle source

Return true if the alcn matches the pattern.

See Webgen::Path.matches_pattern? for more information.

    # File lib/webgen/node.rb
149 def =~(pattern)
150   Webgen::Path.matches_pattern?(@alcn, pattern)
151 end
[](key) click to toggle source

Return the meta information item for key.

    # File lib/webgen/node.rb
103 def [](key)
104   @meta_info[key]
105 end
is_ancestor_of?(node) click to toggle source

Check if the this node is an ancestor of node.

The check is performed using only the parent information of the involved nodes, NOT the actual alcn values!

    # File lib/webgen/node.rb
131 def is_ancestor_of?(node)
132   node = node.parent
133   node = node.parent while node != tree.dummy_root && node != self
134   node != tree.dummy_root
135 end
is_directory?() click to toggle source

Check if the node is a directory.

    # File lib/webgen/node.rb
108 def is_directory?
109   @cn[-1] == ?/ && !is_fragment?
110 end
is_file?() click to toggle source

Check if the node is a file.

    # File lib/webgen/node.rb
113 def is_file?
114   !is_directory? && !is_fragment?
115 end
is_fragment?() click to toggle source

Check if the node is a fragment.

    # File lib/webgen/node.rb
118 def is_fragment?
119   @cn[0] == ?#
120 end
is_root?() click to toggle source

Check if the node is the root node.

    # File lib/webgen/node.rb
123 def is_root?
124   self == tree.root
125 end
proxy_node(lang = @lang) click to toggle source

Return the proxy node in language lang.

This node should be used, for example, when routing to this node. The proxy node is found by using the proxy_path meta information. This meta information is usually set on directories to specify the node that should be used for the “directory index”.

If the lang parameter is not used, it defaults to the language of the current node.

    # File lib/webgen/node.rb
190 def proxy_node(lang = @lang)
191   @meta_info['proxy_path'] && resolve(@meta_info['proxy_path'], lang, true) || self
192 end
resolve(path, lang = @lang, msg_on_failure = false) click to toggle source

Return the node representing the given path in the given language.

The path can be absolute (i.e. starting with a slash) or relative to the current node. Relative paths are made absolute by using the alcn of the current node.

If the lang parameter is not used, it defaults to the language of the current node.

See Tree#resolve_node for detailed information on how the correct node for the path is found and for the msg_on_failure parameter.

    # File lib/webgen/node.rb
162 def resolve(path, lang = @lang, msg_on_failure = false)
163   @tree.resolve_node(Webgen::Path.append(@alcn, path), lang, msg_on_failure)
164 end
route_to(node, lang = @lang) click to toggle source

Return the relative path to the given node.

If the lang parameter is not used, it defaults to the language of the current node.

    # File lib/webgen/node.rb
169 def route_to(node, lang = @lang)
170   my_url = Webgen::Path.url(@dest_path)
171   pnode = node.proxy_node(lang)
172   other_url = Webgen::Path.url(pnode['routing_path'] || pnode.dest_path)
173 
174   # resolve any '.' and '..' paths in the target url
175   if other_url.path =~ /\/\.\.?\// && other_url.scheme == 'webgen'
176     other_url = other_url.dup
177     other_url.path = Pathname.new(other_url.path).cleanpath.to_s
178   end
179   route = my_url.route_to(other_url).to_s
180   (route == '' ? File.basename(@dest_path) : route)
181 end
to_s() click to toggle source

Return the string representation of the node which is just the alcn.

    # File lib/webgen/node.rb
138 def to_s
139   @alcn
140 end
versions() click to toggle source

Return all versions of this node.

    # File lib/webgen/node.rb
217 def versions
218   tree.node_access[:alcn].select {|alcn, n| n.node_info[:path] == node_info[:path]}.
219     each_with_object({}) {|(_, v), h| h[v['version']] = v}
220 end