aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: cfa4b84b1d9d92382a28e60ae54501683c5dff31 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/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;
my %license;
my %copyright;

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};
	my $destpage=$params{destpage};
	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}=HTML::Entities::encode_numeric($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="'.urlto($stylesheet, $page).
			'" rel="'.encode_entities($rel).
			'" title="'.encode_entities($title).
			"\" type=\"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";
	}
	elsif ($key eq 'license') {
		$meta{$page}.="<link rel=\"license\" href=\"#page_license\" />\n";
		$license{$page}=$value;
	}
	elsif ($key eq 'copyright') {
		$meta{$page}.="<link rel=\"copyright\" href=\"#page_copyright\" />\n";
		$copyright{$page}=$value;
	}
	elsif ($key eq 'forward') {
		my $delay=0;
		my $dest_url;
		my $text;
		if (exists $params{delay}) {
			$delay=$params{delay};
		}
		# Is this a wikilink?
		if ($value =~ /^\[\[(.*)\]\]$/) {
			$text=htmllink($page, $destpage, $1);
			$dest_url=urlto(bestlink($page, $1), $destpage);
		} else {
			$text="<a href=\"$dest_url\">$dest_url</a>";
			$dest_url=$value;
		}
# TODO.		$meta{$page}.=scrub("<meta http-equiv=\"refresh\" content=\"$delay; URL=$dest_url\">");
		$meta{$page}.="<meta http-equiv=\"refresh\" content=\"$delay; URL=$dest_url\">";
		return "You are being forwarded to $text.";
	}
	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 $destpage=$params{destpage};
        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");
		
	if ($page ne $destpage &&
	    ((exists $license{$page}   && ! exists $license{$destpage}) ||
	     (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
		# Force a scan of the destpage to get its copyright/license
		# info. If the info is declared after an inline, it will
		# otherwise not be available at this point.
		IkiWiki::scan($pagesources{$destpage});
	}

	if (exists $license{$page} && $template->query(name => "license") &&
	    ($page eq $destpage || ! exists $license{$destpage} ||
	     $license{$page} ne $license{$destpage})) {
		$template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
	}
	if (exists $copyright{$page} && $template->query(name => "copyright") &&
	    ($page eq $destpage || ! exists $copyright{$destpage} ||
	     $copyright{$page} ne $copyright{$destpage})) {
		$template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));
	}
} # }}}

1