ConvertText2/Plugin/Graphviz.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 105 CC 8 SCAR 35.0
CC
8
Cov
0%
process
72.0
BEGIN
2.0
BEGIN
2.0
CRAP 33.1
1  
2 =head1 NAME
3  
4 App::Basis::ConvertText2::Plugin::Graphviz
5  
6 =head1 SYNOPSIS
7  
8 my $content = "digraph G {
9 subgraph cluster_0 {
10 style=filled;
11 color=lightgrey;
12 node [style=filled,color=white];
13 a0 -> a1 -> a2 -> a3;
14 label = "process #1";
15 }
16  
17 subgraph cluster_1 {
18 node [style=filled];
19 b0 -> b1 -> b2 -> b3;
20 label = "process #2";
21 color=blue
22 }
23 start -> a0;
24 start -> b0;
25 a1 -> b3;
26 b2 -> a3;
27 a3 -> a0;
28 a3 -> end;
29 b3 -> end;
30  
31 start [shape=Mdiamond];
32 end [shape=Msquare];
33 }" ;
34 my $params = {
35 size => "600x480",
36 } ;
37 my $obj = App::Basis::ConvertText2::Plugin::Graphviz->new() ;
38 my $out = $obj->process( 'graphviz', $content, $params) ;
39  
40 =head1 DESCRIPTION
41  
42 convert a graphviz text string into a PNG, requires dot program from http://graphviz.org
43  
44 =cut
45  
46 # ----------------------------------------------------------------------------
47  
48 package App::Basis::ConvertText2::Plugin::Graphviz;
49 $App::Basis::ConvertText2::Plugin::Graphviz::VERSION = '0.4';
50 use 5.10.0;
51 use strict;
52 use warnings;
53 use Moo;
54 use Path::Tiny;
55 use App::Basis;
56 use App::Basis::ConvertText2::Support;
57 use namespace::autoclean;
58  
59 has handles => (
60 is => 'ro',
61 init_arg => undef,
62 default => sub { [qw{graphviz dot}] }
63 );
64  
65 # ----------------------------------------------------------------------------
66 use constant GRAPHVIZ => 'dot';
67 use constant DPI => 72;
68  
69 # ----------------------------------------------------------------------------
70  
71 =item graphviz
72  
73 create a simple graphviz structured graph image, from the passed text
74  
75 parameters
76 data - graphviz text
77 filename - filename to save the created image as
78  
79 hashref params of
80 size - size of image, widthxheight - optional
81  
82 =cut
83  
84 sub process {
85 my $self = shift;
86 my ( $tag, $content, $params, $cachedir ) = @_;
87 my $size = "";
88 $params->{size} ||= "";
89 my ( $x, $y ) = ( $params->{size} =~ /^\s*(\d+)\s*x\s*(\d+)\s*$/ );
90  
91 # strip any ending linefeed
92 chomp $content;
93 return "" if ( !$content );
94  
95 # we can use the cache or process everything ourselves
96 my $sig = create_sig( $content, $params );
97 my $filename = cachefile( $cachedir, "$sig.png" );
98 if ( !-f $filename ) {
99  
100 my $dotfile = Path::Tiny->tempfile("graphviz.XXXX");
101  
102 if ( $x && $y ) {
103 $size = sprintf( " size=\"%.5f,%.5f\";", $x / DPI, $y / DPI );
104  
105 # add calculated image size to the graph
106 $content =~ s/(digraph.*?)$/$1\n$size\n/sm;
107 }
108  
109 path($dotfile)->spew_utf8($content);
110 my $cmd = GRAPHVIZ . " -Tpng -o$filename $dotfile";
111 my ( $exit, $stdout, $stderr ) = run_cmd($cmd);
112 if ($exit) {
113 warn "Could not run script " . GRAPHVIZ . " get it from http://graphviz.org";
114 }
115  
116 # if we want to force the size of the graph
117 if ( -f $filename && $x && $y ) {
118 my $image = Image::Resize->new($filename);
119 my $gd = $image->resize( $x, $y );
120  
121 # overwrite original file with resized version
122 if ($gd) {
123 path($filename)->spew_raw( $gd->png );
124 }
125 }
126 }
127  
128 my $out;
129 if ( -f $filename ) {
130  
131 # create something suitable for the HTML
132 $out = create_img_src( $filename, $params->{title} );
133 }
134 return $out;
135 }
136  
137 # ----------------------------------------------------------------------------
138  
139 1;