aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 15a8bad845f08e389bbc656ff518947ff175c6b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl
# Ikiwiki metadata plugin.
package IkiWiki::Plugin::meta;

use warnings;
use strict;
use IkiWiki;

my %meta;
my %title;

sub import { #{{{
	IkiWiki::hook(type => "preprocess", id => "meta", 
		call => \&preprocess);
	IkiWiki::hook(type => "pagetemplate", id => "meta", 
		call => \&pagetemplate);
} # }}}

sub preprocess (@) { #{{{
	if (! @_) {
		return "";
	}
	my %params=@_;
	my $key=shift;
	my $value=$params{$key};
	delete $params{$key};
	my $page=$params{page};
	delete $params{page};
	delete $params{destpage};

	eval q{use CGI 'escapeHTML'};

	if ($key eq 'link') {
		if (%params) {
			$meta{$page}='' unless exists $meta{$page};
			$meta{$page}.="<link href=\"".escapeHTML($value)."\" ".
				join(" ", map { escapeHTML("$_=\"$params{$_}\"") } keys %params).
				" />\n";
		}
		else {
			# hidden WikiLink
			push @{$IkiWiki::links{$page}}, $value;
		}
	}
	elsif ($key eq 'title') {
		$title{$page}=escapeHTML($value);
	}
	else {
		$meta{$page}='' unless exists $meta{$page};
		$meta{$page}.="<meta name=\"".escapeHTML($key)."\" content=\"".escapeHTML($value)."\" />\n";
	}

	return "";
} # }}}

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

	$template->param(meta => $meta{$page})
		if exists $meta{$page} && $template->query(name => "meta");
	$template->param(title => $title{$page})
		if exists $title{$page} && $template->query(name => "title");
} # }}}

1