fork page.tmpl and add flattrthing plugin to auto-generate Flattr buttons
authorRoland Hieber <rohieb@rohieb.name>
Mon, 21 Oct 2013 03:32:14 +0000 (05:32 +0200)
committerRoland Hieber <rohieb@rohieb.name>
Wed, 23 Oct 2013 02:20:30 +0000 (04:20 +0200)
plugins/IkiWiki/Plugin/flattrthing.pm [new file with mode: 0644]
templates/page.tmpl [new file with mode: 0644]

diff --git a/plugins/IkiWiki/Plugin/flattrthing.pm b/plugins/IkiWiki/Plugin/flattrthing.pm
new file mode 100644 (file)
index 0000000..c917ed4
--- /dev/null
@@ -0,0 +1,135 @@
+#!/usr/bin/perl
+
+# This plugin allows your page templates to include Flattr buttons, so
+# Flattr buttons are automatically added on every page that uses the template.
+# If you don't specify a thing ID with the flattrthing directive, it adds
+# auto-submit buttons so things are created when the button is clicked the first
+# time.
+#
+# The plugin is customizable through the following configuration options:
+# - flattrthing_pagespec: allows you to specify a pagespec on which pages the
+#   plugin is active. If empty, no Flattr buttons are generated at all.
+# - flattrthing_defaultuser: sets the default user that owns the Flattr things
+#   which are created by the auto-submit buttons.
+#
+# Also, the plugin adds the flattrthing directive, which allows you to customize
+# the behaviour for one page. It knows the following parameters:
+# - id: the Flattr thing ID (like in http://flattr.com/thing/12341234). If you
+#   specify this option, a button with a thing URL is generated instead of an
+#   auto-submit button. This allows you to link to things which already exist
+#   by the time you create your page.
+# - user: a Flattr username that owns the thing created by the generated
+#   auto-submit button. This parameter has no effect when an ID is specified,
+#   since in this case, the owner is already known to Flattr.
+#
+# This plugin sets the following template variables which your templates should
+# process and generate the according buttons with the right URLs:
+# - FLATTR_ENABLE: contains a truthful value if the page matches the pagespec
+#   configured with flattrthing_pagespec. You can use it with TMPL_IF to
+#   generate code specific to pages which have a Flattr button.
+# - FLATTR_THING_ID: if a thing ID was specified with the flattrthing directive,
+#   this variable contains the ID that was given in the directive.
+# - FLATTR_THING_ID_SET: contains a thurthful value if FLATTR_THING_ID is set.
+# - FLATTR_USER: contains the username that was specified with the flattrthing
+#   directive, or if that was not the case, the username that was configured
+#   with flattrthing_defaultuser in your setup file.
+# - FLATTR_THING_URL: contains the full URL to the page.
+
+package IkiWiki::Plugin::flattrthing;
+
+use warnings;
+use strict;
+use IkiWiki 3.00;
+
+sub import {
+       hook(type => "getsetup", id => "flattrthing", call => \&getsetup);
+       hook(type => "scan", id => "flattrthing", call => \&scan);
+       hook(type => "preprocess", id => "flattrthing", call => \&preprocess);
+       hook(type => "pagetemplate", id => "flattrthing", call => \&pagetemplate);
+}
+
+sub getsetup () {
+       return
+               plugin => {
+                       safe => 1,
+                       rebuild => undef,
+                       section => "widget",
+               },
+               flattrthing_pagespec => {
+                       type => "pagespec",
+                       example => "!*/Discussion",
+                       description => "Pages on which flattr button should be inserted. If empty, flattr buttons are not shown on any page.",
+                       link => "ikiwiki/PageSpec",
+                       safe => 1,
+                       rebuild => undef,
+               },
+               flattrthing_defaultuser => {
+                       type => "string",
+                       example => "!*/Discussion",
+                       description => "Default flattr user that owns a thing if the username is not changed with the flattrthing directive",
+                       safe => 1,
+                       rebuild => undef,
+               },
+}
+
+sub scan {
+       my %params = @_;
+       my $page = $params{page};
+       my $destpage = $params{destpage};
+       my $baseurl = $config{url};
+       $baseurl .= "/" unless substr($baseurl, -1) eq "/";
+       if($destpage) {
+               $baseurl .= $destpage;
+       } else {
+               $baseurl .= $page;
+       }
+       $pagestate{$page}{flattrthing}{url} = $baseurl;
+
+       if(exists $config{flattrthing_defaultuser} &&
+                length $config{flattrthing_defaultuser}) {
+               $pagestate{$page}{flattrthing}{user} = $config{flattrthing_defaultuser};
+       } else {
+               return error("Please set flattrthing_defaultuser in configuration!");
+       }
+}
+
+sub preprocess {
+       return "" unless @_;
+       my %params = @_;
+
+       return error("flattrthing: please specify id or user") unless ($params{id}
+               or $params{user});
+
+       my $page = $params{page};
+       $pagestate{$page}{flattrthing}{id} = int($params{id}) if $params{id};
+       $pagestate{$page}{flattrthing}{user} = int($params{user}) if $params{user};
+
+       return "";
+}
+
+sub pagetemplate {
+       my %params = @_;
+       my $page = $params{page};
+       my $template = $params{template};
+
+       if(exists $config{flattrthing_pagespec} &&
+                length $config{flattrthing_pagespec} &&
+                exists $pagestate{$page}{flattrthing}{user} &&
+                length $pagestate{$page}{flattrthing}{user} &&
+                pagespec_match($params{page}, $config{flattrthing_pagespec})) {
+               $template->param(FLATTR_ENABLED => 1);
+               $template->param(
+                       FLATTR_THING_URL => $pagestate{$page}{flattrthing}{url},
+                       FLATTR_USER => $pagestate{$page}{flattrthing}{user},
+                       );
+
+               if(exists $pagestate{$page}{flattrthing}{id}) {
+                       $template->param(
+                               FLATTR_THING_ID_SET => 1,
+                               FLATTR_THING_ID => $pagestate{$page}{flattrthing}{id},
+                               );
+               }
+       }
+}
+
+1;
diff --git a/templates/page.tmpl b/templates/page.tmpl
new file mode 100644 (file)
index 0000000..ae7fd01
--- /dev/null
@@ -0,0 +1,239 @@
+<TMPL_IF HTML5><!DOCTYPE html>
+<html>
+<TMPL_ELSE><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+</TMPL_IF>
+<head>
+<TMPL_IF DYNAMIC>
+<TMPL_IF FORCEBASEURL><base href="<TMPL_VAR FORCEBASEURL>" /><TMPL_ELSE>
+<TMPL_IF BASEURL><base href="<TMPL_VAR BASEURL>" /></TMPL_IF>
+</TMPL_IF>
+</TMPL_IF>
+<TMPL_IF HTML5><meta charset="utf-8" /><TMPL_ELSE><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></TMPL_IF>
+<title><TMPL_VAR TITLE></title>
+<TMPL_IF FAVICON>
+<link rel="icon" href="<TMPL_VAR BASEURL><TMPL_VAR FAVICON>" type="image/x-icon" />
+</TMPL_IF>
+<link rel="stylesheet" href="<TMPL_VAR BASEURL>style.css" type="text/css" />
+<TMPL_IF LOCAL_CSS>
+<link rel="stylesheet" href="<TMPL_VAR BASEURL><TMPL_VAR LOCAL_CSS>" type="text/css" />
+<TMPL_ELSE>
+<link rel="stylesheet" href="<TMPL_VAR BASEURL>local.css" type="text/css" />
+</TMPL_IF>
+
+<TMPL_UNLESS DYNAMIC>
+<TMPL_IF EDITURL>
+<link rel="alternate" type="application/x-wiki" title="Edit this page" href="<TMPL_VAR EDITURL>" />
+</TMPL_IF>
+<TMPL_IF FEEDLINKS><TMPL_VAR FEEDLINKS></TMPL_IF>
+<TMPL_IF RELVCS><TMPL_VAR RELVCS></TMPL_IF>
+<TMPL_IF META><TMPL_VAR META></TMPL_IF>
+<TMPL_LOOP TRAILLOOP>
+<TMPL_IF PREVPAGE>
+<link rel="prev" href="<TMPL_VAR PREVURL>" title="<TMPL_VAR PREVTITLE>" />
+</TMPL_IF>
+<link rel="up" href="<TMPL_VAR TRAILURL>" title="<TMPL_VAR TRAILTITLE>" />
+<TMPL_IF NEXTPAGE>
+<link rel="next" href="<TMPL_VAR NEXTURL>" title="<TMPL_VAR NEXTTITLE>" />
+</TMPL_IF>
+</TMPL_LOOP>
+</TMPL_UNLESS>
+
+</head>
+<body>
+
+<TMPL_IF HTML5><article class="page"><TMPL_ELSE><div class="page"></TMPL_IF>
+
+<TMPL_IF HTML5><section class="pageheader"><TMPL_ELSE><div class="pageheader"></TMPL_IF>
+<TMPL_IF HTML5><header class="header"><TMPL_ELSE><div class="header"></TMPL_IF>
+<span>
+<span class="parentlinks">
+<TMPL_LOOP PARENTLINKS>
+<a href="<TMPL_VAR URL>"><TMPL_VAR PAGE></a>/
+</TMPL_LOOP>
+</span>
+<span class="title">
+<TMPL_VAR TITLE>
+<TMPL_IF ISTRANSLATION>
+&nbsp;(<TMPL_VAR PERCENTTRANSLATED>%)
+</TMPL_IF>
+</span>
+</span>
+<TMPL_UNLESS DYNAMIC>
+<TMPL_IF SEARCHFORM>
+<TMPL_VAR SEARCHFORM>
+</TMPL_IF>
+</TMPL_UNLESS>
+<TMPL_IF HTML5></header><TMPL_ELSE></div></TMPL_IF>
+
+<TMPL_IF HAVE_ACTIONS>
+<TMPL_IF HTML5><nav class="actions"><TMPL_ELSE><div class="actions"></TMPL_IF>
+<ul>
+<TMPL_IF EDITURL>
+<li><a href="<TMPL_VAR EDITURL>" rel="nofollow">Edit</a></li>
+</TMPL_IF>
+<TMPL_IF RECENTCHANGESURL>
+<li><a href="<TMPL_VAR RECENTCHANGESURL>">RecentChanges</a></li>
+</TMPL_IF>
+<TMPL_IF HISTORYURL>
+<li><a href="<TMPL_VAR HISTORYURL>">History</a></li>
+</TMPL_IF>
+<TMPL_IF GETSOURCEURL>
+<li><a href="<TMPL_VAR GETSOURCEURL>">Source</a></li>
+</TMPL_IF>
+<TMPL_IF PREFSURL>
+<li><a href="<TMPL_VAR PREFSURL>">Preferences</a></li>
+</TMPL_IF>
+<TMPL_IF ACTIONS>
+<TMPL_LOOP ACTIONS>
+<li><TMPL_VAR ACTION></li>
+</TMPL_LOOP>
+</TMPL_IF>
+<TMPL_IF COMMENTSLINK>
+<li><TMPL_VAR COMMENTSLINK></li>
+<TMPL_ELSE>
+<TMPL_IF DISCUSSIONLINK>
+<li><TMPL_VAR DISCUSSIONLINK></li>
+</TMPL_IF>
+</TMPL_IF>
+</ul>
+<TMPL_IF HTML5></nav><TMPL_ELSE></div></TMPL_IF>
+</TMPL_IF>
+
+<TMPL_IF OTHERLANGUAGES>
+<TMPL_IF HTML5><nav id="otherlanguages"><TMPL_ELSE><div id="otherlanguages"></TMPL_IF>
+<ul>
+<TMPL_LOOP OTHERLANGUAGES>
+<li>
+<a href="<TMPL_VAR URL>"><TMPL_VAR LANGUAGE></a>
+<TMPL_IF MASTER>
+(master)
+<TMPL_ELSE>
+&nbsp;(<TMPL_VAR PERCENT>%)
+</TMPL_IF>
+</li>
+</TMPL_LOOP>
+</ul>
+<TMPL_IF HTML5></nav><TMPL_ELSE></div></TMPL_IF>
+</TMPL_IF>
+
+<TMPL_UNLESS DYNAMIC>
+<TMPL_VAR TRAILS>
+</TMPL_UNLESS>
+
+<TMPL_IF HTML5></section><TMPL_ELSE></div></TMPL_IF>
+
+<TMPL_UNLESS DYNAMIC>
+<TMPL_IF SIDEBAR>
+<TMPL_IF HTML5><aside class="sidebar"><TMPL_ELSE><div class="sidebar"></TMPL_IF>
+<TMPL_VAR SIDEBAR>
+<TMPL_IF HTML5></aside><TMPL_ELSE></div></TMPL_IF>
+</TMPL_IF>
+</TMPL_UNLESS>
+
+<div id="pagebody">
+
+<TMPL_IF HTML5><section id="content"><TMPL_ELSE><div id="content"></TMPL_IF>
+<TMPL_VAR CONTENT>
+
+<TMPL_IF HTML5></section><TMPL_ELSE></div></TMPL_IF>
+
+<TMPL_IF ENCLOSURE>
+<TMPL_IF HTML5><section id="enclosure"><TMPL_ELSE><div id="enclosure"></TMPL_IF>
+<a href="<TMPL_VAR ENCLOSURE>">Download</a>
+<TMPL_IF HTML5></section><TMPL_ELSE></div></TMPL_IF>
+</TMPL_IF>
+
+<TMPL_UNLESS DYNAMIC>
+<TMPL_IF COMMENTS>
+<TMPL_IF HTML5><section id="comments"><TMPL_ELSE><div id="comments"></TMPL_IF>
+<TMPL_VAR COMMENTS>
+<TMPL_IF ADDCOMMENTURL>
+<div class="addcomment">
+<a href="<TMPL_VAR ADDCOMMENTURL>">Add a comment</a>
+</div>
+<TMPL_ELSE>
+<div class="addcomment">Comments on this page are closed.</div>
+</TMPL_IF>
+<TMPL_IF HTML5></section><TMPL_ELSE></div></TMPL_IF>
+</TMPL_IF>
+</TMPL_UNLESS>
+
+</div>
+
+<TMPL_IF HTML5><footer id="footer" class="pagefooter"><TMPL_ELSE><div id="footer" class="pagefooter"></TMPL_IF>
+<TMPL_UNLESS DYNAMIC>
+
+<TMPL_IF FLATTR_ENABLED>
+<TMPL_IF HTML5><nav id="flattr"><TMPL_ELSE><div id="flattr"></TMPL_IF>
+<TMPL_IF FLATTR_THING_ID_SET>
+<a href="https://flattr.com/thing/<TMPL_VAR FLATTR_THING_ID ESCAPE=URL>" target="_blank">
+<TMPL_ELSE>
+<a href="https://flattr.com/submit/auto?user_id=<TMPL_VAR FLATTR_USER ESCAPE=URL>&amp;url=<TMPL_VAR FLATTR_THING_URL ESCAPE=URL>&amp;category=text&amp;language=en&amp;title=<TMPL_VAR TITLE ESCAPE=URL>" target="_blank">
+</TMPL_IF>
+<img style="vertical-align:middle" src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" />
+</a>
+<TMPL_IF HTML5></nav><TMPL_ELSE></div></TMPL_IF>
+</TMPL_IF>
+
+<TMPL_IF HTML5><nav id="pageinfo"><TMPL_ELSE><div id="pageinfo"></TMPL_IF>
+
+<TMPL_VAR TRAILS>
+
+<TMPL_IF TAGS>
+<TMPL_IF HTML5><nav class="tags"><TMPL_ELSE><div class="tags"></TMPL_IF>
+Tags:
+<TMPL_LOOP TAGS>
+<TMPL_VAR LINK>
+</TMPL_LOOP>
+<TMPL_IF HTML5></nav><TMPL_ELSE></div></TMPL_IF>
+</TMPL_IF>
+
+<TMPL_IF BACKLINKS>
+<TMPL_IF HTML5><nav id="backlinks"><TMPL_ELSE><div id="backlinks"></TMPL_IF>
+Links:
+<TMPL_LOOP BACKLINKS>
+<a href="<TMPL_VAR URL>"><TMPL_VAR PAGE></a>
+</TMPL_LOOP>
+<TMPL_IF MORE_BACKLINKS>
+<span class="popup">...
+<span class="balloon">
+<TMPL_LOOP MORE_BACKLINKS>
+<a href="<TMPL_VAR URL>"><TMPL_VAR PAGE></a>
+</TMPL_LOOP>
+</span>
+</span>
+</TMPL_IF>
+<TMPL_IF HTML5></nav><TMPL_ELSE></div></TMPL_IF>
+</TMPL_IF>
+
+<TMPL_IF COPYRIGHT>
+<div class="pagecopyright">
+<a name="pagecopyright"></a>
+<TMPL_VAR COPYRIGHT>
+</div>
+</TMPL_IF>
+
+<TMPL_IF LICENSE>
+<div class="pagelicense">
+<a name="pagelicense"></a>
+License: <TMPL_VAR LICENSE>
+</div>
+</TMPL_IF>
+
+<div class="pagedate">
+Last edited <TMPL_VAR MTIME>
+<!-- Created <TMPL_VAR CTIME> -->
+</div>
+
+<TMPL_IF HTML5></nav><TMPL_ELSE></div></TMPL_IF>
+<TMPL_IF EXTRAFOOTER><TMPL_VAR EXTRAFOOTER></TMPL_IF>
+</TMPL_UNLESS>
+<!-- from <TMPL_VAR WIKINAME> -->
+<TMPL_IF HTML5></footer><TMPL_ELSE></div></TMPL_IF>
+
+<TMPL_IF HTML5></article><TMPL_ELSE></div></TMPL_IF>
+
+</body>
+</html>
This page took 0.029608 seconds and 4 git commands to generate.