scratch/lib/graph.rb

30 lines
1.2 KiB
Ruby
Raw Normal View History

2010-05-31 19:29:57 +00:00
class Graph < Nanoc3::Filter
identifier :graph
2010-06-24 05:43:49 +00:00
@@tmpfic="/tmp/graphtemp.dot"
2010-05-31 19:29:57 +00:00
def run(content, params={})
2010-09-21 14:52:25 +00:00
content.gsub(%r{<graph( title="([^"]*)")?>(.+?)</graph>}m) do |full|
2010-06-25 22:43:16 +00:00
# FileUtils.rm(@@tmpfic)
2010-09-21 14:52:25 +00:00
title=$2
str=$3
2010-05-31 19:29:57 +00:00
filename=title.gsub(/[^a-zA-Z0-9_]/,"_")
2010-06-24 05:43:49 +00:00
File.open(@@tmpfic,'w') do |f|
f.write %[ digraph Source {
2010-05-31 19:29:57 +00:00
fontsize=10;
rankdir="LR";
2011-12-07 15:40:03 +00:00
graph [truecolor bgcolor="#FAFAFA00"];
node [width=0, height=0, fontname="Optima Bold", shape="box", color="#333333", style="filled" fillcolor="#FAFAFA", fontcolor="#333333"] ;
edge [arrowsize=.5, color="#333333"] ; ]
2010-06-24 05:43:49 +00:00
f.write str
f.write %[}]
end
FileUtils.mkdir_p('output'+@item.path+'graph')
2010-05-31 19:29:57 +00:00
webpath=@item.path + 'graph/' + filename + '.png'
path='output'+webpath
2011-05-30 12:12:24 +00:00
# puts "dot -Tpng -o #{path} /tmp/graphtemp.dot"
2010-05-31 19:29:57 +00:00
system("dot -Tpng -o #{path} /tmp/graphtemp.dot")
2011-05-30 12:12:24 +00:00
# puts %{<img alt="#{title}" src="#{webpath}"/>}
2010-05-31 19:29:57 +00:00
%{<img alt="#{title}" src="#{webpath}"/>}
end
end
end