Module: Markdoc::Pseudocode

Defined in:
lib/markdoc/pseudocode.rb

Defined Under Namespace

Classes: ActionLiteral, ExpressionLiteral, IfLiteral, Register, SentenceLiteral

Class Method Summary collapse

Class Method Details

.draw(code) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/markdoc/pseudocode.rb', line 103

def self.draw(code)
  parser = PseudocodeParser.new
  tree = parser.parse(code)

  if(tree.nil?)
    puts parser.failure_reason
    raise "Can't generate graphviz code"
  else
    digest = Digest::MD5.hexdigest code

    graphviz = nil
    Tempfile.open([digest, '.gv']) do |file|
      file.write "digraph G {\n"
      tree.out(file)
      file.write "}\n"
      graphviz = file.path
    end

    image = Tempfile.new([digest, '.svg'])
    image.close

    if system("dot -n -Tsvg -o#{image.path} #{graphviz}")
      IO.read image
    else
      raise "Can't generate flowchart"
    end
  end
end