summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/relativedate.pm
blob: 0c9426dda074fc5eb2ded88d82e8387b88e58414 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::relativedate;
  3. use warnings;
  4. no warnings 'redefine';
  5. use strict;
  6. use IkiWiki 2.00;
  7. use POSIX;
  8. use Encode;
  9. sub import { #{{{
  10. add_underlay("javascript");
  11. hook(type => "getsetup", id => "relativedate", call => \&getsetup);
  12. hook(type => "format", id => "relativedate", call => \&format);
  13. } # }}}
  14. sub getsetup () { #{{{
  15. return
  16. plugin => {
  17. safe => 1,
  18. rebuild => 1,
  19. },
  20. } #}}}
  21. sub format (@) { #{{{
  22. my %params=@_;
  23. if (! ($params{content}=~s!^(<body>)!$1.include_javascript($params{page})!em)) {
  24. # no </body> tag, probably in preview mode
  25. $params{content}=include_javascript($params{page}, 1).$params{content};
  26. }
  27. return $params{content};
  28. } # }}}
  29. sub include_javascript ($;$) { #{{{
  30. my $page=shift;
  31. my $absolute=shift;
  32. return '<script src="'.urlto("ikiwiki.js", $page, $absolute).
  33. '" type="text/javascript" charset="utf-8"></script>'."\n".
  34. '<script src="'.urlto("relativedate.js", $page, $absolute).
  35. '" type="text/javascript" charset="utf-8"></script>';
  36. } #}}}
  37. sub IkiWiki::displaytime ($;$) { #{{{
  38. my $time=shift;
  39. my $format=shift;
  40. # This needs to be in a form that can be parsed by javascript.
  41. # Being fairly human readable is also nice, as it will be exposed
  42. # as the title if javascript is not available.
  43. my $gmtime=decode_utf8(POSIX::strftime("%a, %d %b %Y %H:%M:%S %z",
  44. localtime($time)));
  45. return '<span class="date" title="'.$gmtime.'">'.
  46. IkiWiki::formattime($time, $format).'</span>';
  47. } #}}}
  48. 1