Skip to content

Instantly share code, notes, and snippets.

@itarato
Last active October 4, 2023 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itarato/85c8bed23da1610ca7c8c1fc549a79d3 to your computer and use it in GitHub Desktop.
Save itarato/85c8bed23da1610ca7c8c1fc549a79d3 to your computer and use it in GitHub Desktop.
Notes to Dot converter
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