Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki] / IkiWiki / Plugin / relativedate.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::relativedate;
3
4 use warnings;
5 no warnings 'redefine';
6 use strict;
7 use IkiWiki 2.00;
8 use POSIX;
9 use Encode;
10
11 sub import { #{{{
12         add_underlay("javascript");
13         hook(type => "getsetup", id => "relativedate", call => \&getsetup);
14         hook(type => "format", id => "relativedate", call => \&format);
15 } # }}}
16
17 sub getsetup () { #{{{
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => 1,
22                 },
23 } #}}}
24
25 sub format (@) { #{{{
26         my %params=@_;
27
28         if (! ($params{content}=~s!^(<body>)!$1.include_javascript($params{page})!em)) {
29                 # no </body> tag, probably in preview mode
30                 $params{content}=include_javascript($params{page}, 1).$params{content};
31         }
32         return $params{content};
33 } # }}}
34
35 sub include_javascript ($;$) { #{{{
36         my $page=shift;
37         my $absolute=shift;
38         
39         return '<script src="'.urlto("ikiwiki.js", $page, $absolute).
40                 '" type="text/javascript" charset="utf-8"></script>'."\n".
41                 '<script src="'.urlto("relativedate.js", $page, $absolute).
42                 '" type="text/javascript" charset="utf-8"></script>';
43 } #}}}
44
45 sub IkiWiki::displaytime ($;$) { #{{{
46         my $time=shift;
47         my $format=shift;
48
49         # This needs to be in a form that can be parsed by javascript.
50         # Being fairly human readable is also nice, as it will be exposed
51         # as the title if javascript is not available.
52         my $gmtime=decode_utf8(POSIX::strftime("%a, %d %b %Y %H:%M:%S %z",
53                         localtime($time)));
54
55         return '<span class="date" title="'.$gmtime.'">'.
56                 IkiWiki::formattime($time, $format).'</span>';
57 } #}}}
58
59 1