]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/relativedate.pm
add news item for ikiwiki 3.20110225
[ikiwiki.git] / 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 3.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         inject(name => "IkiWiki::displaytime", call => \&mydisplaytime);
16 }
17
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => 1,
23                 },
24 }
25
26 sub format (@) {
27         my %params=@_;
28
29         if (! ($params{content}=~s!^(<body[^>]*>)!$1.include_javascript($params{page})!em)) {
30                 # no <body> tag, probably in preview mode
31                 $params{content}=include_javascript(undef).$params{content};
32         }
33         return $params{content};
34 }
35
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 }
44
45 sub mydisplaytime ($;$$) {
46         my $time=shift;
47         my $format=shift;
48         my $pubdate=shift;
49
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);
58
59         my $mid=' class="relativedate" title="'.$gmtime.'">'.
60                 IkiWiki::formattime($time, $format);
61
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 }
70
71 1