age.pl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2006 Alex Schroeder <alex@emacswiki.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('age.pl', 'Age Indication Extension');
  18. our (%Page, $Now, $ScriptName);
  19. our (%AgeEffect, $AgeParameter);
  20. # map page age to theme
  21. %AgeEffect = (60*60*24 => 'day',
  22. 60*60*24*7 => 'week',
  23. 60*60*24*28 => 'moon',
  24. 60*60*24*365 => 'year',
  25. );
  26. # attribute in the page file to use as the timestamp -- use 'created'
  27. # if using creationdate.pl.
  28. $AgeParameter = 'ts';
  29. *OldAgeGetHeader = \&GetHeader;
  30. *GetHeader = \&NewAgeGetHeader;
  31. sub NewAgeGetHeader {
  32. my $header = OldAgeGetHeader(@_);
  33. return $header unless $Page{$AgeParameter}; # open page required
  34. my $age = $Now - $Page{$AgeParameter};
  35. my $theme = '';
  36. for my $seconds (sort {$b <=> $a} keys %AgeEffect) {
  37. if ($age > $seconds) {
  38. $theme = $AgeEffect{$seconds};
  39. last;
  40. }
  41. }
  42. return $header unless $theme;
  43. my $oldtheme = GetParam('theme', $ScriptName);
  44. $header =~ s/class="$oldtheme"/class="$theme"/; # touch as little as possible
  45. return $header;
  46. }