Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created October 14, 2014 15:12
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garybernhardt/e6beb0c9d2ad94616969 to your computer and use it in GitHub Desktop.
Save garybernhardt/e6beb0c9d2ad94616969 to your computer and use it in GitHub Desktop.
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
Copy link

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