summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/relativedate.pm
blob: 4ae0be8615852db38222ebd892fd5c216bd5b4bf (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(undef).$params{content};
  27. }
  28. return $params{content};
  29. }
  30. sub include_javascript ($) {
  31. my $from=shift;
  32. return '<script src="'.urlto("ikiwiki/ikiwiki.js", $from).
  33. '" type="text/javascript" charset="utf-8"></script>'."\n".
  34. '<script src="'.urlto("ikiwiki/relativedate.js", $from).
  35. '" type="text/javascript" charset="utf-8"></script>';
  36. }
  37. sub mydisplaytime ($;$$) {
  38. my $time=shift;
  39. my $format=shift;
  40. my $pubdate=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 $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  45. POSIX::setlocale(&POSIX::LC_TIME, "C");
  46. my $gmtime=decode_utf8(POSIX::strftime("%a, %d %b %Y %H:%M:%S %z",
  47. localtime($time)));
  48. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  49. my $mid=' class="relativedate" title="'.$gmtime.'">'.
  50. IkiWiki::formattime($time, $format);
  51. if ($config{html5}) {
  52. return '<time datetime="'.IkiWiki::date_3339($time).'"'.
  53. ($pubdate ? ' pubdate="pubdate"' : '').$mid.'</time>';
  54. }
  55. else {
  56. return '<span'.$mid.'</span>';
  57. }
  58. }
  59. 1