Skip to content

Instantly share code, notes, and snippets.

@SteefH
Last active May 8, 2023 08:09
Show Gist options
  • Save SteefH/75de8d3d87fd1616ecb2b1376d50448e to your computer and use it in GitHub Desktop.
Save SteefH/75de8d3d87fd1616ecb2b1376d50448e to your computer and use it in GitHub Desktop.
Silly conversion script: graphviz-digraph-to-mermaid-flowchart.rb
#!/usr/bin/env ruby
nodes = {}
edges = Hash.new { |hash, key| hash[key] = [] }
at_end = false
ARGF.map(&:strip).reject(&:empty?).each_with_index do |line, index|
case [index, line]
in 0, /digraph\s*\{/
in 1.., '}'
at_end = true
in 1.., /"(?<from_name>[^"]+)"\s*->\s*"(?<to_name>[^"]+)"\s*;/
raise "Expected end of file, got #{line.inspect}" if at_end
edges[$~[:from_name]] << $~[:to_name]
in 1.., /"(?<name>[^"]+)"\s*;/
raise "Expected end of file, got #{line.inspect}" if at_end
nodes[$~[:name]] = $~[:name].gsub("/", "_")
else
raise "Unexpected input #{line.inspect}"
end
end
puts '%%{init: {"flowchart": {"padding": 40} } }%%'
puts 'flowchart RL'
nodes.each do |k, v|
puts " #{v}([\"#{k}\"])"
end
puts ''
edges.each do |k, edges|
edges.each do |node|
puts " #{nodes[k]} --> #{nodes[node]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment