Skip to content

Instantly share code, notes, and snippets.

@caius
Created November 29, 2017 18:39
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 caius/f770a6a03a163b44fa6d97a233a0b3b2 to your computer and use it in GitHub Desktop.
Save caius/f770a6a03a163b44fa6d97a233a0b3b2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "pathname"
require "kramdown"
plan = Pathname.new("../plan.md").expand_path(__dir__)
output_dir = Pathname.new("../output").expand_path(__dir__)
output_dir.rmtree if output_dir.exist?
output_dir.mkdir
raw_plan = plan.read
md_output = []
graph = []
graph_counter = 1
raw_plan.split("\n").each do |line|
if (line =~ /^```dot/) .. (line =~ /```\z/)
# Don't save markers
next if line.start_with?("```")
graph << line
next
end
# Just finished grabbing a graph
unless graph.empty?
graph_dot = output_dir.join("graph_#{graph_counter}.dot")
graph_png = graph_dot.sub_ext(".png")
graph_dot.open("w+") do |g|
g.puts graph
end
# Generate graph
%x[dot -Tpng #{graph_dot} > #{graph_png}]
graph_dot.delete
# Insert image tag for it into master doc
md_output << " ![](#{graph_png.basename})"
md_output << ""
# Reset for next graph
graph_counter += 1
graph = []
next
end
# Just a normal line
md_output << line
end
output_dir.join("migration_plan.md").open("w+") do |plan|
plan.puts md_output
end
puts "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment