]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/flattr.pm
9cf9dfb138ae74a3333ae102f3ffba44ab4a3952
[ikiwiki.git] / IkiWiki / Plugin / flattr.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::flattr;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
9         hook(type => "getsetup", id => "flattr", call => \&getsetup);
10         hook(type => "preprocess", id => "flattr", call => \&preprocess);
11         hook(type => "sanitize", id => "flattr", call => \&sanitize, last => 1);
12 }
13
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20                 flattr_userid => {
21                         type => "string",
22                         example => 'joeyh',
23                         description => "userid or user name to use by default for Flattr buttons",
24                         advanced => 0,
25                         safe => 1,
26                         rebuild => undef,
27                 },
28 }
29
30 my %flattr_pages;
31
32 sub preprocess (@) {
33         my %params=@_;
34
35         $flattr_pages{$params{destpage}}=1;
36
37         my $url=$params{url};
38         if (! defined $url) {
39                 $url=urlto($params{page}, "", 1);
40         }
41
42         my @fields;
43         foreach my $field (qw{language uid button hidden category tags}) {
44                 if (exists $params{$field}) {
45                         push @fields, "$field:$params{$field}";
46                 }
47         }
48         
49         return '<a class="FlattrButton" href="'.$url.'"'.
50                 (exists $params{title} ? ' title="'.$params{title}.'"' : '').
51                 ' rev="flattr;'.join(';', @fields).';"'.
52                 '>'.
53                 (exists $params{description} ? $params{description} : '').
54                 '</a>';
55 }
56
57 sub sanitize (@) {
58         my %params=@_;
59
60         # Add flattr's javascript to pages with flattr buttons.
61         if ($flattr_pages{$params{page}}) {
62                 return flattrjs().$params{content};
63         }
64         else {
65                 return $params{content};
66         }
67 }
68
69 my $js_cached;
70 sub flattrjs {
71         return $js_cached if defined $js_cached;
72
73         my $js_url='https://api.flattr.com/js/0.5.0/load.js?mode=auto';
74         if (defined $config{flattr_userid}) {
75                 my $userid=$config{flattr_userid};
76                 $userid=~s/[^-A-Za-z0-9_]//g; # sanitize for inclusion in javascript
77                 $js_url.="&uid=$userid";
78         }
79
80         # This is Flattr's standard javascript snippet to include their
81         # external javascript file, asynchronously.
82         return $js_cached=<<"EOF";
83 <script type="text/javascript">
84 <!--//--><![CDATA[//><!--
85 (function() {
86         var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
87         s.type = 'text/javascript';
88         s.async = true;
89         s.src = '$js_url';
90         t.parentNode.insertBefore(s, t);
91 })();//--><!]]>
92 </script>
93 EOF
94 }
95
96 1