| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | App::Basis::ConvertText2::Plugin::Uml | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | my $content = "' this is a comment on one line | ||
| 9 | /' this is a | ||
| 10 | multi-line | ||
| 11 | comment'/ | ||
| 12 | Alice -> Bob: Authentication Request | ||
| 13 | Bob --> Alice: Authentication Response | ||
| 14 | |||
| 15 | Alice -> Bob: Another authentication Request | ||
| 16 | Alice <-- Bob: another authentication Response | ||
| 17 | " ; | ||
| 18 | my $params = {} ; | ||
| 19 | my $obj = App::Basis::ConvertText2::Plugin::Uml->new() ; | ||
| 20 | my $out = $obj->process( 'uml', $content, $params) ; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | convert a uml text string into a PNG, requires uml program and plantuml | ||
| 25 | from https://github.com/27escape/bin/blob/master/uml and http://plantuml.sourceforge.net | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | # ---------------------------------------------------------------------------- | ||
| 30 | |||
| 31 | package App::Basis::ConvertText2::Plugin::Uml; | ||
| 32 | $App::Basis::ConvertText2::Plugin::Uml::VERSION = '0.4'; | ||
| 33 | use 5.10.0; | ||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | use Path::Tiny; | ||
| 37 | use Moo; | ||
| 38 | use App::Basis; | ||
| 39 | use App::Basis::ConvertText2::Support; | ||
| 40 | use namespace::autoclean; | ||
| 41 | |||
| 42 | has handles => ( | ||
| 43 | is => 'ro', | ||
| 44 | init_arg => undef, | ||
| 45 | default => sub {[qw{uml}]} | ||
| 46 | ); | ||
| 47 | |||
| 48 | # uml is a script to run plantuml basically does java -jar plantuml.jar | ||
| 49 | use constant UML => "uml"; | ||
| 50 | |||
| 51 | # ---------------------------------------------------------------------------- | ||
| 52 | |||
| 53 | =item uml | ||
| 54 | |||
| 55 | create a simple uml image | ||
| 56 | |||
| 57 | parameters | ||
| 58 | data - uml text | ||
| 59 | filename - filename to save the created image as | ||
| 60 | |||
| 61 | hashref params of | ||
| 62 | size - size of image, widthxheight - optional | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub process { | ||
| 67 | my $self = shift; | ||
| 68 | my ( $tag, $content, $params, $cachedir ) = @_; | ||
| 69 | $params->{size} ||= ""; | ||
| 70 | my ( $x, $y ) = ( $params->{size} =~ /^\s*(\d+)\s*x\s*(\d+)\s*$/ ); | ||
| 71 | |||
| 72 | # strip any ending linefeed | ||
| 73 | chomp $content; | ||
| 74 | return "" if ( !$content ); | ||
| 75 | |||
| 76 | # we can use the cache or process everything ourselves | ||
| 77 | my $sig = create_sig( $content, $params ); | ||
| 78 | my $filename = cachefile( $cachedir, "$sig.png" ); | ||
| 79 | if ( !-f $filename ) { | ||
| 80 | |||
| 81 | $content = "\@startuml\n$content" if ( $content !~ /\@startuml/ ); | ||
| 82 | $content .= "\n\@enduml" if ( $content !~ /\@enduml/ ); | ||
| 83 | |||
| 84 | # we are lucky that plantuml can have image sizes | ||
| 85 | if ( $x && $y ) { | ||
| 86 | $content =~ s/\@startuml/\@startuml\nscale $x*$y\n/; | ||
| 87 | } | ||
| 88 | my $umlfile = Path::Tiny->tempfile("umlXXXXXXXX"); | ||
| 89 | |||
| 90 | path($umlfile)->spew_utf8($content); | ||
| 91 | |||
| 92 | my $cmd = UML . " $umlfile $filename"; | ||
| 93 | my ( $exit, $stdout, $stderr ) = run_cmd($cmd); | ||
| 94 | if( $exit) { | ||
| 95 | warn "Could not run script " . UML . " get it from https://github.com/27escape/bin/blob/master/uml" ; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | my $out; | ||
| 99 | if ( -f $filename ) { | ||
| 100 | |||
| 101 | # create something suitable for the HTML | ||
| 102 | $out = create_img_src( $filename, $params->{title} ); | ||
| 103 | } | ||
| 104 | |||
| 105 | return $out; | ||
| 106 | } | ||
| 107 | |||
| 108 | # ---------------------------------------------------------------------------- | ||
| 109 | |||
| 110 | 1; |