<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::flattr;

use warnings;
use strict;
use IkiWiki 3.00;

sub import {
	hook(type =&gt; "getsetup", id =&gt; "flattr", call =&gt; \&amp;getsetup);
	hook(type =&gt; "preprocess", id =&gt; "flattr", call =&gt; \&amp;preprocess);
	hook(type =&gt; "format", id =&gt; "flattr", call =&gt; \&amp;format);
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; undef,
		},
		flattr_userid =&gt; {
			type =&gt; "string",
			example =&gt; 'joeyh',
			description =&gt; "userid or user name to use by default for Flattr buttons",
			advanced =&gt; 0,
			safe =&gt; 1,
			rebuild =&gt; undef,
		},
}

my %flattr_pages;

sub preprocess (@) {
	my %params=@_;

	$flattr_pages{$params{destpage}}=1;

	my $url=$params{url};
	if (! defined $url) {
		$url=urlto($params{page}, "", 1);
	}

	my @fields;
	foreach my $field (qw{language uid button hidden category tags}) {
		if (exists $params{$field}) {
			push @fields, "$field:$params{$field}";
		}
	}
	
	return '&lt;a class="FlattrButton" href="'.$url.'"'.
       		(exists $params{title} ? ' title="'.$params{title}.'"' : '').
		' rev="flattr;'.join(';', @fields).';"'.
		'&gt;'.
		(exists $params{description} ? $params{description} : '').
		'&lt;/a&gt;';
}

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

	# Add flattr's javascript to pages with flattr buttons.
	if ($flattr_pages{$params{page}}) {
		if (! ($params{content}=~s!^(&lt;body[^&gt;]*&gt;)!$1.flattrjs()!em)) {
			# no &lt;body&gt; tag, probably in preview mode
			$params{content}=flattrjs().$params{content};
		}
	}
	return $params{content};
}

my $js_cached;
sub flattrjs {
	return $js_cached if defined $js_cached;

	my $js_url='https://api.flattr.com/js/0.5.0/load.js?mode=auto';
	if (defined $config{flattr_userid}) {
		my $userid=$config{flattr_userid};
		$userid=~s/[^-A-Za-z0-9_]//g; # sanitize for inclusion in javascript
		$js_url.="&amp;uid=$userid";
	}

	# This is Flattr's standard javascript snippet to include their
	# external javascript file, asynchronously.
	return $js_cached=&lt;&lt;"EOF";
&lt;script type="text/javascript"&gt;
&lt;!--//--&gt;&lt;![CDATA[//&gt;&lt;!--
(function() {
	var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
	s.type = 'text/javascript';
	s.async = true;
	s.src = '$js_url';
	t.parentNode.insertBefore(s, t);
})();//--&gt;&lt;!]]&gt;
&lt;/script&gt;
EOF
}

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