#!/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 2.00;

sub import { #{{{
	hook(type => "pagetemplate", id => "topbar", call => \&pagetemplate);
} # }}}

sub topbar_content ($) { #{{{
	my $page=shift;
	
	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=readfile(srcfile($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 $page=$params{page};
	my $template=$params{template};
	
	if ($template->query(name => "topbar")) {
		my $content=topbar_content($page);
		if (defined $content && length $content) {
		        $template->param(topbar => $content);
		}
	}
} # }}}

1