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