summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/relativedate.pm
blob: 7296889ab28f21c9c642e468d503dc502428ede7 (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 $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  46. POSIX::setlocale(&POSIX::LC_TIME, "C");
  47. my $gmtime=decode_utf8(POSIX::strftime("%a, %d %b %Y %H:%M:%S %z",
  48. localtime($time)));
  49. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  50. my $mid=' class="relativedate" title="'.$gmtime.'">'.
  51. IkiWiki::formattime($time, $format);
  52. if ($config{html5}) {
  53. return '<time datetime="'.IkiWiki::date_3339($time).'"'.
  54. ($pubdate ? ' pubdate="pubdate"' : '').$mid.'</time>';
  55. }
  56. else {
  57. return '<span'.$mid.'</span>';
  58. }
  59. }
  60. 1