Module: Mushy::Builder::Documentation

Defined in:
lib/mushy/builder/documentation.rb

Class Method Summary collapse

Class Method Details

.build_from(config) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mushy/builder/documentation.rb', line 5

def self.build_from config
  basic_usage = "#{config[:description]}"
  if config[:config]&.any?
    rows = config[:config]
             .map { |x| [x[0], x[1][:description], (x[1][:shrink] ? '(optional) ' : '')] }
             .reduce("") { |t, i| "#{t}<tr><td>#{i[0]}</td><td>#{i[2]}#{i[1]}</td></tr>" }
    basic_usage += '<table class="table is-bordered"><thead><tr><td>Field</td><td>Description</td></tr></thead>' + rows + "</table>"
  end

  {
    "Basic Usage" => basic_usage,
  }.tap do |documentation|
    if config[:examples]
      config[:examples].each do |item|
        documentation[item[0]] = [
          item[1][:description],
          code_sample('Input', item[1][:input]),
          code_sample('Config', item[1][:config]),
          code_sample('Result', item[1][:result]),
        ].select { |x| x }.reduce('') { |t, i| t + i }
      end
    end
  end
end

.code_sample(title, value) ⇒ Object



30
31
32
33
# File 'lib/mushy/builder/documentation.rb', line 30

def self.code_sample title, value
  return nil unless value
  "<div><b>#{title}</b></div><pre><code>#{JSON.pretty_generate(value)}</code></pre>"
end