2006-08-23 07:41:07 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Structured template plugin.
|
|
|
|
package IkiWiki::Plugin::template;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2007-04-27 04:55:52 +02:00
|
|
|
use IkiWiki 2.00;
|
2006-08-23 07:41:07 +02:00
|
|
|
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}) {
|
2007-03-07 13:04:45 +01:00
|
|
|
return "[[template ".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"),
|
|
|
|
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 ($@) {
|
2007-03-07 13:04:45 +01:00
|
|
|
return "[[template ".gettext("failed to process:")." $@]]";
|
2006-11-06 00:44:20 +01:00
|
|
|
}
|
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 =>
|
|
|
|
IkiWiki::htmlize($params{page},
|
|
|
|
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));
|
2006-08-23 07:41:07 +02:00
|
|
|
} # }}}
|
|
|
|
|
|
|
|
1
|