2006-08-23 07:41:07 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Structured template plugin.
|
|
|
|
package IkiWiki::Plugin::template;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-08-23 07:41:07 +02:00
|
|
|
use HTML::Template;
|
|
|
|
use Encode;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-04 01:35:35 +02:00
|
|
|
hook(type => "getsetup", id => "template", call => \&getsetup);
|
2009-02-17 19:29:08 +01:00
|
|
|
hook(type => "preprocess", id => "template", call => \&preprocess);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-08-23 07:41:07 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-04 01:35:35 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-04 01:35:35 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess (@) {
|
2006-08-23 07:41:07 +02:00
|
|
|
my %params=@_;
|
|
|
|
|
|
|
|
if (! exists $params{id}) {
|
2008-07-13 21:05:34 +02:00
|
|
|
error gettext("missing id parameter")
|
2006-08-23 07:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $template_page="templates/$params{id}";
|
2006-09-10 00:50:27 +02:00
|
|
|
add_depends($params{page}, $template_page);
|
2006-08-23 07:41:07 +02:00
|
|
|
|
2006-09-10 00:50:27 +02:00
|
|
|
my $template_file=$pagesources{$template_page};
|
2006-12-29 05:38:40 +01:00
|
|
|
return sprintf(gettext("template %s not found"),
|
2008-09-27 21:18:36 +02:00
|
|
|
htmllink($params{page}, $params{destpage}, "/".$template_page))
|
2006-12-29 05:38:40 +01:00
|
|
|
unless defined $template_file;
|
2006-08-23 07:41:07 +02:00
|
|
|
|
2006-11-06 00:44:20 +01:00
|
|
|
my $template;
|
|
|
|
eval {
|
|
|
|
$template=HTML::Template->new(
|
|
|
|
filter => sub {
|
|
|
|
my $text_ref = shift;
|
|
|
|
$$text_ref=&Encode::decode_utf8($$text_ref);
|
|
|
|
chomp $$text_ref;
|
|
|
|
},
|
|
|
|
filename => srcfile($template_file),
|
|
|
|
die_on_bad_params => 0,
|
|
|
|
no_includes => 1,
|
|
|
|
blind_cache => 1,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
if ($@) {
|
2008-07-13 21:05:34 +02:00
|
|
|
error gettext("failed to process:")." $@"
|
2006-11-06 00:44:20 +01:00
|
|
|
}
|
2006-08-23 07:41:07 +02:00
|
|
|
|
2008-07-10 21:25:05 +02:00
|
|
|
$params{basename}=IkiWiki::basename($params{page});
|
|
|
|
|
2006-08-23 07:41:07 +02:00
|
|
|
foreach my $param (keys %params) {
|
2008-01-09 20:17:25 +01:00
|
|
|
if ($template->query(name => $param)) {
|
|
|
|
$template->param($param =>
|
2008-06-04 07:24:23 +02:00
|
|
|
IkiWiki::htmlize($params{page}, $params{destpage},
|
2008-01-09 20:17:25 +01:00
|
|
|
pagetype($pagesources{$params{page}}),
|
|
|
|
$params{$param}));
|
|
|
|
}
|
|
|
|
if ($template->query(name => "raw_$param")) {
|
|
|
|
$template->param("raw_$param" => $params{$param});
|
|
|
|
}
|
2006-08-23 07:41:07 +02:00
|
|
|
}
|
|
|
|
|
2008-01-09 20:17:25 +01:00
|
|
|
return IkiWiki::preprocess($params{page}, $params{destpage},
|
2007-05-17 21:55:11 +02:00
|
|
|
IkiWiki::filter($params{page}, $params{destpage},
|
2008-01-09 20:17:25 +01:00
|
|
|
$template->output));
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-08-23 07:41:07 +02:00
|
|
|
|
|
|
|
1
|