Module: Mermaid::FlowchartDiagram::Parser::ClassMethods

Defined in:
lib/mermaid/flowchart_diagram/parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(input) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mermaid/flowchart_diagram/parser.rb', line 8

def parse(input)
  lines = input.split("\n")
  _, direction = lines.shift.split
  nodes = []
  links = []
  lines.each do |line|
    if line =~ /^(\S+)(?:\[(.*)\]|\((.*)\)|\{(.*)\})/
      id = $1
      label = $2 || $3 || $4
      shape = $2 ? 'box' : $3 ? 'round' : 'decision'
      nodes << Node.new(id: id, label: label, shape: shape)
    elsif line =~ /^(\S+)\s+-->(?:\|(.*)\|\s+)?(\S+)/
      from = $1
      label = $2
      to = $3
      links << Link.new(from: from, to: to, label: label)
    end
  end
  new(id: 'parsed', direction: direction, nodes: nodes, links: links)
end