2006-08-23 07:41:07 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Structured template plugin.
|
|
|
|
package IkiWiki::Plugin::template;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use IkiWiki;
|
|
|
|
use HTML::Template;
|
|
|
|
use Encode;
|
|
|
|
|
|
|
|
sub import { #{{{
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "preprocess", id => "template", call => \&preprocess);
|
2006-08-23 07:41:07 +02:00
|
|
|
} # }}}
|
|
|
|
|
|
|
|
sub preprocess (@) { #{{{
|
|
|
|
my %params=@_;
|
|
|
|
|
|
|
|
if (! exists $params{id}) {
|
2006-12-29 05:38:40 +01:00
|
|
|
return "[[".gettext("template 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"),
|
|
|
|
htmllink($params{page}, $params{destpage}, $template_page))
|
|
|
|
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 ($@) {
|
2006-12-29 05:38:40 +01:00
|
|
|
return "[[".gettext("template failed to process:")." $@]]";
|
2006-11-06 00:44:20 +01:00
|
|
|
}
|
2006-08-23 07:41:07 +02:00
|
|
|
|
|
|
|
foreach my $param (keys %params) {
|
|
|
|
$template->param($param => $params{$param});
|
|
|
|
}
|
|
|
|
|
|
|
|
return IkiWiki::preprocess($params{page}, $params{destpage},
|
2007-02-14 01:11:19 +01:00
|
|
|
IkiWiki::filter($params{page},
|
|
|
|
$template->output));
|
2006-08-23 07:41:07 +02:00
|
|
|
} # }}}
|
|
|
|
|
|
|
|
1
|