#!/usr/bin/env perl

package main;
BEGIN {
  $main::AUTHORITY = 'cpan:DBR';
}
{
  $main::VERSION = '0.7.2';
}

#  PODNAME: ppp
# ABSTRACT: Preprocess Pandoc before Processing Pandoc

use v5.14;
use strict;
use warnings;

use FindBin;

use lib "$FindBin::Bin/../lib";

use Data::Printer;
# use Smart::Comments;

state $fileno = 0;
state $outfile;
state $format;
state @children;
state %attributes;

BEGIN { die "No directory tmp/ available! Please create it" unless -d 'tmp/' }

MAIN: {
  while(<>) {
    if ( (my $start = /^~{3,}\s*\{.*?(?<format>rdfdot|ditaa|dot|neato|yuml|plantuml)(?<attributes>.*)\}.*/) ... (my $end = /^~{3,}\s*$/) ) {
        $start ? begin_ppp() : $end ? end_ppp() : print {$outfile} $_
    } else {
      print
    }
  }
}

SUBS: {
  sub begin_ppp {
    $fileno++;
    $format = $+{format};
    $attributes{$fileno.$format} = $+{attributes};
    open $outfile, '>', "tmp/image-$fileno.$format"
  }

  sub end_ppp {
    close $outfile;

    my %attrs = map { /(.+?)=(["']?.+["']?)/ ? ($1 => $2) : ($_ => 1) } split /\s+\./, $attributes{$fileno.$format};
    if( my $child = fork == 0 ) {
       push @children, $child;
       $format =~ /^ditaa$/ and do {
         my $cmd =
          "ditaa " .
          (exists $attrs{"rounded-corners"} ? '--round-corners ' : ' ') .
          (exists $attrs{"no-shadows"}      ? '--no-shadows '    : ' ') .
          (exists $attrs{"no-separation"}   ? '--no-separation ' : ' ') .
          (exists $attrs{"no-antialias"}    ? '--no-antialias '  : ' ') .
          " tmp/image-$fileno.$format tmp/image-$fileno.png 2>&1 >>tmp/ditaa.log"
        ;
        system($cmd);
        system("mogrify -scale $attrs{scale} tmp/image-$fileno.png 2>&1 >>tmp/mogrify.log") if exists $attrs{scale};
       };
       $format =~ /^rdfdot$/ and do {
         system("rdfdot -ttl tmp/image-$fileno.$format tmp/image-$fileno.png 2>&1 >>tmp/rdfdot.log");
         system("mogrify -scale $attrs{scale} tmp/image-$fileno.png 2>&1 >>tmp/mogrify.log") if exists $attrs{scale};
       };
       $format =~ /^dot$/ and do {
         system("dot -Tpng -o tmp/image-$fileno.png tmp/image-$fileno.$format 2>&1 >>tmp/dot.log");
         system("mogrify -scale $attrs{scale} tmp/image-$fileno.png 2>&1 >>tmp/mogrify.log") if exists $attrs{scale};
       };
       $format =~ /^neato$/ and do {
         system("neato -Tpng -o tmp/image-$fileno.png tmp/image-$fileno.$format 2>&1 >>tmp/neato.log");
         system("mogrify -scale $attrs{scale} tmp/image-$fileno.png 2>&1 >>tmp/mogrify.log") if exists $attrs{scale};
       };
       $format =~ /^yuml$/ and do {
         my $cmd =
           "yuml " .
           (exists $attrs{"type"}       ? "--type  @{[$attrs{type}      ]} ": '--type=class ') .
           (exists $attrs{"style"}      ? "--style @{[$attrs{style}     ]} ": '--style=boring ') .
           (exists $attrs{"direction"}  ? "--dir   @{[$attrs{direction} ]} ": '--dir=LR ') .
           " --in tmp/image-$fileno.$format --out tmp/image-$fileno.png 2>&1 >>tmp/yuml.log"
         ;
         system($cmd);
         system("mogrify -scale $attrs{scale} tmp/image-$fileno.png 2>&1 >>tmp/mogrify.log") if exists $attrs{scale};
       };
       $format =~ /^plantuml$/ and do {
         system("plantuml -tpng -charset UTF-8 tmp/image-$fileno.$format 2>&1 >>tmp/plantuml.log");
         system("mogrify -scale $attrs{scale} tmp/image-$fileno.png 2>&1 >>tmp/mogrify.log") if exists $attrs{scale};
       };
       exit 0;
    }

    $format =
      "![" . (exists $attrs{title} ? $attrs{title} : '' )
           . (exists $attrs{label} ? "\\label{$attrs{label}}" : '' ).
      "]"  .
      "(tmp/image-$fileno.png)" .
      (exists $attrs{inline} ? '\\ ' : '')
    ;
    say STDOUT $format;
    $format = '';
  }
}

END {
  while (1) {
    my $child = waitpid(-1, 0);
    last if $child == -1;       # No more outstanding children
  }
}

__END__

=pod

=head1 NAME

ppp - Preprocess Pandoc before Processing Pandoc

=head1 VERSION

version 0.7.2

=head1 AUTHOR

DBR <dbr@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2014 by DBR.

This is free software, licensed under:

  DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 2004

=cut
