tag name sanitation

The use of typed links for tags and some of the consequent changes
introduced some unwanted functionality variations in the tag system. Two
problems in particular could be observed, when compared to the use of
tags in older versions of IkiWiki:

* tags in feeds (both rss and atom) would use the file path as their
  name (e.g. you would have <category term="tags/sometag" /> in an atom
  item for a page tagged sometag with a tagbase of tags), whereas they
  appeared pure before
* tags containing a slash character would appear without the slash
  character but be used with the slash character in other circumstances
  (effect visible by tagging a page with a name such as "with/slash")

Both of these issues are fixed by introducing a tagname() function that
takes a tag link and effectively reverses (as well as possible) the
effects of taglink().

A possible alternative route would have been the reintroduction of the
global %tags hash, but the new approach as the (arguable) benefit of
introducing a small layer of sanitation for tag names.
master
Giuseppe Bilotta 2011-01-04 19:26:21 +01:00 committed by Joey Hess
parent 866486b7a3
commit a17469e388
1 changed files with 15 additions and 3 deletions

View File

@ -55,6 +55,17 @@ sub taglink ($) {
return $tag; return $tag;
} }
# Returns a tag name from a tag link
sub tagname ($) {
my $tag=shift;
if (defined $config{tagbase}) {
$tag =~ s!^/$config{tagbase}/!!;
} else {
$tag =~ s!^\.?/?!!;
}
return $tag;
}
sub htmllink_tag ($$$;@) { sub htmllink_tag ($$$;@) {
my $page=shift; my $page=shift;
my $destpage=shift; my $destpage=shift;
@ -84,7 +95,7 @@ sub gentag ($) {
debug($message); debug($message);
my $template=template("autotag.tmpl"); my $template=template("autotag.tmpl");
$template->param(tagname => IkiWiki::basename($tag)); $template->param(tagname => tagname($tag));
$template->param(tag => $tag); $template->param(tag => $tag);
writefile($tagfile, $config{srcdir}, $template->output); writefile($tagfile, $config{srcdir}, $template->output);
if ($config{rcs}) { if ($config{rcs}) {
@ -154,14 +165,15 @@ sub pagetemplate (@) {
$template->param(tags => [ $template->param(tags => [
map { map {
link => htmllink_tag($page, $destpage, $_, rel => "tag") link => htmllink_tag($page, $destpage, $_,
rel => "tag", linktext => tagname($_))
}, sort keys %$tags }, sort keys %$tags
]) if defined $tags && %$tags && $template->query(name => "tags"); ]) if defined $tags && %$tags && $template->query(name => "tags");
if ($template->query(name => "categories")) { if ($template->query(name => "categories")) {
# It's an rss/atom template. Add any categories. # It's an rss/atom template. Add any categories.
if (defined $tags && %$tags) { if (defined $tags && %$tags) {
$template->param(categories => [map { category => $_ }, $template->param(categories => [map { category => tagname($_) },
sort keys %$tags]); sort keys %$tags]);
} }
} }