| 1 | package Muster::Hook::GraphViz; | ||||||||||||||||||||||||||||
| 2 | 1 | $Muster::Hook::GraphViz::VERSION = '0.96'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Muster::Hook::Directives'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Muster::LeafFile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Muster::Hooks; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Digest::SHA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Muster::Hook::GraphViz - Muster graph directive using GraphViz. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | version 0.96 | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | L<Muster::Hook::GraphViz> directed graphs with graphviz. | ||||||||||||||||||||||||||||
| 22 | This supports a subset of the IkiWiki plugin's functionality. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 METHODS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | L<Muster::Hook::GraphViz> inherits all methods from L<Muster::Hook::Directives>. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 register | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Do some intialization. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 35 | 0 | my $hookmaster = shift; | |||||||||||||||||||||||||||
| 36 | 0 | my $conf = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | $self->{metadb} = $hookmaster->{metadb}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # place to store and serve cached graphs | ||||||||||||||||||||||||||||
| 41 | 0 | $self->{cache_dir} = $conf->{cache_dir}; | |||||||||||||||||||||||||||
| 42 | 0 | $self->{graphs_dir} = File::Spec->catdir($self->{cache_dir}, 'graphs'); | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | if (!-d $self->{graphs_dir}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||
| 45 | 0 | mkdir $self->{graphs_dir}; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 0 | $self->{img_url} = 'graphs/'; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $callback = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | return $self->process(%args); | |||||||||||||||||||||||||||
| 53 | 0 | }; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | $hookmaster->add_hook('graph' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | return $self->do_directives( | |||||||||||||||||||||||||||
| 58 | directive=>'graph', | ||||||||||||||||||||||||||||
| 59 | call=>$callback, | ||||||||||||||||||||||||||||
| 60 | %args, | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||
| 63 | 0 | ); | |||||||||||||||||||||||||||
| 64 | 0 | return $self; | |||||||||||||||||||||||||||
| 65 | } # register | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 process | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Process graph formatting. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my $leaf = $args{leaf}; | |||||||||||||||||||||||||||
| 77 | 0 | my $phase = $args{phase}; | |||||||||||||||||||||||||||
| 78 | 0 | my @p = @{$args{params}}; | |||||||||||||||||||||||||||
| 79 | 0 | my %params = @p; | |||||||||||||||||||||||||||
| 80 | 0 | my $pagename = $leaf->pagename; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # don't show anything if we aren't building | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($phase ne $Muster::Hooks::PHASE_BUILD) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | 0 | return ""; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $params{type} = "digraph" unless defined $params{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $params{prog} = "dot" unless defined $params{prog}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | croak "prog '$params{prog}' not a valid graphviz program" unless $params{prog} =~ /^(dot|neato|fdp|twopi|circo)$/o; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my $src = "charset=\"utf-8\";\n"; | |||||||||||||||||||||||||||
| 93 | $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n" | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if defined $params{width} and defined $params{height}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $params{"width"} and defined $params{"height"}
MC/DC0%0/2
defined $params{"width"} and defined $params{"height"}
defined $params{"width"}
defined $params{"height"}
Truth table0%0/3
A and B
A= defined $params{"width"}B= defined $params{"height"}
| |||||||||||||||||||||||||||||
| 95 | 0 | $src .= $params{src}; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Use the sha1 of the graphviz code as part of its filename, | ||||||||||||||||||||||||||||
| 98 | # and as a unique identifier | ||||||||||||||||||||||||||||
| 99 | 0 | my $sha=Digest::SHA::sha1_hex($params{type}.$src); | |||||||||||||||||||||||||||
| 100 | 0 | $src = "$params{type} graph$sha {\n".$src."}\n"; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | my $dest = $pagename; | |||||||||||||||||||||||||||
| 103 | 0 | $dest =~ s!/!-!g; | |||||||||||||||||||||||||||
| 104 | 0 | $dest=$dest."-graph-".$sha.".png"; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | my $outfile = File::Spec->catfile($self->{graphs_dir}, $dest); | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (! -e $outfile) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||
| 109 | 0 | my $cmd="$params{prog} -Tpng -o '$outfile'"; | |||||||||||||||||||||||||||
| 110 | 0 | my $fh; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | open($fh, "|-", $cmd) or die "ERROR: $cmd : $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | print $fh $src; | |||||||||||||||||||||||||||
| 114 | 0 | close $fh; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 0 | my $imglink = File::Spec->abs2rel($self->{img_url} . $dest, $leaf->pagename); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return <<EOT; | |||||||||||||||||||||||||||
| 119 | <img src="$imglink" alt="$sha"/> | ||||||||||||||||||||||||||||
| 120 | EOT | ||||||||||||||||||||||||||||
| 121 | } # process | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | 1; | |||||||||||||||||||||||||||