2 # Define self-documenting templates as wiki pages without HTML::Template
3 # markup leaking into IkiWiki's output.
4 # Copyright © 2013-2014 Simon McVittie. GPL-2+, see debian/copyright
5 package IkiWiki::Plugin::templatebody;
13 hook(type => "getsetup", id => "templatebody", call => \&getsetup);
14 hook(type => "preprocess", id => "templatebody", call => \&preprocess,
16 hook(type => "readtemplate", id => "templatebody",
17 call => \&readtemplate);
29 # This doesn't persist between runs: we're going to read and scan the
30 # template file regardless, so there's no point in saving it to the index.
32 # ("templates/note" => "<div class=\"notebox\">\n<TMPL_VAR text>\n</div>")
38 # [[!templatebody "<div>hello</div>"]] results in
39 # preprocess("<div>hello</div>" => undef, page => ...)
42 error(gettext("first parameter must be the content"));
45 $templates{$params{page}} = $content;
52 my $tpage = $params{page};
53 my $content = $params{content};
54 my $filename = $params{filename};
56 # pass-through if it's a .tmpl attachment or otherwise unsuitable
57 return $content unless defined $tpage;
58 return $content if $tpage =~ /\.tmpl$/;
59 my $src = $pagesources{$tpage};
60 return $content unless defined $src;
61 return $content unless defined pagetype($src);
63 # We might be using the template for [[!template]], which has to run
64 # during the scan stage so that templates can include scannable
65 # directives which are expanded in the resulting page. Calls to
66 # IkiWiki::scan are in arbitrary order, so the template might
67 # not have been scanned yet. Make sure.
68 require IkiWiki::Render;
71 # Having scanned it, we know whether it had a [[!templatebody]].
72 if (exists $templates{$tpage}) {
73 return $templates{$tpage};
76 # If not, return the whole thing. (Eventually, after implementing
77 # a transition, this can become an error.)