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 {
|
2010-12-26 23:19:58 +01:00
|
|
|
hook(type => "checkconfig", id => "tag", call => \&checkconfig);
|
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);
|
2010-12-26 00:01:08 +01:00
|
|
|
|
|
|
|
IkiWiki::loadplugin("transient");
|
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
|
|
|
},
|
2010-12-26 00:01:08 +01:00
|
|
|
tag_autocreate_commit => {
|
|
|
|
type => "boolean",
|
|
|
|
example => 1,
|
|
|
|
default => 1,
|
|
|
|
description => "commit autocreated tag pages",
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-26 06:38:13 +02:00
|
|
|
|
2010-12-26 23:19:58 +01:00
|
|
|
sub checkconfig () {
|
|
|
|
if (! defined $config{tag_autocreate_commit}) {
|
|
|
|
$config{tag_autocreate_commit} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-01-04 19:26:21 +01:00
|
|
|
# Returns a tag name from a tag link
|
|
|
|
sub tagname ($) {
|
|
|
|
my $tag=shift;
|
|
|
|
if (defined $config{tagbase}) {
|
2011-01-04 20:59:00 +01:00
|
|
|
$tag =~ s!^/\Q$config{tagbase}\E/!!;
|
2011-01-04 19:26:21 +01:00
|
|
|
} else {
|
2011-01-04 20:59:28 +01:00
|
|
|
$tag =~ s!^\.?/!!;
|
2011-01-04 19:26:21 +01:00
|
|
|
}
|
2011-01-05 07:20:47 +01:00
|
|
|
return pagetitle($tag, 1);
|
2011-01-04 19:26:21 +01: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/^\///;
|
|
|
|
}
|
2011-04-30 22:30:07 +02:00
|
|
|
if (exists $IkiWiki::pagecase{lc $tagpage}) {
|
|
|
|
$tagpage=$IkiWiki::pagecase{lc $tagpage}
|
|
|
|
}
|
2010-04-17 22:01:41 +02:00
|
|
|
|
|
|
|
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");
|
2011-01-04 19:26:21 +01:00
|
|
|
$template->param(tagname => tagname($tag));
|
2010-04-22 02:08:23 +02:00
|
|
|
$template->param(tag => $tag);
|
2010-12-26 00:01:08 +01:00
|
|
|
|
|
|
|
my $dir = $config{srcdir};
|
|
|
|
if (! $config{tag_autocreate_commit}) {
|
|
|
|
$dir = $IkiWiki::Plugin::transient::transientdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
writefile($tagfile, $dir, $template->output);
|
|
|
|
if ($config{rcs} && $config{tag_autocreate_commit}) {
|
2010-04-21 22:13:37 +02:00
|
|
|
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 {
|
2011-01-04 19:26:21 +01:00
|
|
|
link => htmllink_tag($page, $destpage, $_,
|
|
|
|
rel => "tag", linktext => tagname($_))
|
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) {
|
2011-07-29 12:54:30 +02:00
|
|
|
eval q{use HTML::Entities};
|
|
|
|
$template->param(categories =>
|
2011-12-24 00:54:59 +01:00
|
|
|
[map { category => HTML::Entities::encode_entities_numeric(tagname($_)) },
|
2011-07-29 12:54:30 +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
|