]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/comments.pm
avoid unnecessary variable
[ikiwiki.git] / IkiWiki / Plugin / comments.pm
index 4fa82f4678da6fb1e211d2c0a9ff9374eaa73377..f612f89475dd6450c8128b3a974c6c60bddc1936 100644 (file)
@@ -9,15 +9,18 @@ use warnings;
 use strict;
 use IkiWiki 2.00;
 use Encode;
+use POSIX qw(strftime);
 
 use constant PREVIEW => "Preview";
 use constant POST_COMMENT => "Post comment";
 use constant CANCEL => "Cancel";
 
+my $postcomment;
+
 sub import { #{{{
        hook(type => "checkconfig", id => 'comments',  call => \&checkconfig);
        hook(type => "getsetup", id => 'comments',  call => \&getsetup);
-       hook(type => "preprocess", id => 'comment', call => \&preprocess);
+       hook(type => "preprocess", id => '_comment', call => \&preprocess);
        hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
        hook(type => "htmlize", id => "_comment", call => \&htmlize);
        hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
@@ -25,6 +28,68 @@ sub import { #{{{
        IkiWiki::loadplugin("inline");
 } # }}}
 
+sub getsetup () { #{{{
+       return
+               plugin => {
+                       safe => 1,
+                       rebuild => 1,
+               },
+               # Pages where comments are shown, but new comments are not
+               # allowed, will show "Comments are closed".
+               comments_shown_pagespec => {
+                       type => 'pagespec',
+                       example => 'blog/*',
+                       default => '',
+                       description => 'PageSpec for pages where comments will be shown inline',
+                       link => 'ikiwiki/PageSpec',
+                       safe => 1,
+                       rebuild => 1,
+               },
+               comments_open_pagespec => {
+                       type => 'pagespec',
+                       example => 'blog/* and created_after(close_old_comments)',
+                       default => '',
+                       description => 'PageSpec for pages where new comments can be posted',
+                       link => 'ikiwiki/PageSpec',
+                       safe => 1,
+                       rebuild => 1,
+               },
+               comments_pagename => {
+                       type => 'string',
+                       example => 'comment_',
+                       default => 'comment_',
+                       description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
+                       safe => 0, # manual page moving required
+                       rebuild => undef,
+               },
+               comments_allowdirectives => {
+                       type => 'boolean',
+                       default => 0,
+                       example => 0,
+                       description => 'Interpret directives in comments?',
+                       safe => 1,
+                       rebuild => 0,
+               },
+               comments_allowauthor => {
+                       type => 'boolean',
+                       default => 0,
+                       example => 0,
+                       description => 'Allow anonymous commenters to set an author name?',
+                       safe => 1,
+                       rebuild => 0,
+               },
+               comments_commit => {
+                       type => 'boolean',
+                       example => 1,
+                       default => 1,
+                       description => 'commit comments to the VCS',
+                       # old uncommitted comments are likely to cause
+                       # confusion if this is changed
+                       safe => 0,
+                       rebuild => 0,
+               },
+} #}}}
+
 sub htmlize { # {{{
        my %params = @_;
        return $params{content};
@@ -89,12 +154,10 @@ sub preprocess { # {{{
                ($commentauthorurl, $commentauthor) =
                        linkuser($params{username});
        }
-       elsif (defined $params{ip}) {
-               $commentip = $params{ip};
-               $commentauthor = sprintf(
-                       gettext("Anonymous (IP: %s)"), $params{ip});
-       }
        else {
+               if (defined $params{ip}) {
+                       $commentip = $params{ip};
+               }
                $commentauthor = gettext("Anonymous");
        }
 
@@ -146,68 +209,6 @@ sub preprocess { # {{{
        return $content;
 } # }}}
 
-sub getsetup () { #{{{
-       return
-               plugin => {
-                       safe => 1,
-                       rebuild => 1,
-               },
-               # Pages where comments are shown, but new comments are not
-               # allowed, will show "Comments are closed".
-               comments_shown_pagespec => {
-                       type => 'pagespec',
-                       example => 'blog/*',
-                       default => '',
-                       description => 'PageSpec for pages where comments will be shown inline',
-                       link => 'ikiwiki/PageSpec',
-                       safe => 1,
-                       rebuild => 1,
-               },
-               comments_open_pagespec => {
-                       type => 'pagespec',
-                       example => 'blog/* and created_after(close_old_comments)',
-                       default => '',
-                       description => 'PageSpec for pages where new comments can be posted',
-                       link => 'ikiwiki/PageSpec',
-                       safe => 1,
-                       rebuild => 1,
-               },
-               comments_pagename => {
-                       type => 'string',
-                       example => 'comment_',
-                       default => 'comment_',
-                       description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
-                       safe => 0, # manual page moving will required
-                       rebuild => undef,
-               },
-               comments_allowdirectives => {
-                       type => 'boolean',
-                       default => 0,
-                       example => 0,
-                       description => 'Interpret directives in comments?',
-                       safe => 1,
-                       rebuild => 0,
-               },
-               comments_allowauthor => {
-                       type => 'boolean',
-                       default => 0,
-                       example => 0,
-                       description => 'Allow anonymous commenters to set an author name?',
-                       safe => 1,
-                       rebuild => 0,
-               },
-               comments_commit => {
-                       type => 'boolean',
-                       example => 1,
-                       default => 1,
-                       description => 'commit comments to the VCS',
-                       # old uncommitted comments are likely to cause
-                       # confusion if this is changed
-                       safe => 0,
-                       rebuild => 0,
-               },
-} #}}}
-
 sub checkconfig () { #{{{
        $config{comments_commit} = 1 unless defined $config{comments_commit};
        $config{comments_pagename} = 'comment_'
@@ -384,7 +385,11 @@ sub sessioncgi ($$) { #{{{
                        $page));
        }
 
-       IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
+       # Set a flag to indicate that we're posting a comment,
+       # so that postcomment() can tell it should match.
+       $postcomment=1;
+       IkiWiki::check_canedit($page, $cgi, $session);
+       $postcomment=0;
 
        my $editcontent = $form->field('editcontent') || '';
        $editcontent =~ s/\r\n/\n/g;
@@ -405,7 +410,7 @@ sub sessioncgi ($$) { #{{{
        my $anchor = "${comments_pagename}${i}";
 
        $editcontent =~ s/"/\\"/g;
-       my $content = "[[!comment format=$type\n";
+       my $content = "[[!_comment format=$type\n";
 
        # FIXME: handling of double quotes probably wrong?
        if (defined $session->param('name')) {
@@ -439,7 +444,7 @@ sub sessioncgi ($$) { #{{{
                $content .= " subject=\"$subject\"\n";
        }
 
-       $content .= " date=\"" . IkiWiki::formattime(time, '%X %x') . "\"\n";
+       $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
 
        $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
 
@@ -553,15 +558,16 @@ sub pagetemplate (@) { #{{{
                        eval q{use IkiWiki::Plugin::inline};
                        error($@) if $@;
 
-                       my @args = (
+                       $comments = IkiWiki::preprocess_inline(
                                pages => "internal($page/${comments_pagename}*)",
                                template => 'comments_display',
                                show => 0,
                                reverse => 'yes',
                                page => $page,
                                destpage => $params{destpage},
+                               feedfile => 'comments',
+                               emptyfeeds => 'no',
                        );
-                       $comments = IkiWiki::preprocess_inline(@args);
                }
 
                if (defined $comments && length $comments) {
@@ -602,7 +608,7 @@ sub match_postcomment ($$;@) {
        my $page = shift;
        my $glob = shift;
 
-       unless ($page =~ s/\[postcomment\]$//) {
+       if (! $postcomment) {
                return IkiWiki::FailReason->new("not posting a comment");
        }
        return match_glob($page, $glob);