Class: Webgen::SourceHandler::Main

Inherits:
Object
  • Object
show all
Includes:
Loggable, WebsiteAccess
Defined in:
lib/webgen/sourcehandler.rb

Overview

This class is used by Website to do the actual rendering of the website. It

  • collects all source paths using the source classes

  • creates nodes using the source handler classes

  • writes changed nodes out using an output class

Instance Method Summary collapse

Methods included from Loggable

#log, #puts

Methods included from WebsiteAccess

included, website

Constructor Details

#initializeMain

:nodoc:



33
34
35
36
37
# File 'lib/webgen/sourcehandler.rb', line 33

def initialize #:nodoc:
  website.blackboard.add_service(:create_nodes, method(:create_nodes))
  website.blackboard.add_service(:source_paths, method(:find_all_source_paths))
  website.blackboard.add_listener(:node_meta_info_changed?, method(:meta_info_changed?))
end

Instance Method Details

#render(tree) ⇒ Object

Render the nodes provided in the tree. Before the actual rendering is done, the sources are checked (nodes for deleted sources are deleted, nodes for new and changed sources).



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/webgen/sourcehandler.rb', line 41

def render(tree)
  # Add new and changed nodes, remove nodes of deleted paths
  puts "Generating tree..."
  time = Benchmark.measure do
    used_paths = Set.new
    paths = Set.new([nil])
    while paths.length > 0
      used_paths += (paths = Set.new(find_all_source_paths.keys) - used_paths - clean(tree))
      create_nodes_from_paths(tree, paths)
      website.cache.reset_volatile_cache
    end
  end
  puts "...done in " + ('%2.4f' % time.real) + ' seconds'

  output = website.blackboard.invoke(:output_instance)

  puts "Writing changed nodes..."
  time = Benchmark.measure do
    tree.node_access[:alcn].sort.each do |name, node|
      node.dirty_meta_info = node.created = false
      next if node == tree.dummy_root || !node.dirty
      node.dirty = false

      begin
        if !node['no_output'] && (content = node.content)
          puts " "*4 + name, :verbose
          type = if node.is_directory?
                   :directory
                 elsif node.is_fragment?
                   :fragment
                 else
                   :file
                 end
          output.write(node.path, content, type)
        end
      rescue
        raise RuntimeError, "Error while processing <#{node.absolute_lcn}>: #{$!.message}", $!.backtrace
      end
    end
  end
  puts "...done in " + ('%2.4f' % time.real) + ' seconds'
end