imagify.pl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (C) 2015 Alex-Daniel Jakimenko <alex.jakimenko@gmail.com>
  2. #
  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. use Digest::SHA qw(sha256_hex);
  17. AddModuleDescription('imagify.pl', 'Imagify Extension');
  18. our ($q, @MyRules, $ScriptName, $DataDir);
  19. our (%ImagifyParams, $ImagifyDir, $ImagifyFormat);
  20. $ImagifyFormat = 'png';
  21. %ImagifyParams = qw{-background transparent -fill black -font Corsiva -pointsize 16 -size 600x};
  22. $ImagifyDir = "$DataDir/imagify"; # For images with rendered text
  23. push(@MyRules, \&DivFooRule);
  24. sub DivFooRule {
  25. if (m/\Gimagify [ \t]* \{\{\{[ \t]*\n? (.*?)\n? \}\}\}[ \t]*(\n|$)?/cgsx) {
  26. my $str = $1;
  27. CreateDir($ImagifyDir);
  28. my $fileName = sha256_hex($str) . '.' . $ImagifyFormat;
  29. system('convert', %ImagifyParams, "caption:$str", "$ImagifyDir/$fileName") unless IsFile("$ImagifyDir/$fileName");
  30. my $src = $ScriptName . "/imagify/" . UrlEncode($fileName);
  31. return CloseHtmlEnvironments() . $q->img({-class => 'imagify', -src => $src, -alt => '(rendered text)'}) . AddHtmlEnvironment('p');
  32. }
  33. return;
  34. }