| Class | Node |
| In: |
lib/webgen/node.rb
|
| Parent: | Object |
The Node class is used for building the internal data structure which represents the output tree.
| ABSOLUTE_URL | = | /^\w[a-zA-Z0-9+.-]*:/ | Regexp for matching absolute URLs, ie. URLs with a scheme part (also see RFC1738) |
| meta_info | [RW] | Meta information associated with the node. |
| node_info | [RW] | Information used for processing the node. |
| parent | [R] | The parent node. |
| path | [R] | The path of this node. |
Initializes a new Node instance.
| parent: | if this parameter is nil, then the new node acts as root. Otherwise, parent has to be a valid node instance. |
| path: | The path for this node. If this node is a directory, the path must have a
trailing slash (‘dir/’). If it is a fragment, the hash sign
must be the first character of the path (‘fragment’). A
compound path like ‘dir/file#fragment’ is also allowed as are
absolute paths like ‘myhost.com/’.
Note: a compound path like ‘dir/file’ is invalid if the parent node already has a child with path ‘dir/’!!! (solution: just create a node with path ‘file’ and node ‘dir/’ as parent!) |
# File lib/webgen/node.rb, line 57
57: def initialize( parent, path )
58: @parent = nil
59: @path = path
60: self.parent = parent
61: @node_info = Hash.new
62: @meta_info = Hash.new
63: end
Sorts nodes by using the meta info orderInfo of both involved nodes or, if these values are equal, by the meta info title.
# File lib/webgen/node.rb, line 170
170: def <=>( other )
171: self_oi = self.order_info
172: other_oi = other.order_info
173: (self_oi == other_oi ? (self['title'] || '') <=> (other['title'] || '') : self_oi <=> other_oi)
174: end
Matches the path of the node against the given path at the beginning. Returns the matched portion or nil. Used by resolve_node.
# File lib/webgen/node.rb, line 152
152: def =~( path )
153: md = if is_directory?
154: /^#{Regexp.escape(@path.chomp('/'))}(\/|$)/ =~ path #' #emacs hack
155: elsif is_fragment?
156: /^#{Regexp.escape(@path)}$/ =~ path
157: else
158: /^#{Regexp.escape(@path)}(?=#|$)/ =~ path
159: end
160: if md then $& end
161: end
Gets object name from meta_info.
# File lib/webgen/node.rb, line 86
86: def []( name )
87: @meta_info[name]
88: end
Assigns value to meta_info called +name.
# File lib/webgen/node.rb, line 91
91: def []=( name, value )
92: @meta_info[name] = value
93: end
Returns the absolute path, ie. starting with a slash for the root directory, for this node.
Here is an example that shows the difference between full_path and absolute_path:
root = Node.new( nil, '../output/' ) dir = Node.new( root, 'testdir/' ) node = Node.new( dir, 'testfile' ) node.full_path # => '../output/testdir/testfile' node.absolute_path # => '/testdir/testfile'
# File lib/webgen/node.rb, line 118
118: def absolute_path
119: return @precalc[:absolute_path] if @precalc
120:
121: if @parent.nil?
122: '/'
123: elsif @path =~ ABSOLUTE_URL
124: @path
125: else
126: full_path.sub( /^#{Regexp.escape(Node.root( self ).path)}/, '/' )
127: end
128: end
Returns the full path for this node. See also Node#absolute_path !
# File lib/webgen/node.rb, line 99
99: def full_path
100: return @precalc[:full_path] if @precalc
101:
102: if @path =~ ABSOLUTE_URL
103: @path
104: else
105: (@parent.nil? ? @path : @parent.full_path + @path)
106: end
107: end
Checks if the current node is in the subtree which is spanned by the supplied node. The check is performed using only the parent information of the involved nodes, NOT the actual path values!
# File lib/webgen/node.rb, line 194
194: def in_subtree_of?( node )
195: temp = self
196: temp = temp.parent while !temp.nil? && temp != node
197: !temp.nil?
198: end
Returns an informative representation of the node.
# File lib/webgen/node.rb, line 233
233: def inspect
234: "<##{self.class.name}: path=#{full_path}>"
235: end
Checks if the node is a directory.
# File lib/webgen/node.rb, line 136
136: def is_directory?
137: @path[-1] == ?/
138: end
Checks if the node is a file.
# File lib/webgen/node.rb, line 141
141: def is_file?
142: !is_directory? && !is_fragment?
143: end
Checks if the node is a fragment.
# File lib/webgen/node.rb, line 146
146: def is_fragment?
147: @path[0] == ?#
148: end
Returns the value of the meta info orderInfo or +0+ if it is not set.
# File lib/webgen/node.rb, line 164
164: def order_info
165: self['orderInfo'].to_s.to_i # nil.to_s.to_i => 0
166: end
Sets the path for the node.
# File lib/webgen/node.rb, line 80
80: def path=( path )
81: @path = path
82: precalc!
83: end
Returns the node representing the given path. The path can be absolute (i.e. starting with a slash) or relative to the current node. If no node exists for the given path or it would lie outside the node tree, nil is returned.
# File lib/webgen/node.rb, line 203
203: def resolve_node( path )
204: url = self.to_url + path
205:
206: path = url.path[1..-1].to_s + (url.fragment.nil? ? '' : '#' + url.fragment)
207: return nil if path =~ /^\.\./ || url.scheme != 'webgen' # path outside dest dir or not an internal URL (webgen://...)
208:
209: node = Node.root( self )
210:
211: match = nil
212: while !node.nil? && !path.empty?
213: node = node.find {|c| match = (c =~ path) }
214: path.sub!( match, '' ) unless node.nil?
215: break if path.empty?
216: end
217:
218: node
219: end
Returns the route to the given path. The parameter path can be a String or a Node.
# File lib/webgen/node.rb, line 177
177: def route_to( other )
178: my_url = self.to_url
179: other_url = case other
180: when String then my_url + other
181: when Node then other.to_url
182: else raise ArgumentError
183: end
184: # resolve any '.' and '..' paths in the target url
185: if other_url.path =~ /\/\.\.?\// && other_url.scheme == 'webgen'
186: other_url.path = Pathname.new( other_url.path ).cleanpath.to_s
187: end
188: route = my_url.route_to( other_url ).to_s
189: (route == '' ? ( self.is_fragment? ? self.parent.path : self.path ) : route )
190: end
Returns the full URL (including dummy scheme and host) for use with URI classes. The returned URL does not include the real path of the root node but a slash instead. So if the full path of the node is ‘a/b/c/d/file1’ and the root node path is ‘a/b/c’, the URL path would be ’/d/file1’.
# File lib/webgen/node.rb, line 224
224: def to_url
225: return @precalc[:to_url] if @precalc
226:
227: url = URI::parse( absolute_path )
228: url = URI::parse( 'webgen://webgen.localhost/' ) + url unless url.absolute?
229: url
230: end
# File lib/webgen/node.rb, line 252
252: def precalc_with_children!
253: precalc!
254: each {|child| child.precalc_with_children!}
255: end
Delegates missing methods to a processor. The current node is placed into the argument array as the first argument before the method name is invoked on the processor.
# File lib/webgen/node.rb, line 260
260: def method_missing( name, *args, &block )
261: if @node_info[:processor]
262: @node_info[:processor].send( name, *([self] + args), &block )
263: else
264: super
265: end
266: end