Class: Giblish::DependencyGraphPostBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/giblish/indexbuilders/depgraphbuilder.rb

Overview

Generates a summary page with a docid-based dependency graph for an entire destination tree.

Constant Summary collapse

DEFAULT_BASENAME =
"gibgraph"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_2_ids, docattr_provider = nil, api_opt_provider = nil, adoc_src_provider = nil, basename = DEFAULT_BASENAME) ⇒ DependencyGraphPostBuilder

node_2_ids

a { src_node => [doc id refs]} hash. It will be queried during the post-build phase => it must be populated before the end of the build phase



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/giblish/indexbuilders/depgraphbuilder.rb', line 49

def initialize(node_2_ids, docattr_provider = nil, api_opt_provider = nil, adoc_src_provider = nil, basename = DEFAULT_BASENAME)
  # this class relies on graphwiz (dot), make sure we can access it
  raise "Could not find the 'dot' tool needed to generate a dependency graph!" unless DependencyGraphPostBuilder.dot_supported

  # require asciidoctor module needed for generating diagrams
  require "asciidoctor-diagram/graphviz"

  @node_2_ids = node_2_ids
  @docattr_provider = docattr_provider
  @api_opt_provider = api_opt_provider
  @adoc_src_provider = adoc_src_provider || GraphPageBase
  @basename = basename

  @adoc_source = nil
end

Class Method Details

.dot_supportedObject

the dependency graph relies on graphwiz (dot), check if we can access that



40
41
42
# File 'lib/giblish/indexbuilders/depgraphbuilder.rb', line 40

def self.dot_supported
  !Giblish.which("dot").nil?
end

Instance Method Details

#adoc_source(src_node, dst_node, dst_top) ⇒ Object



73
74
75
# File 'lib/giblish/indexbuilders/depgraphbuilder.rb', line 73

def adoc_source(src_node, dst_node, dst_top)
  @adoc_source
end

#api_options(src_node, dst_node, dst_top) ⇒ Object



69
70
71
# File 'lib/giblish/indexbuilders/depgraphbuilder.rb', line 69

def api_options(src_node, dst_node, dst_top)
  @api_opt_provider.nil? ? {} : @api_opt_provider.api_options(dst_top)
end

#document_attributes(src_node, dst_node, dst_top) ⇒ Object



65
66
67
# File 'lib/giblish/indexbuilders/depgraphbuilder.rb', line 65

def document_attributes(src_node, dst_node, dst_top)
  @docattr_provider.nil? ? {} : @docattr_provider.document_attributes(src_node, dst_node, dst_top)
end

#on_postbuild(src_tree, dst_tree, converter) ⇒ Object

Called from TreeConverter during post build phase



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/giblish/indexbuilders/depgraphbuilder.rb', line 78

def on_postbuild(src_tree, dst_tree, converter)
  return unless DependencyGraphPostBuilder.dot_supported

  # convert {src_node => [doc ids]} to {conv_info => [doc ids]}
  info_2_ids = {}
  dst_tree.traverse_preorder do |_level, dst_node|
    next unless dst_node.leaf?

    sn = dst_node.data.src_node
    info_2_ids[dst_node] = @node_2_ids[sn] if @node_2_ids.key?(sn)
  end

  # add a virtual 'gibgraph.adoc' node as the only node in a source tree
  # with this object as provider of both adoc source and conversion options
  v_srcpath = Pathname.new("/virtual") / "#{@basename}.adoc"
  src_node = PathTree.new(v_srcpath, self).node(v_srcpath, from_root: true)

  # add the destination node where the converted file will be stored
  i_node = dst_tree.add_descendants(@basename)

  # get the adoc source from the provider (Class or instance)
  @adoc_source = if @adoc_src_provider.is_a?(Class)
    @adoc_src_provider.new(info_2_ids, dst_tree, @basename).adoc_source
  else
    @adoc_src_provider.adoc_source
  end

  # do the conversion
  converter.convert(src_node, i_node, dst_tree)
end