Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require "erb"
require "pathname"
DOT_TEMPLATE=<<-END
digraph {
size="20,20";
overlap=false;
sep=0.4;
graph [fontname=Helvetica,fontsize=10];
node [fontname=Helvetica,fontsize=10];
edge [fontname=Helvetica,fontsize=10];
rankdir=TB;
edge [len=1.5];
node[shape=box];
%s
}
END
def slugify(text)
text.gsub(/[^a-zA-Z0-9_\-]+/, "-")
end
class Context
def graph(title, dot_source)
dot_source = DOT_TEMPLATE % dot_source
filename = slugify(title)
dot_path = "build/graphs/#{filename}.dot"
png_path = "build/graphs/#{filename}.png"
png_path_relative_to_build = "graphs/#{filename}.png"
File.write(dot_path, dot_source)
send :`, %{dot #{dot_path} -T png -o build/tmp.png}
exit(1) unless $?.success?
if !File.exist?(png_path) || File.read("build/tmp.png") != File.read(png_path)
puts "Overwriting #{png_path}"
File.write(png_path, File.read("build/tmp.png"))
end
"![%s](%s)" % [title, png_path_relative_to_build]
end
def binding
super
end
end
path = ARGV.fetch(0)
path = Pathname.new(path).relative_path_from(Pathname.new(Dir.getwd))
content = File.read(path)
context = Context.new
rendered = ERB.new(content).result(context.binding)
File.write("build/#{path}", rendered)
@robmiller

This comment has been minimized.

Copy link

robmiller commented Oct 14, 2014

On line 32, why use send :`` rather than than the regular backtick literal/just a call to system`? (Not pedantry, a genuine question.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.