Class: Litbuild::AsciiDocVisitor

Inherits:
MultiPartVisitor show all
Defined in:
lib/litbuild/ascii_doc_visitor.rb

Overview

This class writes AsciiDoc fragments to directories, rooted at the DOCUMENT_DIR directory. It adds a sequential number prefix on each fragment written to a directory, and ensures that no duplicate fragments are written: if a fragment for a specific blueprint (or blueprint phase) has already been written to any directory, AsciiDocVisitor will ignore subsequent requests to write that blueprint (phase). AsciiDocVisitor can also write a top-level AsciiDoc document that includes all top-level fragments.

Instance Method Summary collapse

Methods inherited from MultiPartVisitor

#visit_appendix, #visit_part

Methods inherited from Visitor

#in_subdirectory

Constructor Details

#initialize(parameters:) ⇒ AsciiDocVisitor

Returns a new instance of AsciiDocVisitor.



18
19
20
21
22
23
# File 'lib/litbuild/ascii_doc_visitor.rb', line 18

def initialize(parameters:)
  @parameters = parameters
  super(directory: @parameters['DOCUMENT_DIR'])
  @written = Hash.new { |hash, key| hash[key] = [] }
  @all_written_targets = []
end

Instance Method Details

#visit_commands(commands:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/litbuild/ascii_doc_visitor.rb', line 25

def visit_commands(commands:)
  file_collector = {}
  extra_section = proc do |doc|
    render_files(doc, commands, file_collector)
  end
  write(blueprint: commands,
        location: cwd,
        extra: extra_section) do |doc, directive, values|
    case directive
    when 'commands' then write_commands(doc, values)
    when 'file' then write_file_chunk(doc, file_collector, values)
    end
  end
end

#visit_narrative(narrative:) ⇒ Object



40
41
42
# File 'lib/litbuild/ascii_doc_visitor.rb', line 40

def visit_narrative(narrative:)
  write(blueprint: narrative, location: cwd)
end

#visit_package(package:) ⇒ Object



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
# File 'lib/litbuild/ascii_doc_visitor.rb', line 44

def visit_package(package:)
  write(blueprint: package,
        location: cwd,
        summary: summary_block_for(package)) do |doc, directive, value|
    case directive
    when 'configure-commands'
      write_stage_commands(doc, value, 'Configuration')
    when 'compile-commands'
      write_stage_commands(doc, value, 'Compilation')
    when 'test-commands'
      write_stage_commands(doc, value, 'Test')
    when 'install-commands'
      write_stage_commands(doc, value, 'Installation')
    when 'before-build-as-root'
      write_stage_commands(doc, value, 'Pre-build (as `root`)')
    when 'after-install-as-root'
      write_stage_commands(doc, value, 'Post-installation (as `root`)')
    when 'patches'
      write_patch_block(doc, package, value)
    when 'in-tree-sources'
      write_in_tree_block(doc, package, value)
    when 'build-dir'
      write_build_dir(doc, value)
    end
  end
end

#visit_section(section:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/litbuild/ascii_doc_visitor.rb', line 71

def visit_section(section:)
  # The files that need to be included by the section -- explicitly
  # declared, added for dependencies, implicitly included by phase
  # or whatever. Explicit blueprints (and their dependencies, if
  # any) will be written into the narrative wherever they appear;
  # the others will be written at the end by
  # `write_remaining_blueprints`.
  files = @written[File.join(cwd, section.name)].clone
  write_remaining_blueprints = proc do |doc|
    files.each do |file|
      doc.puts
      doc.puts("include::./#{section.name}/#{file}[leveloffset=+1]")
    end
  end
  write(blueprint: section,
        location: cwd,
        extra: write_remaining_blueprints) do |doc, directive, value|
    case directive
    when 'blueprints'
      write_blueprint_includes(doc, value, section.name, files)
    when 'package-list-with-versions'
      write_package_list(doc, section)
    end
  end
end

#write_toplevel_doc(blueprint:) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/litbuild/ascii_doc_visitor.rb', line 97

def write_toplevel_doc(blueprint:)
  doc_dir = @parameters['DOCUMENT_DIR']
  return if @written[doc_dir].empty?

  doc_name = File.join(doc_dir, "#{blueprint.file_name}.adoc")
  File.open(doc_name, 'w') do |f|
    top_header = blueprint.header_text
    top_header += " (#{blueprint.active_phase})" if blueprint.active_phase
    f.puts("= #{top_header}")
    f.puts(blueprint.value('author')) if blueprint['author']
    f.puts(blueprint.value('revision')) if blueprint['revision']
    f.puts(':sectnums:')
    f.puts(':doctype: book') unless other_parts.empty?
    write_toplevel_includes(location: doc_dir, document: f)
  end
end