]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/templatebody.pm
(no commit message)
[ikiwiki.git] / IkiWiki / Plugin / templatebody.pm
1 #!/usr/bin/perl
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;
6
7 use warnings;
8 use strict;
9 use IkiWiki 3.00;
10 use Encode;
11
12 sub import {
13         hook(type => "getsetup", id => "templatebody", call => \&getsetup);
14         hook(type => "preprocess", id => "templatebody", call => \&preprocess,
15                 scan => 1);
16         hook(type => "readtemplate", id => "templatebody",
17                 call => \&readtemplate);
18 }
19
20 sub getsetup () {
21         return
22                 plugin => {
23                         safe => 1,
24                         rebuild => undef,
25                         section => "core",
26                 },
27 }
28
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.
31 # Example contents:
32 # ("templates/note" => "<div class=\"notebox\">\n<TMPL_VAR text>\n</div>")
33 my %templates;
34
35 sub preprocess (@) {
36         my %params=@_;
37
38         # [[!templatebody "<div>hello</div>"]] results in
39         # preprocess("<div>hello</div>" => undef, page => ...)
40         my $content = $_[0];
41         if (length $_[1]) {
42                 error(gettext("first parameter must be the content"));
43         }
44
45         $templates{$params{page}} = $content;
46
47         return "";
48 }
49
50 sub readtemplate {
51         my %params = @_;
52         my $tpage = $params{page};
53         my $content = $params{content};
54         my $filename = $params{filename};
55
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);
62
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;
69         IkiWiki::scan($src);
70
71         # Having scanned it, we know whether it had a [[!templatebody]].
72         if (exists $templates{$tpage}) {
73                 return $templates{$tpage};
74         }
75
76         # If not, return the whole thing. (Eventually, after implementing
77         # a transition, this can become an error.)
78         return $content;
79 }
80
81 1