+}
+
+sub commentsshown ($) {
+ my $page=shift;
+
+ return ! pagespec_match($page, "*/$config{comments_pagename}*",
+ location => $page) &&
+ pagespec_match($page, $config{comments_pagespec},
+ location => $page);
+}
+
+sub commentsopen ($) {
+ my $page = shift;
+
+ return length $config{cgiurl} > 0 &&
+ (! length $config{comments_closed_pagespec} ||
+ ! pagespec_match($page, $config{comments_closed_pagespec},
+ location => $page));
+}
+
+sub pagetemplate (@) {
+ my %params = @_;
+
+ my $page = $params{page};
+ my $template = $params{template};
+ my $shown = ($template->query(name => 'commentslink') ||
+ $template->query(name => 'commentsurl') ||
+ $template->query(name => 'atomcommentsurl') ||
+ $template->query(name => 'comments')) &&
+ commentsshown($page);
+
+ if ($template->query(name => 'comments')) {
+ my $comments = undef;
+ if ($shown) {
+ $comments = IkiWiki::preprocess_inline(
+ pages => "internal($page/$config{comments_pagename}*)",
+ template => 'comment',
+ show => 0,
+ reverse => 'yes',
+ page => $page,
+ destpage => $params{destpage},
+ feedfile => 'comments',
+ emptyfeeds => 'no',
+ );
+ }
+
+ if (defined $comments && length $comments) {
+ $template->param(comments => $comments);
+ }
+
+ if ($shown && commentsopen($page)) {
+ my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
+ page => $page);
+ $template->param(addcommenturl => $addcommenturl);
+ }
+ }
+
+ if ($template->query(name => 'commentsurl')) {
+ if ($shown) {
+ $template->param(commentsurl =>
+ urlto($page, undef, 1).'#comments');
+ }
+ }
+
+ if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
+ if ($shown) {
+ # This will 404 until there are some comments, but I
+ # think that's probably OK...
+ $template->param(atomcommentsurl =>
+ urlto($page, undef, 1).'comments.atom');
+ }
+ }
+
+ if ($template->query(name => 'commentslink')) {
+ # XXX Would be nice to say how many comments there are in
+ # the link. But, to update the number, blog pages
+ # would have to update whenever comments of any inlines
+ # page are added, which is not currently done.
+ if ($shown) {
+ $template->param(commentslink =>
+ htmllink($page, $params{destpage}, $page,
+ linktext => gettext("Comments"),
+ anchor => "comments",
+ noimageinline => 1));
+ }
+ }
+
+ if ($template->query(name => 'commentuser')) {
+ $template->param(commentuser =>
+ $commentstate{$page}{commentuser});
+ }
+
+ if ($template->query(name => 'commentopenid')) {
+ $template->param(commentopenid =>
+ $commentstate{$page}{commentopenid});
+ }
+
+ if ($template->query(name => 'commentip')) {
+ $template->param(commentip =>
+ $commentstate{$page}{commentip});
+ }
+
+ if ($template->query(name => 'commentauthor')) {
+ $template->param(commentauthor =>
+ $commentstate{$page}{commentauthor});
+ }
+
+ if ($template->query(name => 'commentauthorurl')) {
+ $template->param(commentauthorurl =>
+ $commentstate{$page}{commentauthorurl});
+ }
+}