]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/relativedate.pm
(no commit message)
[git.ikiwiki.info.git] / IkiWiki / Plugin / relativedate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::relativedate;
4 use warnings;
5 no warnings 'redefine';
6 use strict;
7 use IkiWiki 3.00;
8 use POSIX ();
9 use Encode;
11 sub import {
12         add_underlay("javascript");
13         hook(type => "getsetup", id => "relativedate", call => \&getsetup);
14         hook(type => "format", id => "relativedate", call => \&format);
15         inject(name => "IkiWiki::displaytime", call => \&mydisplaytime);
16 }
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => 1,
23                 },
24 }
26 sub format (@) {
27         my %params=@_;
29         if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
30                 # no <body> tag, probably in preview mode
31                 $params{content}=$params{content}.include_javascript(undef);
32         }
33         return $params{content};
34 }
36 sub include_javascript ($) {
37         my $from=shift;
38         
39         return '<script src="'.urlto("ikiwiki/ikiwiki.js", $from).
40                 '" type="text/javascript" charset="utf-8"></script>'."\n".
41                 '<script src="'.urlto("ikiwiki/relativedate.js", $from).
42                 '" type="text/javascript" charset="utf-8"></script>';
43 }
45 sub mydisplaytime ($;$$) {
46         my $time=shift;
47         my $format=shift;
48         my $pubdate=shift;
50         # This needs to be in a form that can be parsed by javascript.
51         # (Being fairly human readable is also nice, as it will be exposed
52         # as the title if javascript is not available.)
53         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
54         POSIX::setlocale(&POSIX::LC_TIME, "C");
55         my $gmtime=decode_utf8(POSIX::strftime("%a, %d %b %Y %H:%M:%S %z",
56                         localtime($time)));
57         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
59         my $mid=' class="relativedate" title="'.$gmtime.'">'.
60                 IkiWiki::formattime($time, $format);
62         if ($config{html5}) {
63                 return '<time datetime="'.IkiWiki::date_3339($time).'"'.
64                         ($pubdate ? ' pubdate="pubdate"' : '').$mid.'</time>';
65         }
66         else {
67                 return '<span'.$mid.'</span>';
68         }
69 }
71 1