Created
October 14, 2014 15:12
-
-
Save garybernhardt/e6beb0c9d2ad94616969 to your computer and use it in GitHub Desktop.
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
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 | |
"" % [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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On line 32, why use
send :`` rather than than the regular backtick literal/just a call to
system`? (Not pedantry, a genuine question.)