blib/lib/Kwiki/MindMap.pm untested

filter: Statementstmt 0.0% 0 / 138 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 211 CC 17 SCAR 22.8
CC
17
Cov
0%
read_pageconf
20.0
render_mindmap
20.0
hash2graph
20.0
CRAP 9.8
1 package Kwiki::MindMap;
2 use Kwiki::Plugin -Base;
3 use YAML;
4  
5 our $VERSION = '0.09';
6  
7 const class_id => 'mindmap';
8 const class_title => 'MindMap Blocks';
9 const screen_template => 'site_mindmap_screen.html';
10  
11 field prefix => '[*+0=]';
12 field fontsize => 12;
13 field fontname => 'Times';
14  
15 sub register {
16 my $registry = shift;
17 $registry->add(wafl => mindmap => 'Kwiki::MindMap::Wafl');
18 }
19  
20 package Kwiki::MindMap::Wafl;
21 use base 'Spoon::Formatter::WaflBlock';
22 use Digest::MD5;
23 use GraphViz;
24  
25 sub to_html {
26 $self->cleanup;
27 $self->read_pageconf;
28 $self->render_mindmap($self->units->[0]);
29 }
30  
31 # XXX: I think cleanup should be called ony once per-page.
32 # (If a page is modified, re-generate all the mindmap inside)
33 # but put it in here will make it be called once per-mindmap-block.
34 sub cleanup {
35 my $path = $self->hub->mindmap->plugin_directory;
36 my $page =$self->hub->pages->current;
37 my $page_id = $page->id;
38 for(<$path/$page_id/*.png>) {
39 my $mt = (stat($_))[9];
40 unlink($_) if $mt < $page->modified_time;
41 }
42 }
43  
44 sub read_pageconf {
45 if(my $obj = $self->hub->load_class('config_blocks')) {
46 my $conf = $obj->pageconf;
47 $self->hub->mindmap->fontsize($conf->{mindmap_fontsize}) if defined $conf->{mindmap_fontsize};
48 $self->hub->mindmap->fontname($conf->{mindmap_fontname}) if defined $conf->{mindmap_fontname};
49 }
50 }
51  
52 # use md5 as filename because I don't want to regenerate all the graphs
53 # on every page rendering. That's totally a waste of time.
54 sub render_mindmap {
55 my $reldump = shift;
56 $reldump =~ s/^\s+$//gm;
57 $reldump =~ s/\n\n+/\n/gs;
58 my $page = $self->hub->pages->current->id;
59 my $digest = Digest::MD5::md5_hex($reldump);
60 my $path = $self->hub->mindmap->plugin_directory;
61 my ($title,$tree) = $self->load_mindmap($reldump);
62 my $file = io->catfile($path,$page,"$digest.png")->assert;
63 my $svg = io->catfile($path,$page,"$digest.svg")->assert;
64 unless(-f "$file") {
65 my $grvz = $self->hash2graph($tree,$title);
66 $grvz->as_png > $file;
67 $grvz->as_svg > $svg;
68 }
69 if(my $obj = $self->hub->load_class('config_blocks')) {
70 my $conf = $obj->pageconf;
71 if(defined $conf->{mindmap_as_svg}) {
72 my $width = $conf->{mindmap_svg_width} || 800;
73 my $height = $conf->{mindmap_svg_height} || $width;
74 return qq{<embed src="$svg" name="$title" width="$width" height="$height" type="image/svg-xml" pluginspage="http://www.adobe.com/svg/viewer/install/">};
75 }
76 }
77 return qq{<img src="$file">};
78 }
79  
80 my $hue = 0;
81  
82 sub hash2graph {
83 my ($tree,$title) = @_;
84 my $g = GraphViz->new(graph => {rankdir=>"LR", ranksep=>'0.05', nodesep=>'0.10'},
85 node => {label=>"",width=>0.04,height=>0.04,style=>'filled',fixedsize=>'true'},
86 edge => {arrowhead=>'none',style=>"setlinewidth(2)",
87 tailclip=>'false',headclip=>'false',
88 fontsize=> $self->hub->mindmap->fontsize,fontname=>$self->hub->mindmap->fontname },
89 );
90  
91 $g->add_node('root_left',label=>'');
92 $g->add_node('root_right',label=>'');
93 $g->add_edge('root_right' => 'root_left', label => $title);
94  
95 my @kids = sort { $self->count_kids($tree->{$a}) <=> $self->count_kids($tree->{$b}) } keys %$tree;
96 my $hue_step = 1 / @kids;
97 my (@left, @right);
98 while (@kids) {
99 push @left, shift @kids;
100 push @right, shift @kids if @kids;
101 push @right, pop @kids if @kids;
102 push @left, pop @kids if @kids;
103 }
104  
105 foreach my $left (@left) {
106 $self->draw_left_kids($g,"root_left","root_left/$left",$tree->{$left});
107 $hue += $hue_step;
108 }
109  
110 foreach my $right (@right) {
111 $self->draw_right_kids($g,"root_right","root_right/$right",$tree->{$right});
112 $hue += $hue_step;
113 }
114  
115 $hue = 0;
116 return $g;
117 }
118  
119 sub load_mindmap {
120 my $dump = shift;
121 my @lines = split/\n/,$dump;
122 my $title = shift @lines;
123 my $tree = $self->load_mindmap_subtree(@lines);
124 return ($title,$tree);
125 }
126  
127 sub load_mindmap_subtree {
128 my @lines = @_;
129  
130 return '' unless(defined($lines[0]));
131  
132 my $tree = {};
133 my @scope = $self->subtree_scopes(@lines);
134 my $i = 0;
135 my $prefix = $self->hub->mindmap->prefix;
136 while($i < @scope) {
137 my $node = $lines[$i]; $node =~ s/^($prefix)+\s*//;
138 $tree->{$node} = $self->load_mindmap_subtree(@lines[$i+1..$scope[$i]]);
139 $i = $scope[$i] + 1;
140 }
141 return $tree;
142 }
143  
144 sub subtree_scopes {
145 my @lines = @_;
146 my @levels = $self->all_node_level(@lines);
147 my @scope;
148  
149 # O(n^2). Could be better.
150 for my $i (0..$#lines) {
151 $scope[$i] = $i;
152 for my $j ($i+1..$#lines) {
153 if($levels[$j] > $levels[$i]) {
154 $scope[$i] = $j
155 } else {
156 last;
157 }
158 }
159 }
160 return @scope;
161 }
162  
163 sub all_node_level {
164 my @lines = @_;
165 my @levels;
166 for(@lines) {
167 push @levels, $self->node_level($_);
168 }
169 return @levels;
170 }
171  
172 sub node_level {
173 my $line = shift;
174 my $prefix = $self->hub->mindmap->prefix;
175 if ($line =~ /^(($prefix)+)/) {
176 return length($1);
177 } else {
178 return -1;
179 }
180 }
181  
182 sub draw_left_kids {
183 my ($graph, $parent_symbol,$this_symbol,$tree) = @_;
184 $graph->add_node($this_symbol, label=>'',color=>"$hue,1,1");
185 $graph->add_edge($parent_symbol => $this_symbol,
186 color => "$hue,1,1",
187 label => (split('/',$this_symbol))[-1],
188 );
189 return unless (ref($tree) eq 'HASH');
190 foreach my $kid (keys %$tree) {
191 $self->draw_left_kids($graph,$this_symbol, "$this_symbol/$kid", $tree->{$kid});
192 }
193 }
194  
195 sub draw_right_kids {
196 my ($graph, $parent_symbol,$this_symbol,$tree) = @_;
197 $graph->add_node($this_symbol, label=>'',color=>"$hue,1,1");
198 $graph->add_edge($this_symbol => $parent_symbol,
199 color => "$hue,1,1",
200 label => (split('/',$this_symbol))[-1],
201 );
202 return unless (ref($tree) eq 'HASH');
203 foreach my $kid (keys %$tree) {
204 $self->draw_right_kids($graph,$this_symbol, "$this_symbol/$kid", $tree->{$kid});
205 }
206 }
207  
208  
209 sub count_kids {
210 my $root = shift;
211 return 0 unless ref($root) eq 'HASH';
212  
213 my @kids = keys %$root;
214 my $count = @kids;
215 foreach my $kid (@kids) {
216 $count += $self->count_kids($root->{$kid});
217 }
218  
219 return $count;
220 }
221  
222 __END__