portrait-support.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2004, 2005, 2006, 2009 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('portrait-support.pl', 'Portraits Support Extension');
  18. our ($q, $bol, $Now, @MyMacros, @MyRules, $FreeLinkPattern, $UrlPattern, $FS);
  19. push(@MyMacros, sub{ s/\[new::\]/"[new:" . GetParam('username', T('Anonymous'))
  20. . ':' . TimeToText($Now) . "]"/eg });
  21. push(@MyMacros, sub{ s/\[new:$FreeLinkPattern\]/"[new:$1:" . TimeToText($Now) . "]"/eg });
  22. push(@MyRules, \&PortraitSupportRule);
  23. our ($PortraitSupportColorDiv, $PortraitSupportColor);
  24. $PortraitSupportColor = 0;
  25. $PortraitSupportColorDiv = 0;
  26. my %Portrait = ();
  27. sub PortraitSupportRule {
  28. if ($bol) {
  29. if (m/\G(\s*\n)*----+[ \t]*\n?/cg) {
  30. $PortraitSupportColor = 0;
  31. my $html = CloseHtmlEnvironments() . ($PortraitSupportColorDiv ? '</div>' : '')
  32. . $q->hr() . AddHtmlEnvironment('p');
  33. $PortraitSupportColorDiv = 0;
  34. return $html;
  35. } elsif ($bol && m/\Gportrait:$UrlPattern/cg) {
  36. return $q->img({-src=>$1, -alt=>T("Portrait"), -class=>'portrait', -loading=>'lazy'});
  37. } elsif ($bol && m/\G(:*)\[new(.*)\]/cg) {
  38. my $portrait = '';
  39. my $depth = length($1);
  40. my ($ignore, $name, $time) = split(/:/, $2, 3);
  41. if ($name) {
  42. if (not $Portrait{$name}) {
  43. my $oldpos = pos;
  44. if (GetPageContent($name) =~ m/portrait:$UrlPattern/) {
  45. $Portrait{$name} =
  46. $q->div({-class=>"portrait"},
  47. $q->p(ScriptLink($name, $q->img({-src=>$1, -alt=>'new: ' . $time,
  48. -class=>'portrait'}),
  49. 'newauthor', '', $FS),
  50. $q->br(),
  51. GetPageLink($name)));
  52. }
  53. }
  54. $portrait = $Portrait{$name};
  55. $portrait =~ s/$FS/$time/;
  56. }
  57. my $html = CloseHtmlEnvironments()
  58. . ($PortraitSupportColorDiv ? '</div>' : '');
  59. $PortraitSupportColor = !$PortraitSupportColor;
  60. $html .= '<div class="color '
  61. . ($PortraitSupportColor ? 'one' : 'two')
  62. . ' level' . $depth
  63. . '">' . $portrait . AddHtmlEnvironment('p');
  64. $PortraitSupportColorDiv = 1;
  65. return $html;
  66. }
  67. }
  68. return;
  69. }
  70. *OldPortraitSupportApplyRules = \&ApplyRules;
  71. *ApplyRules = \&NewPortraitSupportApplyRules;
  72. sub NewPortraitSupportApplyRules {
  73. my ($blocks, $flags) = OldPortraitSupportApplyRules(@_);
  74. if ($PortraitSupportColorDiv) {
  75. print '</div>';
  76. $blocks .= $FS . '</div>';
  77. $flags .= $FS . 0;
  78. $PortraitSupportColorDiv = 0;
  79. }
  80. return ($blocks, $flags);
  81. }