From 9c0ec843dda48af69ad30d09443157a6b0936511 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Mon, 13 Jun 2011 21:52:45 +0200 Subject: Add branding (clone of sidebar). --- IkiWiki/Plugin/branding.pm | 110 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 IkiWiki/Plugin/branding.pm 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 +# Heavily based on Sidebar by Tuomo Valkonen + +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 -- cgit v1.2.3