def graphviz( *args, &block )
opts = args.last.instance_of?(Hash) ? args.pop : {}
text = capture_erb(&block)
return if text.empty?
err = Tempfile.new('graphviz_err')
err.close
defaults = ::Webby.site.graphviz
path = opts.getopt(:path, defaults.path)
cmd = opts.getopt(:cmd, defaults.cmd)
type = opts.getopt(:type, defaults.type)
name = text.match(%r/\A\s*(?:strict\s+)?(?:di)?graph\s+([A-Za-z_][A-Za-z0-9_]*)\s+\{/o)[1]
usemap = text.match(%r/(?:URL|href)\s*=/o) != nil
image_fn = path.nil? ? name.dup : ::File.join(path, name)
image_fn = ::File.join('', image_fn) << '.' << type
out = "<img src=\"#{image_fn}\""
%w[class style id alt].each do |atr|
val = opts.getopt(atr)
next if val.nil?
out << " %s=\"%s\"" % [atr, val]
end
out << " usemap=\"\##{name}\"" if usemap
out << " />\n"
if usemap
IO.popen("#{cmd} -Tcmapx 2> #{err.path}", 'r+') do |io|
io.write text
io.close_write
out << io.read
end
GraphvizHelper.error_check(err)
end
out_dir = ::Webby.site.output_dir
out_file = ::File.join(out_dir, image_fn)
FileUtils.mkpath(::File.join(out_dir, path)) unless path.nil?
cmd = "#{cmd} -T#{type} -o #{out_file} 2> #{err.path}"
IO.popen(cmd, 'w') {|io| io.write text}
GraphvizHelper.error_check(err)
out = _guard(out)
concat_erb(out, block.binding)
return
end