| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | App::Basis::ConvertText2::Plugin::Mscgen | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | my $content = "# MSC for some fictional process | ||
| 9 | msc { | ||
| 10 | a,b,c; | ||
| 11 | |||
| 12 | a->b [ label = "ab()" ] ; | ||
| 13 | b->c [ label = "bc(TRUE)"]; | ||
| 14 | c=>c [ label = "process(1)" ]; | ||
| 15 | c=>c [ label = "process(2)" ]; | ||
| 16 | ...; | ||
| 17 | c=>c [ label = "process(n)" ]; | ||
| 18 | c=>c [ label = "process(END)" ]; | ||
| 19 | a<<=c [ label = "callback()"]; | ||
| 20 | --- [ label = "If more to run", ID="*" ]; | ||
| 21 | a->a [ label = "next()"]; | ||
| 22 | a->c [ label = "ac1()\nac2()"]; | ||
| 23 | b<-c [ label = "cb(TRUE)"]; | ||
| 24 | b->b [ label = "stalled(...)"]; | ||
| 25 | a<-b [ label = "ab() = FALSE"]; | ||
| 26 | }" ; | ||
| 27 | my $params = {} ; | ||
| 28 | my $obj = App::Basis::ConvertText2::Plugin::Mscgen->new() ; | ||
| 29 | my $out = $obj->process( 'mscgen', $content, $params) ; | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | convert a mscgen text string into a PNG, requires mscgen program from http://www.mcternan.me.uk/mscgen/ | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | # ---------------------------------------------------------------------------- | ||
| 38 | |||
| 39 | package App::Basis::ConvertText2::Plugin::Mscgen; | ||
| 40 | $App::Basis::ConvertText2::Plugin::Mscgen::VERSION = '0.4'; | ||
| 41 | use 5.10.0; | ||
| 42 | use strict; | ||
| 43 | use warnings; | ||
| 44 | use Image::Resize; | ||
| 45 | use Moo; | ||
| 46 | use Path::Tiny; | ||
| 47 | use App::Basis; | ||
| 48 | use App::Basis::ConvertText2::Support; | ||
| 49 | use namespace::autoclean; | ||
| 50 | |||
| 51 | has handles => ( | ||
| 52 | is => 'ro', | ||
| 53 | init_arg => undef, | ||
| 54 | default => sub { [qw{mscgen}] } | ||
| 55 | ); | ||
| 56 | |||
| 57 | use constant MSCGEN => 'mscgen'; | ||
| 58 | |||
| 59 | # ---------------------------------------------------------------------------- | ||
| 60 | |||
| 61 | =item mscgen | ||
| 62 | |||
| 63 | create a simple msc image | ||
| 64 | |||
| 65 | parameters | ||
| 66 | data - msc text | ||
| 67 | filename - filename to save the created image as | ||
| 68 | |||
| 69 | hashref params of | ||
| 70 | size - size of image, widthxheight - optional | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub process { | ||
| 75 | my $self = shift; | ||
| 76 | my ( $tag, $content, $params, $cachedir ) = @_; | ||
| 77 | $params->{size} ||= ""; | ||
| 78 | my ( $x, $y ) = ( $params->{size} =~ /^\s*(\d+)\s*x\s*(\d+)\s*$/ ); | ||
| 79 | $params->{title} ||= ""; | ||
| 80 | |||
| 81 | # strip any ending linefeed | ||
| 82 | chomp $content; | ||
| 83 | return "" if ( !$content ); | ||
| 84 | |||
| 85 | # we can use the cache or process everything ourselves | ||
| 86 | my $sig = create_sig( $content, $params ); | ||
| 87 | my $filename = cachefile( $cachedir, "$sig.png" ); | ||
| 88 | if ( !-f $filename ) { | ||
| 89 | my $mscfile = Path::Tiny->tempfile("mscgen.XXXX"); | ||
| 90 | path($mscfile)->spew_utf8($content); | ||
| 91 | my $cmd = MSCGEN . " -Tpng -o$filename $mscfile"; | ||
| 92 | my ( $exit, $stdout, $stderr ) = run_cmd($cmd); | ||
| 93 | if ($exit) { | ||
| 94 | warn "Could not run script " . MSCGEN . " get it from http://www.mcternan.me.uk/mscgen/"; | ||
| 95 | } | ||
| 96 | |||
| 97 | # if we want to force the size of the graph | ||
| 98 | if ( -f $filename && $x && $y ) { | ||
| 99 | my $image = Image::Resize->new($filename); | ||
| 100 | my $gd = $image->resize( $x, $y ); | ||
| 101 | |||
| 102 | # overwrite original file with resized version | ||
| 103 | if ($gd) { | ||
| 104 | path($filename)->spew_raw( $gd->png ); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | my $out; | ||
| 110 | if ( -f $filename ) { | ||
| 111 | |||
| 112 | # create something suitable for the HTML | ||
| 113 | $out = create_img_src( $filename, $params->{title} ); | ||
| 114 | } | ||
| 115 | return $out; | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | # ---------------------------------------------------------------------------- | ||
| 120 | |||
| 121 | 1; |