Last active
October 4, 2023 20:16
-
-
Save itarato/85c8bed23da1610ca7c8c1fc549a79d3 to your computer and use it in GitHub Desktop.
Notes to Dot converter
This file contains 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
note = File.read(ARGV[0]) | |
gid = 1 | |
prev_indent = 0 | |
stack = [0] | |
labels = ["Root"] | |
edges = [] | |
note.lines.map { _1.delete!("\n") }.map do |line| | |
indent = /^ */.match(line)[0].size | |
current_gid = gid | |
gid += 1 | |
labels[current_gid] = line.gsub(/^ *- ?/, '') | |
if indent > prev_indent # New subtree | |
stack.push(current_gid - 1) | |
elsif indent < prev_indent # Back to parent | |
((prev_indent - indent) / 2).times { stack.pop } | |
end | |
edges.push([stack.last, current_gid]) | |
prev_indent = indent | |
end | |
# p labels | |
# p edges | |
puts("digraph {") | |
labels.each_with_index do |label, i| | |
puts("\t#{i}[label=\"#{label}\"]") | |
end | |
puts | |
edges.each do |(from, to)| | |
puts("\t#{from} -> #{to}") | |
end | |
puts("}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment