2006-07-27 23:38:02 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Ikiwiki tag plugin.
|
|
|
|
package IkiWiki::Plugin::tag;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-07-27 23:38:02 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "getopt", id => "tag", call => \&getopt);
|
2008-07-26 06:38:13 +02:00
|
|
|
hook(type => "getsetup", id => "tag", call => \&getsetup);
|
2007-09-20 20:59:27 +02:00
|
|
|
hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
|
|
|
|
hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-27 23:38:02 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getopt () {
|
2006-07-28 07:26:49 +02:00
|
|
|
eval q{use Getopt::Long};
|
2006-11-08 22:03:33 +01:00
|
|
|
error($@) if $@;
|
2006-07-28 07:26:49 +02:00
|
|
|
Getopt::Long::Configure('pass_through');
|
2006-09-10 00:50:27 +02:00
|
|
|
GetOptions("tagbase=s" => \$config{tagbase});
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-28 07:26:49 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-07-26 06:38:13 +02:00
|
|
|
return
|
2008-08-04 01:35:35 +02:00
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2008-07-26 06:38:13 +02:00
|
|
|
tagbase => {
|
|
|
|
type => "string",
|
|
|
|
example => "tag",
|
|
|
|
description => "parent page tags are located under",
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
2010-01-30 18:22:32 +01:00
|
|
|
tag_autocreate => {
|
|
|
|
type => "boolean",
|
2010-04-22 02:39:20 +02:00
|
|
|
example => 1,
|
2010-04-17 21:40:39 +02:00
|
|
|
description => "autocreate new tag pages?",
|
2010-01-30 18:22:32 +01:00
|
|
|
safe => 1,
|
2010-04-17 21:40:39 +02:00
|
|
|
rebuild => undef,
|
2010-01-30 18:22:32 +01:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-26 06:38:13 +02:00
|
|
|
|
2010-04-17 22:01:41 +02:00
|
|
|
sub taglink ($) {
|
2006-07-29 09:25:17 +02:00
|
|
|
my $tag=shift;
|
2010-04-17 21:48:00 +02:00
|
|
|
|
2010-04-22 02:15:11 +02:00
|
|
|
if ($tag !~ m{^/} &&
|
2006-09-10 00:50:27 +02:00
|
|
|
defined $config{tagbase}) {
|
2008-10-21 00:17:03 +02:00
|
|
|
$tag="/".$config{tagbase}."/".$tag;
|
2008-11-11 01:48:58 +01:00
|
|
|
$tag=~y#/#/#s; # squash dups
|
2006-07-29 09:25:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tag;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-29 09:25:17 +02:00
|
|
|
|
2010-04-17 22:01:41 +02:00
|
|
|
sub htmllink_tag ($$$;@) {
|
2008-09-26 02:25:07 +02:00
|
|
|
my $page=shift;
|
|
|
|
my $destpage=shift;
|
|
|
|
my $tag=shift;
|
|
|
|
my %opts=@_;
|
|
|
|
|
2010-04-17 22:01:41 +02:00
|
|
|
return htmllink($page, $destpage, taglink($tag), %opts);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-09-26 02:25:07 +02:00
|
|
|
|
2010-01-30 18:22:32 +01:00
|
|
|
sub gentag ($) {
|
|
|
|
my $tag=shift;
|
2010-04-17 22:01:41 +02:00
|
|
|
|
2010-04-22 02:39:20 +02:00
|
|
|
if ($config{tag_autocreate} ||
|
|
|
|
($config{tagbase} && ! defined $config{tag_autocreate})) {
|
2010-04-17 22:01:41 +02:00
|
|
|
my $tagpage=taglink($tag);
|
|
|
|
if ($tagpage=~/^\.\/(.*)/) {
|
|
|
|
$tagpage=$1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$tagpage=~s/^\///;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $tagfile = newpagefile($tagpage, $config{default_pageext});
|
2010-02-03 04:29:10 +01:00
|
|
|
|
2010-04-17 21:43:58 +02:00
|
|
|
add_autofile($tagfile, "tag", sub {
|
2010-04-22 02:08:23 +02:00
|
|
|
my $message=sprintf(gettext("creating tag page %s"), $tagpage);
|
2010-04-21 22:13:37 +02:00
|
|
|
debug($message);
|
2010-01-30 18:22:32 +01:00
|
|
|
|
2010-04-17 19:35:15 +02:00
|
|
|
my $template=template("autotag.tmpl");
|
2010-04-22 02:08:23 +02:00
|
|
|
$template->param(tagname => IkiWiki::basename($tag));
|
|
|
|
$template->param(tag => $tag);
|
2010-04-17 19:35:15 +02:00
|
|
|
writefile($tagfile, $config{srcdir}, $template->output);
|
2010-04-21 22:13:37 +02:00
|
|
|
if ($config{rcs}) {
|
|
|
|
IkiWiki::disable_commit_hook();
|
|
|
|
IkiWiki::rcs_add($tagfile);
|
2010-06-23 23:35:21 +02:00
|
|
|
IkiWiki::rcs_commit_staged(message => $message);
|
2010-04-21 22:13:37 +02:00
|
|
|
IkiWiki::enable_commit_hook();
|
|
|
|
}
|
2010-04-17 19:35:15 +02:00
|
|
|
});
|
2010-01-30 18:22:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess_tag (@) {
|
2006-07-27 23:38:02 +02:00
|
|
|
if (! @_) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
my %params=@_;
|
|
|
|
my $page = $params{page};
|
|
|
|
delete $params{page};
|
2006-07-28 01:47:13 +02:00
|
|
|
delete $params{destpage};
|
2007-03-07 06:53:47 +01:00
|
|
|
delete $params{preview};
|
2006-07-27 23:38:02 +02:00
|
|
|
|
|
|
|
foreach my $tag (keys %params) {
|
2008-09-27 20:14:36 +02:00
|
|
|
$tag=linkpage($tag);
|
2010-04-17 18:50:23 +02:00
|
|
|
|
2006-07-27 23:38:02 +02:00
|
|
|
# hidden WikiLink
|
2010-04-17 22:01:41 +02:00
|
|
|
add_link($page, taglink($tag), 'tag');
|
2010-04-17 18:50:23 +02:00
|
|
|
|
2010-01-30 18:22:32 +01:00
|
|
|
gentag($tag);
|
2006-07-27 23:38:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-27 23:38:02 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess_taglink (@) {
|
2007-09-20 20:59:27 +02:00
|
|
|
if (! @_) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
my %params=@_;
|
2007-09-22 19:07:51 +02:00
|
|
|
return join(" ", map {
|
|
|
|
if (/(.*)\|(.*)/) {
|
2008-09-27 20:14:36 +02:00
|
|
|
my $tag=linkpage($2);
|
2010-04-17 22:01:41 +02:00
|
|
|
add_link($params{page}, taglink($tag), 'tag');
|
2010-04-17 21:48:00 +02:00
|
|
|
gentag($tag);
|
2010-04-17 22:01:41 +02:00
|
|
|
return htmllink_tag($params{page}, $params{destpage}, $tag,
|
2008-09-27 20:14:36 +02:00
|
|
|
linktext => pagetitle($1));
|
2007-09-22 19:07:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2008-09-27 20:14:36 +02:00
|
|
|
my $tag=linkpage($_);
|
2010-04-17 22:01:41 +02:00
|
|
|
add_link($params{page}, taglink($tag), 'tag');
|
2010-04-17 21:48:00 +02:00
|
|
|
gentag($tag);
|
2010-04-17 22:01:41 +02:00
|
|
|
return htmllink_tag($params{page}, $params{destpage}, $tag);
|
2007-09-22 19:07:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
grep {
|
|
|
|
$_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
|
|
|
|
} keys %params);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-09-20 20:59:27 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub pagetemplate (@) {
|
2006-07-28 01:41:58 +02:00
|
|
|
my %params=@_;
|
|
|
|
my $page=$params{page};
|
|
|
|
my $destpage=$params{destpage};
|
|
|
|
my $template=$params{template};
|
2006-07-28 01:08:03 +02:00
|
|
|
|
2010-04-02 01:28:02 +02:00
|
|
|
my $tags = $typedlinks{$page}{tag};
|
|
|
|
|
2006-07-28 02:21:13 +02:00
|
|
|
$template->param(tags => [
|
2006-07-29 09:25:17 +02:00
|
|
|
map {
|
2010-04-17 22:01:41 +02:00
|
|
|
link => htmllink_tag($page, $destpage, $_, rel => "tag")
|
2010-04-02 01:28:02 +02:00
|
|
|
}, sort keys %$tags
|
|
|
|
]) if defined $tags && %$tags && $template->query(name => "tags");
|
2006-07-29 09:25:17 +02:00
|
|
|
|
2007-01-14 21:14:36 +01:00
|
|
|
if ($template->query(name => "categories")) {
|
|
|
|
# It's an rss/atom template. Add any categories.
|
2010-04-02 01:28:02 +02:00
|
|
|
if (defined $tags && %$tags) {
|
2007-02-14 05:05:08 +01:00
|
|
|
$template->param(categories => [map { category => $_ },
|
2010-04-02 01:28:02 +02:00
|
|
|
sort keys %$tags]);
|
2006-07-29 09:25:17 +02:00
|
|
|
}
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-28 01:08:03 +02:00
|
|
|
|
2009-02-20 00:49:30 +01:00
|
|
|
package IkiWiki::PageSpec;
|
|
|
|
|
2009-02-20 00:54:25 +01:00
|
|
|
sub match_tagged ($$;@) {
|
2010-04-22 01:46:27 +02:00
|
|
|
my $page=shift;
|
2010-04-22 01:49:11 +02:00
|
|
|
my $glob=IkiWiki::Plugin::tag::taglink(shift);
|
|
|
|
return match_link($page, $glob, linktype => 'tag', @_);
|
2009-02-20 00:49:30 +01:00
|
|
|
}
|
|
|
|
|
2006-07-27 23:38:02 +02:00
|
|
|
1
|