summaryrefslogtreecommitdiff
path: root/perl/IkiWiki/Plugin/topbar.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perl/IkiWiki/Plugin/topbar.pm')
-rw-r--r--perl/IkiWiki/Plugin/topbar.pm110
1 files changed, 110 insertions, 0 deletions
diff --git a/perl/IkiWiki/Plugin/topbar.pm b/perl/IkiWiki/Plugin/topbar.pm
new file mode 100644
index 0000000..2705161
--- /dev/null
+++ b/perl/IkiWiki/Plugin/topbar.pm
@@ -0,0 +1,110 @@
+#!/usr/bin/perl
+# Topbar plugin.
+# by Jonas Smedegaard <dr@jones.dk>
+# Heavily based on Sidebar by Tuomo Valkonen <tuomov at iki dot fi>
+
+package IkiWiki::Plugin::topbar;
+
+use warnings;
+use strict;
+use IkiWiki 3.00;
+
+sub import {
+ hook(type => "getsetup", id => "topbar", call => \&getsetup);
+ hook(type => "preprocess", id => "topbar", call => \&preprocess);
+ hook(type => "pagetemplate", id => "topbar", call => \&pagetemplate);
+}
+
+sub getsetup () {
+ return
+ plugin => {
+ safe => 1,
+ rebuild => 1,
+ },
+ global_topbars => {
+ type => "boolean",
+ example => 1,
+ description => "show topbar page on all pages?",
+ safe => 1,
+ rebuild => 1,
+ },
+}
+
+my %pagetopbar;
+
+sub preprocess (@) {
+ my %params=@_;
+
+ my $page=$params{page};
+ return "" unless $page eq $params{destpage};
+
+ if (! defined $params{content}) {
+ $pagetopbar{$page}=undef;
+ }
+ else {
+ my $file = $pagesources{$page};
+ my $type = pagetype($file);
+
+ $pagetopbar{$page}=
+ IkiWiki::htmlize($page, $page, $type,
+ IkiWiki::linkify($page, $page,
+ IkiWiki::preprocess($page, $page, $params{content})));
+ }
+
+ return "";
+}
+
+my $oldfile;
+my $oldcontent;
+
+sub topbar_content ($) {
+ my $page=shift;
+
+ return delete $pagetopbar{$page} if defined $pagetopbar{$page};
+
+ return if ! exists $pagetopbar{$page} &&
+ defined $config{global_topbars} && ! $config{global_topbars};
+
+ my $topbar_page=bestlink($page, "topbar") || return;
+ my $topbar_file=$pagesources{$topbar_page} || return;
+ my $topbar_type=pagetype($topbar_file);
+
+ if (defined $topbar_type) {
+ # FIXME: This isn't quite right; it won't take into account
+ # adding a new topbar page. So adding such a page
+ # currently requires a wiki rebuild.
+ add_depends($page, $topbar_page);
+
+ my $content;
+ if (defined $oldfile && $topbar_file eq $oldfile) {
+ $content=$oldcontent;
+ }
+ else {
+ $content=readfile(srcfile($topbar_file));
+ $oldcontent=$content;
+ $oldfile=$topbar_file;
+ }
+
+ return unless length $content;
+ return IkiWiki::htmlize($topbar_page, $page, $topbar_type,
+ IkiWiki::linkify($topbar_page, $page,
+ IkiWiki::preprocess($topbar_page, $page,
+ IkiWiki::filter($topbar_page, $page, $content))));
+ }
+
+}
+
+sub pagetemplate (@) {
+ my %params=@_;
+
+ my $template=$params{template};
+ if ($params{destpage} eq $params{page} &&
+ $template->query(name => "topbar")) {
+ my $content=topbar_content($params{destpage});
+ if (defined $content && length $content) {
+ $template->param(topbar => $content);
+ }
+ }
+}
+
+1