Class: OKF::CLI::Render

Inherits:
Command show all
Defined in:
lib/okf/cli/render.rb

Overview

The static counterpart to server: bake the whole bundle into one self-contained HTML file (bodies, catalog, index, logs baked in, no server needed — e.g. hosting on GitHub Pages). Prints to stdout unless -o is given.

Constant Summary

Constants inherited from Command

Command::DUCK_TYPE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

hidden?, #initialize

Constructor Details

This class inherits a constructor from OKF::CLI::Command

Class Method Details

.groupObject



13
14
15
# File 'lib/okf/cli/render.rb', line 13

def self.group
  :act
end

.help_rowsObject



17
18
19
20
21
# File 'lib/okf/cli/render.rb', line 17

def self.help_rows
  [
    [ "render    <dir|@slug> [-o FILE] [--layout NAME] [...]", "write a static, self-contained HTML graph" ]
  ]
end

.idObject



9
10
11
# File 'lib/okf/cli/render.rb', line 9

def self.id
  :render
end

Instance Method Details

#call(argv) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/okf/cli/render.rb', line 23

def call(argv)
  require "okf/render/graph"

  options = { output: nil, title: nil, link: nil, layout: "cose" }
  parser = OptionParser.new do |o|
    o.banner = "Usage: okf render <dir|@slug> [-o FILE] [--layout NAME] [-t title] [-l url]"
    o.on("-o", "--output FILE", "write to FILE instead of stdout") { |v| options[:output] = v }
    o.on("-t", "--title TITLE", "graph title (default: parent/bundle dir name)") { |v| options[:title] = v }
    o.on("-l", "--link URL", "source URL shown in the header") { |v| options[:link] = v }
    o.on("--layout NAME", OKF::Render::Graph::LAYOUTS, "initial layout (#{OKF::Render::Graph::LAYOUTS.join(", ")})") { |v| options[:layout] = v }
    help_flag(o)
  end
  dir = positional_dir(parser, argv) or return 2

  folder = OKF::Bundle::Folder.load(dir)
  report_skipped(folder)
  html = OKF::Render::Graph.static(folder, title: options[:title], link: options[:link], layout: options[:layout])
  if options[:output]
    # A bad -o path (a missing directory, a permission denial) is a bad
    # *argument*: exit 2 with the reason, never a backtrace and an exit code
    # that means "failing bundle".
    begin
      File.write(options[:output], html)
    rescue SystemCallError => e
      return usage_error("cannot write #{options[:output]}: #{e.message}")
    end
    # Off the bundle, not a second graph: Graph.build maps one node per
    # concept, so the counts are identical — and Folder#graph is not
    # memoized, so asking for one here would build a whole second graph
    # (Render::Graph.static already built one) to print one number. Only
    # the graph, to be exact: the concepts are parsed once at Folder.load
    # and Graph.build reads them from memory, so this costs no disk.
    count = folder.bundle.concepts.size
    @out.puts "wrote #{count} #{pluralize(count, "concept")} to #{options[:output]}"
  else
    @out.print html
  end
  0
end