<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::relativedate;

use warnings;
no warnings 'redefine';
use strict;
use IkiWiki 2.00;
use POSIX;
use Encode;

sub import { #{{{
	add_underlay("javascript");
	hook(type =&gt; "getsetup", id =&gt; "relativedate", call =&gt; \&amp;getsetup);
	hook(type =&gt; "format", id =&gt; "relativedate", call =&gt; \&amp;format);
	inject(name =&gt; "IkiWiki::displaytime", call =&gt; \&amp;mydisplaytime);
} # }}}

sub getsetup () { #{{{
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
} #}}}

sub format (@) { #{{{
        my %params=@_;

	if (! ($params{content}=~s!^(&lt;body&gt;)!$1.include_javascript($params{page})!em)) {
		# no &lt;/body&gt; tag, probably in preview mode
		$params{content}=include_javascript($params{page}, 1).$params{content};
	}
	return $params{content};
} # }}}

sub include_javascript ($;$) { #{{{
	my $page=shift;
	my $absolute=shift;
	
	return '&lt;script src="'.urlto("ikiwiki.js", $page, $absolute).
		'" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;'."\n".
		'&lt;script src="'.urlto("relativedate.js", $page, $absolute).
		'" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;';
} #}}}

sub mydisplaytime ($;$) { #{{{
	my $time=shift;
	my $format=shift;

	# This needs to be in a form that can be parsed by javascript.
	# Being fairly human readable is also nice, as it will be exposed
	# as the title if javascript is not available.
	my $gmtime=decode_utf8(POSIX::strftime("%a, %d %b %Y %H:%M:%S %z",
			localtime($time)));

	return '&lt;span class="relativedate" title="'.$gmtime.'"&gt;'.
		IkiWiki::formattime($time, $format).'&lt;/span&gt;';
} #}}}

1
</pre></body></html>