class SyntaxTree::Mermaid::FlowChart

This is the main class that handles rendering a flowchart. It keeps track of its nodes and links and renders them according to the mermaid syntax.

Attributes

nodes[R]
output[R]
prefix[R]

Public Class Methods

new() click to toggle source
# File lib/syntax_tree/mermaid.rb, line 15
def initialize
  @output = StringIO.new
  @output.puts("flowchart TD")
  @prefix = "  "

  @nodes = {}
  @links = []
end

Public Instance Methods

fetch(id) click to toggle source

Retrieve a node that has already been added to the flowchart by its id.

# File lib/syntax_tree/mermaid.rb, line 25
def fetch(id)
  nodes.fetch(id)
end
node(id, label = " ", shape: :rectangle) click to toggle source

Add a node to the flowchart with an optional label.

# File lib/syntax_tree/mermaid.rb, line 39
def node(id, label = " ", shape: :rectangle)
  node = Node.new(id, label, shape)
  nodes[id] = node

  output.puts("#{prefix}#{nodes[id].render}")
  node
end
render() click to toggle source

Return the rendered flowchart.

# File lib/syntax_tree/mermaid.rb, line 64
def render
  links.each_with_index do |link, index|
    if link.color
      output.puts("#{prefix}linkStyle #{index} stroke:#{link.color}")
    end
  end

  output.string
end
subgraph(label) { || ... } click to toggle source

Add a subgraph to the flowchart. Within the given block, all of the nodes will be rendered within the subgraph.

# File lib/syntax_tree/mermaid.rb, line 49
def subgraph(label)
  output.puts("#{prefix}subgraph #{Mermaid.escape(label)}")

  previous = prefix
  @prefix = "#{prefix}  "

  begin
    yield
  ensure
    @prefix = previous
    output.puts("#{prefix}end")
  end
end