From c0228aceb072537da9744f87dc5a9c85d6a3fff5 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Wed, 19 May 2010 15:47:51 +0200 Subject: [PATCH] WIP --- IkiWiki/Plugin/tag.pm | 111 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 13 deletions(-) diff --git a/IkiWiki/Plugin/tag.pm b/IkiWiki/Plugin/tag.pm index 86e95cd24..f915007f7 100644 --- a/IkiWiki/Plugin/tag.pm +++ b/IkiWiki/Plugin/tag.pm @@ -9,6 +9,7 @@ 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); @@ -41,14 +42,86 @@ sub getsetup () { safe => 1, rebuild => undef, }, + tagtypes => { + type => "string", + # TODO document that this can be either an array or a hash with hashes as values + description => "extra categorization types", + example => "[category, column]" + } +} + +sub tagtypes() { + if (defined $config{tagtypes}) { + my $tagtypes = $config{tagtypes}; + if (ref($tagtypes) eq 'HASH') { + return %$tagtypes; + } else { + my %hash = (); + foreach my $tagtype (@$tagtypes) { + $hash{$tagtype} = {}; + } + return %hash; + } + } else { + return (); + } +} + +# Custom tagtypes have an automatic tagbase equal to the tagtype itself, +# unless overridden +sub tagbase($) { + my $tagtype=shift; + if ($tagtype eq 'tag') { + if (defined $config{tagbase}) { + return $config{tagbase}; + } else { + return undef; + } + } else { + my %tagtypes = tagtypes(); + if (exists $tagtypes{tagbase}) { + return $tagtypes{tagbase}; + } else { + return $tagtype; + } + } } -sub taglink ($) { +sub tag_autocreate($) { + my $tagtype=shift; + if ($tagtype eq 'tag') { + return $config{tag_autocreate}; + } else { + my %tagtypes = tagtypes(); + if (exists $tagtypes{tag_autocreate}) { + return $tagtypes{tag_autocreate}; + } else { + return $config{tag_autocreate}; + } + } +} + +sub checkconfig() { + my %tagtypes = tagtypes(); + while (my ($tagtype, $conf) = each %tagtypes) { + my $directive = (defined $conf->{directive}) ? $conf->{directive} : $tagtype ; + if ($directive) { + debug("defining '$tagtype' tagtype with directive '$directive'"); + hook(type => "preprocess", id => $directive, call => sub { preprocess_custom_tag($tagtype, @_) }); + } else { + debug("defining '$tagtype' tagtype with no directive"); + } + } +} + +sub taglink ($;$) { my $tag=shift; + my $tagtype=shift || 'tag'; + my $tagbase=tagbase($tagtype); + - if ($tag !~ m{^/} && - defined $config{tagbase}) { - $tag="/".$config{tagbase}."/".$tag; + if ($tag !~ m{^/} && $tagbase) { + $tag="/".$tagbase."/".$tag; $tag=~y#/#/#s; # squash dups } @@ -64,12 +137,18 @@ sub htmllink_tag ($$$;@) { return htmllink($page, $destpage, taglink($tag), %opts); } -sub gentag ($) { +sub gentag ($;$) { my $tag=shift; + my $tagtype=shift || 'tag'; - if ($config{tag_autocreate} || - ($config{tagbase} && ! defined $config{tag_autocreate})) { - my $tagpage=taglink($tag); + my $debug_ac = tag_autocreate($tagtype); + my $debug_tb = tagbase($tagtype); + debug("gentag $tag;$tagtype ac=$debug_ac tb=$debug_tb"); + + if (tag_autocreate($tagtype) || + (tagbase($tagtype) && ! tag_autocreate($tagtype))) { + debug("gentag $tag;$tagtype TRUE"); + my $tagpage=taglink($tag,$tagtype); if ($tagpage=~/^\.\/(.*)/) { $tagpage=$1; } @@ -79,13 +158,14 @@ sub gentag ($) { my $tagfile = newpagefile($tagpage, $config{default_pageext}); - add_autofile($tagfile, "tag", sub { - my $message=sprintf(gettext("creating tag page %s"), $tagpage); + add_autofile($tagfile, $tagtype, sub { + my $message=sprintf(gettext("creating %s page %s"), $tagtype, $tagpage); debug($message); my $template=template("autotag.tmpl"); $template->param(tagname => IkiWiki::basename($tag)); $template->param(tag => $tag); + $template->param(tagtype => $tagtype); writefile($tagfile, $config{srcdir}, $template->output); if ($config{rcs}) { IkiWiki::disable_commit_hook(); @@ -97,7 +177,8 @@ sub gentag ($) { } } -sub preprocess_tag (@) { +sub preprocess_custom_tag ($@) { + my $tagtype=shift; if (! @_) { return ""; } @@ -111,14 +192,18 @@ sub preprocess_tag (@) { $tag=linkpage($tag); # hidden WikiLink - add_link($page, taglink($tag), 'tag'); + add_link($page, taglink($tag,$tagtype), $tagtype); - gentag($tag); + gentag($tag, $tagtype); } return ""; } +sub preprocess_tag(@) { + return preprocess_custom_tag('tag', @_); +} + sub preprocess_taglink (@) { if (! @_) { return ""; -- 2.32.0.93.g670b81a890