Class: Giblish::SubtreeInfoBuilder

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

Overview

A post builder that provides the ability for a adoc source provider to run on each directory node in the destination pathtree output from the build step.

The adoc source provider is invoked on each directory node depth-first. The resulting adoc source is converted and the result added to the destination pathtree from the build step.

Constant Summary collapse

DEFAULT_BASENAME =
"index"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

docattr_provider

an object implementing the DocAttributesBase interface

api_opt_provider

an object implementing the api_options(dst_top) interface

adoc_src_provider

a Class or object implementing the SubtreeSrcItf interface

basename

the name of the output file that is generated in each directory



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/giblish/subtreeinfobuilder.rb', line 30

def initialize(docattr_provider = nil, api_opt_provider = nil, adoc_src_provider = nil, basename = DEFAULT_BASENAME)
  @docattr_provider = docattr_provider
  @api_opt_provider = api_opt_provider

  @adoc_src_provider = adoc_src_provider || SubtreeIndexBase
  if @adoc_src_provider.is_a?(Class)
    @adoc_src_provider = @adoc_src_provider.new(
      {docattr_provider: docattr_provider,
       api_opt_provider: api_opt_provider}
    )
  end

  @basename = basename
  @adoc_source = nil
end

Instance Attribute Details

#docattr_providerObject

Returns the value of attribute docattr_provider.



22
23
24
# File 'lib/giblish/subtreeinfobuilder.rb', line 22

def docattr_provider
  @docattr_provider
end

Instance Method Details

#adoc_source(src_node, dst_node, dst_top) ⇒ Object



54
55
56
# File 'lib/giblish/subtreeinfobuilder.rb', line 54

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

#api_options(src_node, dst_node, dst_top) ⇒ Object



50
51
52
# File 'lib/giblish/subtreeinfobuilder.rb', line 50

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



46
47
48
# File 'lib/giblish/subtreeinfobuilder.rb', line 46

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

adds a ‘index’ node for each directory in the source tree and convert that index using the options from the provider objects given at instantiation of this object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/giblish/subtreeinfobuilder.rb', line 63

def on_postbuild(src_tree, dst_tree, converter)
  dst_tree.traverse_preorder do |level, dst_node|
    # we only care about directories
    next if dst_node.leaf?

    # get the relative path to the index dir from the top dir
    index_dir = dst_node.pathname.relative_path_from(dst_tree.pathname).cleanpath
    Giblog.logger.debug { "Creating #{@basename} under #{index_dir}" }

    @adoc_source = @adoc_src_provider.adoc_source(dst_node, @basename)

    # add a virtual 'index.adoc' node as the only node in a source tree
    # with this object as source for conversion options
    # and adoc_source
    v_path = Pathname.new("/virtual") / index_dir / "#{@basename}.adoc"
    v_tree = PathTree.new(v_path, self)
    src_node = v_tree.node(v_path, from_root: true)

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

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