add paths to plugin files in copyright
[ikiwiki.git] / IkiWiki / Plugin / relativedate.pm
blob4ae0be8615852db38222ebd892fd5c216bd5b4bf
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);
18 sub getsetup () {
19 return
20 plugin => {
21 safe => 1,
22 rebuild => 1,
26 sub format (@) {
27 my %params=@_;
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};
33 return $params{content};
36 sub include_javascript ($) {
37 my $from=shift;
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>';
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>';
66 else {
67 return '<span'.$mid.'</span>';