(WIP) projects: Das Huhn
[www-rohieb-name.git] / plugins / IkiWiki / Plugin / flattrthing.pm
1 #!/usr/bin/perl
2
3 # This plugin allows your page templates to include Flattr buttons, so
4 # Flattr buttons are automatically added on every page that uses the template.
5 # If you don't specify a thing ID with the flattrthing directive, it adds
6 # auto-submit buttons so things are created when the button is clicked the first
7 # time.
8 #
9 # The plugin is customizable through the following configuration options:
10 # - flattrthing_pagespec: allows you to specify a pagespec on which pages the
11 # plugin is active. If empty, no Flattr buttons are generated at all.
12 # - flattrthing_defaultuser: sets the default user that owns the Flattr things
13 # which are created by the auto-submit buttons.
14 #
15 # Also, the plugin adds the flattrthing directive, which allows you to customize
16 # the behaviour for one page. It knows the following parameters:
17 # - id: the Flattr thing ID (like in http://flattr.com/thing/12341234). If you
18 # specify this option, a button with a thing URL is generated instead of an
19 # auto-submit button. This allows you to link to things which already exist
20 # by the time you create your page.
21 # - user: a Flattr username that owns the thing created by the generated
22 # auto-submit button. This parameter has no effect when an ID is specified,
23 # since in this case, the owner is already known to Flattr.
24 #
25 # This plugin sets the following template variables which your templates should
26 # process and generate the according buttons with the right URLs:
27 # - FLATTR_ENABLE: contains a truthful value if the page matches the pagespec
28 # configured with flattrthing_pagespec. You can use it with TMPL_IF to
29 # generate code specific to pages which have a Flattr button.
30 # - FLATTR_THING_ID: if a thing ID was specified with the flattrthing directive,
31 # this variable contains the ID that was given in the directive.
32 # - FLATTR_THING_ID_SET: contains a thurthful value if FLATTR_THING_ID is set.
33 # - FLATTR_USER: contains the username that was specified with the flattrthing
34 # directive, or if that was not the case, the username that was configured
35 # with flattrthing_defaultuser in your setup file.
36 # - FLATTR_THING_URL: contains the full URL to the page.
37
38 package IkiWiki::Plugin::flattrthing;
39
40 use warnings;
41 use strict;
42 use IkiWiki 3.00;
43
44 sub import {
45 hook(type => "getsetup", id => "flattrthing", call => \&getsetup);
46 hook(type => "scan", id => "flattrthing", call => \&scan);
47 hook(type => "preprocess", id => "flattrthing", call => \&preprocess);
48 hook(type => "pagetemplate", id => "flattrthing", call => \&pagetemplate);
49 }
50
51 sub getsetup () {
52 return
53 plugin => {
54 safe => 1,
55 rebuild => undef,
56 section => "widget",
57 },
58 flattrthing_pagespec => {
59 type => "pagespec",
60 example => "!*/Discussion",
61 description => "Pages on which flattr button should be inserted. If empty, flattr buttons are not shown on any page.",
62 link => "ikiwiki/PageSpec",
63 safe => 1,
64 rebuild => undef,
65 },
66 flattrthing_defaultuser => {
67 type => "string",
68 example => "!*/Discussion",
69 description => "Default flattr user that owns a thing if the username is not changed with the flattrthing directive",
70 safe => 1,
71 rebuild => undef,
72 },
73 }
74
75 sub scan {
76 my %params = @_;
77 my $page = $params{page};
78 my $destpage = $params{destpage};
79 my $baseurl = $config{url};
80 $baseurl .= "/" unless substr($baseurl, -1) eq "/";
81 if($destpage) {
82 $baseurl .= $destpage;
83 } else {
84 $baseurl .= $page;
85 }
86 $pagestate{$page}{flattrthing}{url} = $baseurl;
87
88 if(exists $config{flattrthing_defaultuser} &&
89 length $config{flattrthing_defaultuser}) {
90 $pagestate{$page}{flattrthing}{user} = $config{flattrthing_defaultuser};
91 } else {
92 return error("Please set flattrthing_defaultuser in configuration!");
93 }
94 }
95
96 sub preprocess {
97 return "" unless @_;
98 my %params = @_;
99
100 return error("flattrthing: please specify id or user") unless ($params{id}
101 or $params{user});
102
103 my $page = $params{page};
104 $pagestate{$page}{flattrthing}{id} = int($params{id}) if $params{id};
105 $pagestate{$page}{flattrthing}{user} = int($params{user}) if $params{user};
106
107 return "";
108 }
109
110 sub pagetemplate {
111 my %params = @_;
112 my $page = $params{page};
113 my $template = $params{template};
114
115 if(exists $config{flattrthing_pagespec} &&
116 length $config{flattrthing_pagespec} &&
117 exists $pagestate{$page}{flattrthing}{user} &&
118 length $pagestate{$page}{flattrthing}{user} &&
119 pagespec_match($params{page}, $config{flattrthing_pagespec})) {
120 $template->param(FLATTR_ENABLED => 1);
121 $template->param(
122 FLATTR_THING_URL => $pagestate{$page}{flattrthing}{url},
123 FLATTR_USER => $pagestate{$page}{flattrthing}{user},
124 );
125
126 if(exists $pagestate{$page}{flattrthing}{id}) {
127 $template->param(
128 FLATTR_THING_ID_SET => 1,
129 FLATTR_THING_ID => $pagestate{$page}{flattrthing}{id},
130 );
131 }
132 }
133 }
134
135 1;
This page took 0.061208 seconds and 5 git commands to generate.