Last active
May 8, 2023 08:09
-
-
Save SteefH/75de8d3d87fd1616ecb2b1376d50448e to your computer and use it in GitHub Desktop.
Silly conversion script: graphviz-digraph-to-mermaid-flowchart.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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