- #!/usr/bin/perl
- # Farbar plugin.
- # by Jonas Smedegaard <dr@jones.dk>
- # Heavily based on Sidebar by Tuomo Valkonen <tuomov at iki dot fi>
- package IkiWiki::Plugin::farbar;
- use warnings;
- use strict;
- use IkiWiki 3.00;
- sub import {
- hook(type => "getsetup", id => "farbar", call => \&getsetup);
- hook(type => "preprocess", id => "farbar", call => \&preprocess);
- hook(type => "pagetemplate", id => "farbar", call => \&pagetemplate);
- }
- sub getsetup () {
- return
- plugin => {
- safe => 1,
- rebuild => 1,
- },
- global_farbars => {
- type => "boolean",
- example => 1,
- description => "show farbar page on all pages?",
- safe => 1,
- rebuild => 1,
- },
- }
- my %pagefarbar;
- sub preprocess (@) {
- my %params=@_;
- my $page=$params{page};
- return "" unless $page eq $params{destpage};
-
- if (! defined $params{content}) {
- $pagefarbar{$page}=undef;
- }
- else {
- my $file = $pagesources{$page};
- my $type = pagetype($file);
- $pagefarbar{$page}=
- IkiWiki::htmlize($page, $page, $type,
- IkiWiki::linkify($page, $page,
- IkiWiki::preprocess($page, $page, $params{content})));
- }
- return "";
- }
- my $oldfile;
- my $oldcontent;
- sub farbar_content ($) {
- my $page=shift;
-
- return delete $pagefarbar{$page} if defined $pagefarbar{$page};
- return if ! exists $pagefarbar{$page} &&
- defined $config{global_farbars} && ! $config{global_farbars};
- my $farbar_page=bestlink($page, "farbar") || return;
- my $farbar_file=$pagesources{$farbar_page} || return;
- my $farbar_type=pagetype($farbar_file);
-
- if (defined $farbar_type) {
- # FIXME: This isn't quite right; it won't take into account
- # adding a new farbar page. So adding such a page
- # currently requires a wiki rebuild.
- add_depends($page, $farbar_page);
- my $content;
- if (defined $oldfile && $farbar_file eq $oldfile) {
- $content=$oldcontent;
- }
- else {
- $content=readfile(srcfile($farbar_file));
- $oldcontent=$content;
- $oldfile=$farbar_file;
- }
- return unless length $content;
- return IkiWiki::htmlize($farbar_page, $page, $farbar_type,
- IkiWiki::linkify($farbar_page, $page,
- IkiWiki::preprocess($farbar_page, $page,
- IkiWiki::filter($farbar_page, $page, $content))));
- }
- }
- sub pagetemplate (@) {
- my %params=@_;
- my $template=$params{template};
- if ($params{destpage} eq $params{page} &&
- $template->query(name => "farbar")) {
- my $content=farbar_content($params{destpage});
- if (defined $content && length $content) {
- $template->param(farbar => $content);
- }
- }
- }
- 1
|