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