<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 3.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;]*&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/ikiwiki.js", $page, $absolute).
		'" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;'."\n".
		'&lt;script src="'.urlto("ikiwiki/relativedate.js", $page, $absolute).
		'" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;';
}

sub mydisplaytime ($;$$) {
	my $time=shift;
	my $format=shift;
	my $pubdate=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)));

	my $mid=' class="relativedate" title="'.$gmtime.'"&gt;'.
		IkiWiki::formattime($time, $format);

	if ($config{html5}) {
		return '&lt;time datetime="'.IkiWiki::date_3339($time).'"'.
			($pubdate ? ' pubdate' : '').$mid.'&lt;/time&gt;';
	}
	else {
		return '&lt;span'.$mid.'&lt;/span&gt;';
	}
}

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