class Webgen::PathHandler::Virtual

Handles files which contain specifications for “virtual” nodes, ie. nodes that don't have real source paths.

This can be used, for example, to provide multiple links to the same node or links to external URLs.

Public Instance Methods

create_nodes(path, blocks) click to toggle source

Create all virtual nodes which are specified in path.

   # File lib/webgen/path_handler/virtual.rb
21 def create_nodes(path, blocks)
22   if path.meta_info.delete(:virtual)
23     create_node(path)
24   else
25     read_entries(blocks) do |key, meta_info|
26       meta_info['modified_at'] = path.meta_info['modified_at']
27       meta_info['no_output'] = true
28 
29       key = Webgen::Path.append(path.parent_path, key)
30       parent_path = create_directories(File.dirname(key), 'modified_at' => meta_info['modified_at'])
31 
32       dest_path = meta_info.delete('dest_path') || key
33       dest_path = if Webgen::Path.absolute?(dest_path)
34                     dest_path
35                   elsif dest_path =~ /^\//
36                     "webgen:#{dest_path}"
37                   else
38                     "webgen:#{File.join(parent_path, dest_path)}"
39                   end
40       meta_info['dest_path'] = dest_path
41       entry_path = Webgen::Path.new(key, meta_info)
42 
43       if key =~ /\/$/
44         entry_path['handler'] = 'directory'
45         @website.ext.path_handler.create_secondary_nodes(entry_path)
46       else
47         entry_path[:virtual] = true
48         entry_path['handler'] = 'virtual'
49         entry_path['node_class'] ||= Webgen::PathHandler::Base::Node.to_s
50         @website.ext.path_handler.create_secondary_nodes(entry_path)
51       end
52     end
53 
54     nil
55   end
56 end

Private Instance Methods

create_directories(directory, mi) click to toggle source

Create the needed parent directories for a virtual node.

   # File lib/webgen/path_handler/virtual.rb
81 def create_directories(directory, mi)
82   mi.merge!('no_output' => true, 'handler' => 'directory')
83   directory.sub(/^\//, '').split('/').inject('/') do |parent_path, dir|
84     parent_path = File.join(parent_path, dir) + '/'
85     path = Webgen::Path.new(parent_path, mi)
86     if !@website.tree[path.alcn]
87       @website.ext.path_handler.create_secondary_nodes(path)
88     end
89     parent_path
90   end
91 end
read_entries(blocks) { |key, meta_info| ... } click to toggle source

Read all entries from all blocks and yield the found path as well as the meta info hash for each entry.

   # File lib/webgen/path_handler/virtual.rb
64 def read_entries(blocks)
65   blocks.each do |name, content|
66     begin
67       data = YAML::load(content)
68     rescue RuntimeError, ArgumentError, SyntaxError, YAML::SyntaxError => e
69       raise RuntimeError, "Problem parsing block '#{name}' (it needs to contain a YAML hash): #{e.message}", e.backtrace
70     end
71     raise "Structure of block '#{name}' is invalid, it has to be a Hash" unless data.kind_of?(Hash)
72     data.each do |key, meta_info|
73       meta_info ||= {}
74       raise "Each path key value needs to be a Hash, found a #{meta_info.class} for '#{key}'" unless meta_info.kind_of?(Hash)
75       yield(key, meta_info)
76     end
77   end
78 end