summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2011-06-13 21:52:45 +0200
committerJonas Smedegaard <dr@jones.dk>2013-10-13 00:52:57 +0200
commit9c0ec843dda48af69ad30d09443157a6b0936511 (patch)
tree8c34a1f2b5d9cec00ba8ae7c7bcebf3755ab4679
parentf0e36d348497976918bbf941c44e54888115ea3d (diff)
Add branding (clone of sidebar).
-rw-r--r--IkiWiki/Plugin/branding.pm110
1 files changed, 110 insertions, 0 deletions
diff --git a/IkiWiki/Plugin/branding.pm b/IkiWiki/Plugin/branding.pm
new file mode 100644
index 0000000..653eea4
--- /dev/null
+++ b/IkiWiki/Plugin/branding.pm
@@ -0,0 +1,110 @@
+#!/usr/bin/perl
+# Branding plugin.
+# by Jonas Smedegaard <dr@jones.dk>
+# Heavily based on Sidebar by Tuomo Valkonen <tuomov at iki dot fi>
+
+package IkiWiki::Plugin::branding;
+
+use warnings;
+use strict;
+use IkiWiki 3.00;
+
+sub import {
+ hook(type => "getsetup", id => "branding", call => \&getsetup);
+ hook(type => "preprocess", id => "branding", call => \&preprocess);
+ hook(type => "pagetemplate", id => "branding", call => \&pagetemplate);
+}
+
+sub getsetup () {
+ return
+ plugin => {
+ safe => 1,
+ rebuild => 1,
+ },
+ global_brandings => {
+ type => "boolean",
+ example => 1,
+ description => "show branding page on all pages?",
+ safe => 1,
+ rebuild => 1,
+ },
+}
+
+my %pagebranding;
+
+sub preprocess (@) {
+ my %params=@_;
+
+ my $page=$params{page};
+ return "" unless $page eq $params{destpage};
+
+ if (! defined $params{content}) {
+ $pagebranding{$page}=undef;
+ }
+ else {
+ my $file = $pagesources{$page};
+ my $type = pagetype($file);
+
+ $pagebranding{$page}=
+ IkiWiki::htmlize($page, $page, $type,
+ IkiWiki::linkify($page, $page,
+ IkiWiki::preprocess($page, $page, $params{content})));
+ }
+
+ return "";
+}
+
+my $oldfile;
+my $oldcontent;
+
+sub branding_content ($) {
+ my $page=shift;
+
+ return delete $pagebranding{$page} if defined $pagebranding{$page};
+
+ return if ! exists $pagebranding{$page} &&
+ defined $config{global_brandings} && ! $config{global_brandings};
+
+ my $branding_page=bestlink($page, "branding") || return;
+ my $branding_file=$pagesources{$branding_page} || return;
+ my $branding_type=pagetype($branding_file);
+
+ if (defined $branding_type) {
+ # FIXME: This isn't quite right; it won't take into account
+ # adding a new branding page. So adding such a page
+ # currently requires a wiki rebuild.
+ add_depends($page, $branding_page);
+
+ my $content;
+ if (defined $oldfile && $branding_file eq $oldfile) {
+ $content=$oldcontent;
+ }
+ else {
+ $content=readfile(srcfile($branding_file));
+ $oldcontent=$content;
+ $oldfile=$branding_file;
+ }
+
+ return unless length $content;
+ return IkiWiki::htmlize($branding_page, $page, $branding_type,
+ IkiWiki::linkify($branding_page, $page,
+ IkiWiki::preprocess($branding_page, $page,
+ IkiWiki::filter($branding_page, $page, $content))));
+ }
+
+}
+
+sub pagetemplate (@) {
+ my %params=@_;
+
+ my $template=$params{template};
+ if ($params{destpage} eq $params{page} &&
+ $template->query(name => "branding")) {
+ my $content=branding_content($params{destpage});
+ if (defined $content && length $content) {
+ $template->param(branding => $content);
+ }
+ }
+}
+
+1