class Graphdown::Parser

Attributes

graph[R]

Public Class Methods

new() click to toggle source
# File lib/graphdown/parser.rb, line 5
def initialize
  @graph = Graph.new
end

Public Instance Methods

output() click to toggle source
# File lib/graphdown/parser.rb, line 36
def output
  @graph.layer_nodes
  @graph.layout_nodes
  @graph.layout_edges
  @graph.to_svg
end
parse(text) click to toggle source
# File lib/graphdown/parser.rb, line 9
def parse(text)
  tokens = text.split(/(?<=[\]>])\s+/)
  2.step(tokens.size, 2) do |n|
    # origins
    origin_labels = tokens[n - 2].scan(/(?<=\[).+?(?=\])/)
    origins = origin_labels.map do |label|
      @graph.find_node_by_label(label) || Node.new(label)
    end

    # targets
    target_labels = tokens[n].scan(/(?<=\[).+?(?=\])/)
    targets = target_labels.map do |label|
      @graph.find_node_by_label(label) || Node.new(label)
    end

    # edges
    direction = (tokens[n - 1] == "<->") ? :two_way : :forward
    origins.each do |origin|
      @graph << origin
      targets.each do |target|
        origin.connect(target, direction)
        @graph << target
      end
    end
  end
end