summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/relativedate.pm
blob: 179f9238c97efa990836049e3c6543ceb2d3bc70 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::relativedate;
  3. use warnings;
  4. no warnings 'redefine';
  5. use strict;
  6. use IkiWiki 3.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/ikiwiki.js", $page, $absolute).
  34. '" type="text/javascript" charset="utf-8"></script>'."\n".
  35. '<script src="'.urlto("ikiwiki/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. my $pubdate=shift;
  42. # This needs to be in a form that can be parsed by javascript.
  43. # Being fairly human readable is also nice, as it will be exposed
  44. # as the title if javascript is not available.
  45. my $gmtime=decode_utf8(POSIX::strftime("%a, %d %b %Y %H:%M:%S %z",
  46. localtime($time)));
  47. my $mid=' class="relativedate" title="'.$gmtime.'">'.
  48. IkiWiki::formattime($time, $format);
  49. if ($config{html5}) {
  50. return '<time datetime="'.IkiWiki::date_3339($time).'"'.
  51. ($pubdate ? ' pubdate="pubdate"' : '').$mid.'</time>';
  52. }
  53. else {
  54. return '<span'.$mid.'</span>';
  55. }
  56. }
  57. 1