]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/comments.pm
jump to comments anchor after post
[git.ikiwiki.info.git] / IkiWiki / Plugin / comments.pm
index 6e257d1d9b2d21d3da582639c07f5845d09f7fea..2ca2d0a1d02d2e9332571ea1f3fe2ef5876afdb0 100644 (file)
@@ -36,7 +36,7 @@ sub getsetup () {
                },
                comments_pagespec => {
                        type => 'pagespec',
-                       example => 'blog/* and *!/Discussion',
+                       example => 'blog/* and !*/Discussion',
                        description => 'PageSpec of pages where comments are allowed',
                        link => 'ikiwiki/PageSpec',
                        safe => 1,
@@ -500,7 +500,8 @@ sub sessioncgi ($$) {
                error($conflict) if defined $conflict;
 
                # Bounce back to where we were, but defeat broken caches
-               my $anticache = "?updated=$page/$config{comments_pagename}$i";
+               # and jump to the comments anchor.
+               my $anticache = "?updated=$page/$config{comments_pagename}$i#comments";
                IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
        }
        else {
@@ -511,6 +512,24 @@ sub sessioncgi ($$) {
        exit;
 }
 
+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 = @_;
 
@@ -518,24 +537,9 @@ sub pagetemplate (@) {
        my $template = $params{template};
 
        if ($template->query(name => 'comments')) {
-               my $comments = undef;
-
-               my $open = 0;
-               my $shown = 0;
-               if (pagespec_match($page,
-                               $config{comments_pagespec},
-                               location => $page)) {
-                       $shown = 1;
-                       $open = length $config{cgiurl} > 0;
-               }
-
-               if (pagespec_match($page,
-                               "$config{comments_closed_pagespec} or */$config{comments_pagename}*",
-                               location => $page)) {
-                       $shown = 0;
-                       $open = 0;
-               }
+               my $shown = commentsshown($page);
 
+               my $comments = undef;
                if ($shown) {
                        $comments = IkiWiki::preprocess_inline(
                                pages => "internal($page/$config{comments_pagename}*)",
@@ -553,13 +557,27 @@ sub pagetemplate (@) {
                        $template->param(comments => $comments);
                }
 
-               if ($open) {
+               if ($shown && commentsopen($page)) {
                        my $commenturl = IkiWiki::cgiurl(do => 'comment',
                                page => $page);
                        $template->param(commenturl => $commenturl);
                }
        }
 
+       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 (commentsshown($page)) {
+                       $template->param(commentslink =>
+                               htmllink($page, $params{destpage}, $page,
+                                       linktext => gettext("Comments"),
+                                       anchor => "comments",
+                                       noimageinline => 1));
+               }
+       }
+
        if ($template->query(name => 'commentuser')) {
                $template->param(commentuser =>
                        $pagestate{$page}{comments}{commentuser});