]> sipb.mit.edu Git - ikiwiki.git/commitdiff
look for templates in srcdir and underlays, first
authorJoey Hess <joey@kitenet.net>
Thu, 22 Apr 2010 19:34:32 +0000 (15:34 -0400)
committerJoey Hess <joey@kitenet.net>
Thu, 22 Apr 2010 19:55:58 +0000 (15:55 -0400)
This entailed changing template_params; it no longer takes the template
filename as its first parameter.

Add template_depends to api and replace calls to template() with
template_depends() in appropriate places, where a dependency should be
added on the template.

Other plugins don't use template(), so will need further work.

Also, includes are disabled for security. Enabling includes only when using
templates from the templatedir would be nice, but would add a lot of
complexity to the implementation.

IkiWiki.pm
IkiWiki/Plugin/comments.pm
IkiWiki/Plugin/editpage.pm
IkiWiki/Plugin/google.pm
IkiWiki/Plugin/inline.pm
IkiWiki/Plugin/search.pm
IkiWiki/Render.pm
doc/plugins/write.mdwn

index c2c2337b4a80fddc778feee9cf0cd990f5477210..1327e4db50495f0b2b02ba0ab08eb1b55445bec6 100644 (file)
@@ -18,8 +18,8 @@ use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
        %autofiles};
 
 use Exporter q{import};
        %autofiles};
 
 use Exporter q{import};
-our @EXPORT = qw(hook debug error template htmlpage deptype
-       add_depends pagespec_match pagespec_match_list bestlink
+our @EXPORT = qw(hook debug error htmlpage template template_depends
+       deptype add_depends pagespec_match pagespec_match_list bestlink
        htmllink readfile writefile pagetype srcfile pagename
        displaytime will_render gettext ngettext urlto targetpage
        add_underlay pagetitle titlepage linkpage newpagefile
        htmllink readfile writefile pagetype srcfile pagename
        displaytime will_render gettext ngettext urlto targetpage
        add_underlay pagetitle titlepage linkpage newpagefile
@@ -1652,47 +1652,58 @@ sub saveindex () {
 }
 
 sub template_file ($) {
 }
 
 sub template_file ($) {
-       my $template=shift;
+       my $name=shift;
 
 
+       my $template=srcfile("templates/$name", 1);
+       return $template if defined $template;
+       
        foreach my $dir ($config{templatedir},
                         "$installdir/share/ikiwiki/templates") {
        foreach my $dir ($config{templatedir},
                         "$installdir/share/ikiwiki/templates") {
-               return "$dir/$template" if -e "$dir/$template";
+               return "$dir/$name" if -e "$dir/$name";
        }
        return;
 }
 
 sub template_params (@) {
        }
        return;
 }
 
 sub template_params (@) {
-       my $filename=template_file(shift);
-
-       if (! defined $filename) {
-               return if wantarray;
-               return "";
-       }
+       filter => sub {
+               my $text_ref = shift;
+               ${$text_ref} = decode_utf8(${$text_ref});
+       },
+       loop_context_vars => 1,
+       die_on_bad_params => 0,
+       @_,
+       no_includes => 1,
+}
 
 
-       my @ret=(
-               filter => sub {
-                       my $text_ref = shift;
-                       ${$text_ref} = decode_utf8(${$text_ref});
-               },
-               filename => $filename,
-               loop_context_vars => 1,
-               die_on_bad_params => 0,
+sub template ($;@) {
+       require HTML::Template;
+       return HTML::Template->new(template_params(
+               filename => template_file(shift),
                @_
                @_
-       );
-       return wantarray ? @ret : {@ret};
+       ));
 }
 
 }
 
-sub template ($;@) {
+sub template_depends ($$;@) {
+       my $name=shift;
+       my $page=shift;
+
+       if (defined $page) {
+               add_depends($page, "templates/$name");
+       }
+       my $filename=template_file($name);
+
        require HTML::Template;
        require HTML::Template;
-       return HTML::Template->new(template_params(@_));
+       return HTML::Template->new(template_params(
+               filename => $filename,
+               @_
+       ));
 }
 
 sub misctemplate ($$;@) {
        my $title=shift;
        my $pagebody=shift;
        
 }
 
 sub misctemplate ($$;@) {
        my $title=shift;
        my $pagebody=shift;
        
-       my $template=template("misc.tmpl");
-       $template->param(
+       my $template=template("misc.tmpl",
                title => $title,
                indexlink => indexlink(),
                wikiname => $config{wikiname},
                title => $title,
                indexlink => indexlink(),
                wikiname => $config{wikiname},
index 58bd4b851d9434ea142e21e9d8262d9ceb72b444..ed75a6e466ac29f06708b5efbfda8aa04838d756 100644 (file)
@@ -274,7 +274,7 @@ sub editcomment ($$) {
                action => $config{cgiurl},
                header => 0,
                table => 0,
                action => $config{cgiurl},
                header => 0,
                table => 0,
-               template => scalar IkiWiki::template_params('editcomment.tmpl'),
+               template => template('editcomment.tmpl'),
        );
 
        IkiWiki::decode_form_utf8($form);
        );
 
        IkiWiki::decode_form_utf8($form);
index 26e38abc121ea06b53a250e5061a1227ac76099c..5c94ecbca1839c99dbac697b9c29d5922e7b843d 100644 (file)
@@ -78,7 +78,7 @@ sub cgi_editpage ($$) {
                action => $config{cgiurl},
                header => 0,
                table => 0,
                action => $config{cgiurl},
                header => 0,
                table => 0,
-               template => scalar template_params("editpage.tmpl"),
+               template => template("editpage.tmpl"),
        );
        
        decode_form_utf8($form);
        );
        
        decode_form_utf8($form);
index 48ad4c8cea058f9a024bfdf99c01f5fcca8f4ca8..68cb16513c69812c47443ad2377c091e4d17037e 100644 (file)
@@ -36,7 +36,7 @@ sub pagetemplate (@) {
        # Add search box to page header.
        if ($template->query(name => "searchform")) {
                if (! defined $form) {
        # Add search box to page header.
        if ($template->query(name => "searchform")) {
                if (! defined $form) {
-                       my $searchform = template("googleform.tmpl", blind_cache => 1);
+                       my $searchform = template_depends("googleform.tmpl", $page, blind_cache => 1);
                        $searchform->param(url => $config{url});
                        $form=$searchform->output;
                }
                        $searchform->param(url => $config{url});
                        $form=$searchform->output;
                }
index 3359af31410b3c7eb9a5b2c7655ce91c707acdba..0436497420489a93964cdfc47d31abc34a17b9e2 100644 (file)
@@ -299,7 +299,7 @@ sub preprocess_inline (@) {
            (exists $params{postform} && yesno($params{postform}))) &&
            IkiWiki->can("cgi_editpage")) {
                # Add a blog post form, with feed buttons.
            (exists $params{postform} && yesno($params{postform}))) &&
            IkiWiki->can("cgi_editpage")) {
                # Add a blog post form, with feed buttons.
-               my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
+               my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
                $formtemplate->param(cgiurl => $config{cgiurl});
                $formtemplate->param(rootpage => rootpage(%params));
                $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
                $formtemplate->param(cgiurl => $config{cgiurl});
                $formtemplate->param(rootpage => rootpage(%params));
                $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
@@ -320,19 +320,23 @@ sub preprocess_inline (@) {
        }
        elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
                # Add feed buttons.
        }
        elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
                # Add feed buttons.
-               my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
+               my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
                $linktemplate->param(rssurl => $rssurl) if $rss;
                $linktemplate->param(atomurl => $atomurl) if $atom;
                $ret.=$linktemplate->output;
        }
        
        if (! $feedonly) {
                $linktemplate->param(rssurl => $rssurl) if $rss;
                $linktemplate->param(atomurl => $atomurl) if $atom;
                $ret.=$linktemplate->output;
        }
        
        if (! $feedonly) {
-               require HTML::Template;
-               my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
-               if (! @params) {
-                       error sprintf(gettext("nonexistant template %s"), $params{template});
+               my $template;
+               if (! $raw) {
+                       eval {
+                               $template=template_depends($params{template}.".tmpl", $params{page},
+                                       blind_cache => 1);
+                       };
+                       if (! $@ || ! $template) {
+                               error sprintf(gettext("nonexistant template %s"), $params{template});
+                       }
                }
                }
-               my $template=HTML::Template->new(@params) unless $raw;
                my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
        
                foreach my $page (@list) {
                my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
        
                foreach my $page (@list) {
@@ -534,7 +538,7 @@ sub genfeed ($$$$$@) {
        
        my $url=URI->new(encode_utf8(urlto($page,"",1)));
        
        
        my $url=URI->new(encode_utf8(urlto($page,"",1)));
        
-       my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
+       my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
        my $content="";
        my $lasttime = 0;
        foreach my $p (@pages) {
        my $content="";
        my $lasttime = 0;
        foreach my $p (@pages) {
@@ -598,7 +602,7 @@ sub genfeed ($$$$$@) {
                $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
        }
 
                $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
        }
 
-       my $template=template($feedtype."page.tmpl", blind_cache => 1);
+       my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
        $template->param(
                title => $page ne "index" ? pagetitle($page) : $config{wikiname},
                wikiname => $config{wikiname},
        $template->param(
                title => $page ne "index" ? pagetitle($page) : $config{wikiname},
                wikiname => $config{wikiname},
index a1e7026caf0e11d783c65d90ea6e233123122bbb..55edf8752a8e04c4abbe9e320f36922ad13b0884 100644 (file)
@@ -52,7 +52,7 @@ sub pagetemplate (@) {
        # Add search box to page header.
        if ($template->query(name => "searchform")) {
                if (! defined $form) {
        # Add search box to page header.
        if ($template->query(name => "searchform")) {
                if (! defined $form) {
-                       my $searchform = template("searchform.tmpl", blind_cache => 1);
+                       my $searchform = template_depends("searchform.tmpl", $page, blind_cache => 1);
                        $searchform->param(searchaction => $config{cgiurl});
                        $form=$searchform->output;
                }
                        $searchform->param(searchaction => $config{cgiurl});
                        $form=$searchform->output;
                }
index 49d080c161a9c6cc93250b3c6a8dc204a5ee209a..7cf19645e17c07fa0b0833e161bb12a88ac3e4bc 100644 (file)
@@ -74,7 +74,9 @@ sub genpage ($$) {
                        $templatefile=$file;
                }
        });
                        $templatefile=$file;
                }
        });
-       my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
+       my $template=template_depends(
+               defined $templatefile ? $templatefile : 'page.tmpl', $page,
+               blind_cache => 1);
        my $actions=0;
 
        if (length $config{cgiurl}) {
        my $actions=0;
 
        if (length $config{cgiurl}) {
index 9e8c59f63938a69a795df63734ba2bfafc5f94a0..eaa0081319d223a0934b7b48d4edbaf27eac501e 100644 (file)
@@ -705,6 +705,14 @@ Creates and returns a [[!cpan HTML::Template]] object. The first parameter
 is the name of the file in the template directory. The optional remaining
 parameters are passed to `HTML::Template->new`.
 
 is the name of the file in the template directory. The optional remaining
 parameters are passed to `HTML::Template->new`.
 
+### `template_depends($$;@)`
+
+Use this instead of `template()` if the content of a template is being
+included into a page. This causes the page to depend on the template,
+so it will be updated if the template is modified.
+
+Like `template()`, except the second parameter is the page.
+
 ### `htmlpage($)`
 
 Passed a page name, returns the base name that will be used for a the html
 ### `htmlpage($)`
 
 Passed a page name, returns the base name that will be used for a the html