#!/usr/bin/perl
# Ikiwiki tag plugin.
package IkiWiki::Plugin::tag;

use warnings;
use strict;
use IkiWiki 3.00;

sub import {
	hook(type => "getopt", id => "tag", call => \&getopt);
	hook(type => "getsetup", id => "tag", call => \&getsetup);
	hook(type => "checkconfig", id => "tag", call => \&checkconfig);
	hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
	hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
	hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
}

sub getopt () {
	eval q{use Getopt::Long};
	error($@) if $@;
	Getopt::Long::Configure('pass_through');
	GetOptions("tagbase=s" => \$config{tagbase});
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => undef,
		},
		tagbase => {
			type => "string",
			example => "tag",
			description => "parent page tags are located under",
			safe => 1,
			rebuild => 1,
		},
		# TODO make this a hash so that we can have the user specify the linktype, tagbase and template param for each custom tag type
		tagtypes => {
			type => "string",
			example => ["category", "column"],
			description => "extra categorization types: they define a new directive each, with a corresponding template parameter, link type and tag base",
			safe => 1,
			rebuild => 1,
		}
}

sub tagtypes() {
	if (defined $config{tagtypes}) {
		return @{$config{tagtypes}};
	} else {
		return ();
	}
}

sub checkconfig() {
	foreach my $tagtype (tagtypes()) {
		debug("defining '$tagtype' tagtype (directive, linktype, tagbase, template param)");
		hook(type => "preprocess", id => $tagtype, call => sub { preprocess_custom_tag($tagtype, @_) });
	}
}

sub tagpage ($$) {
	my $tag=shift;
	my $type=shift;
			
	if (defined $type && $type ne "tag") {
		$tag="/$type/$tag";
	} elsif ($tag !~ m{^\.?/} &&
	    defined $config{tagbase}) {
		$tag="/".$config{tagbase}."/".$tag;
		$tag=~y#/#/#s; # squash dups
	}

	return $tag;
}

sub taglink ($$$$;@) {
	my $page=shift;
	my $destpage=shift;
	my $tag=shift;
	my $tagtype=shift;
	my %opts=@_;

	return htmllink($page, $destpage, tagpage($tag, $tagtype), %opts);
}

sub preprocess_tag (@) {
	if (! @_) {
		return "";
	}
	my %params=@_;
	my $page = $params{page};
	my $tagtype = defined $params{tagtype} ? $params{tagtype} : 'tag' ;
	delete $params{page};
	delete $params{tagtype};
	delete $params{destpage};
	delete $params{preview};

	foreach my $tag (keys %params) {
		$tag=linkpage($tag);
		# hidden WikiLink
		add_link($page, tagpage($tag, $tagtype), $tagtype);
	}
		
	return "";
}

sub preprocess_custom_tag($;@) {
	my $tagtype = shift;
	if (! @_) {
		return "";
	}
	my %params=@_;
	$params{tagtype} ||=  $tagtype;
	preprocess_tag(%params);
}

sub preprocess_taglink (@) {
	if (! @_) {
		return "";
	}
	my %params=@_;
	my $tagtype = 'tag'; # TODO customizable?
	return join(" ", map {
		if (/(.*)\|(.*)/) {
			my $tag=linkpage($2);
			add_link($params{page}, tagpage($tag, $tagtype), $tagtype);
			return taglink($params{page}, $params{destpage}, $tag, $tagtype,
				linktext => pagetitle($1));
		}
		else {
			my $tag=linkpage($_);
			add_link($params{page}, tagpage($tag, $tagtype), $tagtype);
			return taglink($params{page}, $params{destpage}, $tag, $tagtype);
		}
	}
	grep {
		$_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
	} keys %params);
}

sub pagetemplate (@) {
	my %params=@_;
	my $page=$params{page};
	my $destpage=$params{destpage};
	my $template=$params{template};

	my $tags = $typedlinks{$page}{tag};
	my @taglist = ();

	if (defined $tags && %$tags) {
		push @taglist, keys %$tags;
		$template->param(tags => [
			map {
				link => taglink($page, $destpage, $_, 'tag', rel => "tag")
			}, sort keys %$tags
		]) if $template->query(name => "tags");
	}

	# custom tagtypes have a separate treatment for a number of reasons, including the fact that
	# their template parameter name is different from the tag/link type ('tags' vs 'tag')
	# notice the use of rel => $tagtype 
	foreach my $tagtype (tagtypes()) {
		$tags = $typedlinks{$page}{$tagtype};
		if (defined $tags && %$tags) {
			push @taglist, keys %$tags;
			$template->param($tagtype => [
				map {
					link => taglink($page, $destpage, $_, $tagtype, rel => $tagtype)
				}, sort keys %$tags
			]) if $template->query(name => $tagtype);
		}
	}

	debug("full tag list: " . join(',', @taglist)) if @taglist;

	if ($template->query(name => "categories")) {
		# It's an rss/atom template. Add any categories.
		if (@taglist) {
			$template->param(categories => [map { category => $_ },
				sort @taglist]);
		}
	}
}

package IkiWiki::PageSpec;

sub match_tagged ($$;@) {
	my %params = @_;
	my $tagtype = defined $params{tagtype} ? $params{tagtype} : 'tag' ;
	return match_link($_[0], IkiWiki::Plugin::tag::tagpage($_[1], $tagtype), linktype => $tagtype);
}

1
