ditaa.pl 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #! /usr/bin/perl
  2. # Copyright (C) 2015–2017 Alex Schroeder <alex@gnu.org>
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. AddModuleDescription('ditaa.pl', 'Ditaa for Diagrams');
  17. our ($q, $bol, @MyRules, @KnownLocks, $TempDir);
  18. push (@MyRules, \&DitaaRule);
  19. push(@KnownLocks, 'diagram');
  20. sub DitaaRule {
  21. if ($bol && m/\G&lt;diagram(\s+style=".*")?&gt;\n((.*\n)+)&lt;\/diagram&gt;/cg) {
  22. return "MIME::Base64 not installed" unless eval { require MIME::Base64; };
  23. my $style = $1;
  24. my $map = UnquoteHtml($2);
  25. RequestLockDir('diagram', undef, undef, 1);
  26. WriteStringToFile("$TempDir/diagram.txt", $map);
  27. $ENV{LANG}='en_US.UTF-8'; # Java needs Locale to match as well!
  28. my $output = `ditaa "$TempDir/diagram.txt" "$TempDir/diagram.png"`;
  29. my $image = '';
  30. # not UTF-8 layer!
  31. if (open(IN, '<', "$TempDir/diagram.png")) {
  32. local $/ = undef; # Read complete files
  33. $image = <IN>;
  34. close IN;
  35. }
  36. ReleaseLockDir('diagram');
  37. my $data = MIME::Base64::encode_base64($image);
  38. my $url = "data:image/png;base64,$data";
  39. return CloseHtmlEnvironments()
  40. . "<div$style>" . $q->img({-src=>$url, -alt=>$map, -loading=>'lazy'}) . "</div>";
  41. }
  42. return undef;
  43. }