aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: 18e28a196a47805f6d95550894dc62e917d60593 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/perl
# Ikiwiki metadata plugin.
package IkiWiki::Plugin::meta;

use warnings;
use strict;
use IkiWiki 2.00;

my %meta;
my %title;
my %permalink;
my %author;
my %authorurl;

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

sub filter (@) { #{{{
	my %params=@_;
	
	$meta{$params{page}}='';

	return $params{content};
} # }}}

sub scrub ($) { #{{{
	if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
		return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
	}
	else {
		return shift;
	}
} #}}}

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};
	delete $params{preview};

	eval q{use HTML::Entities};
	# Always dencode, even if encoding later, since it might not be
	# fully encoded.
	$value=decode_entities($value);

	if ($key eq 'link') {
		if (%params) {
			$meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
				join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
				" />\n");
		}
		else {
			# hidden WikiLink
			push @{$links{$page}}, $value;
		}
	}
	elsif ($key eq 'title') {
		$title{$page}=encode_entities($value);
	}
	elsif ($key eq 'permalink') {
		$permalink{$page}=$value;
		$meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
	}
	elsif ($key eq 'date') {
		eval q{use Date::Parse};
		if (! $@) {
			my $time = str2time($value);
			$IkiWiki::pagectime{$page}=$time if defined $time;
		}
	}
	elsif ($key eq 'stylesheet') {
		my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
		my $title=exists $params{title} ? $params{title} : $value;
		# adding .css to the value prevents using any old web
		# editable page as a stylesheet
		my $stylesheet=bestlink($page, $value.".css");
		if (! length $stylesheet) {
			return "[[meta ".gettext("stylesheet not found")."]]";
		}
		$meta{$page}.='<link href="'.$stylesheet.
			'" rel="'.encode_entities($rel).
			'" title="'.encode_entities($title).
			"\" style=\"text/css\" />\n";
	}
	elsif ($key eq 'openid') {
		if (exists $params{server}) {
			$meta{$page}.='<link href="'.encode_entities($params{server}).
				"\" rel=\"openid.server\" />\n";
		}
		$meta{$page}.='<link href="'.encode_entities($value).
			"\" rel=\"openid.delegate\" />\n";
	}
	else {
		$meta{$page}.=scrub("<meta name=\"".encode_entities($key).
			"\" content=\"".encode_entities($value)."\" />\n");
		if ($key eq 'author') {
			$author{$page}=$value;
		}
		elsif ($key eq 'authorurl') {
			$authorurl{$page}=$value;
		}
	}

	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");
	if (exists $title{$page} && $template->query(name => "title")) {
		$template->param(title => $title{$page});
		$template->param(title_overridden => 1);
	}
	$template->param(permalink => $permalink{$page})
		if exists $permalink{$page} && $template->query(name => "permalink");
	$template->param(author => $author{$page})
		if exists $author{$page} && $template->query(name => "author");
	$template->param(authorurl => $authorurl{$page})
		if exists $authorurl{$page} && $template->query(name => "authorurl");
	
} # }}}

1