]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/comments.pm
whitespace
[ikiwiki.git] / IkiWiki / Plugin / comments.pm
index 4ed696026c009f80db4857ee9b7829fa02afda21..6c8952d4553deb99d1435beddd5de21aae76771c 100644 (file)
@@ -15,6 +15,8 @@ use constant PREVIEW => "Preview";
 use constant POST_COMMENT => "Post comment";
 use constant CANCEL => "Cancel";
 
 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);
 sub import { #{{{
        hook(type => "checkconfig", id => 'comments',  call => \&checkconfig);
        hook(type => "getsetup", id => 'comments',  call => \&getsetup);
@@ -26,6 +28,68 @@ sub import { #{{{
        IkiWiki::loadplugin("inline");
 } # }}}
 
        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};
 sub htmlize { # {{{
        my %params = @_;
        return $params{content};
@@ -48,12 +112,12 @@ sub preprocess { # {{{
        my $page = $params{page};
 
        my $format = $params{format};
        my $page = $params{page};
 
        my $format = $params{format};
-       if (defined $format && !exists $IkiWiki::hooks{htmlize}{$format}) {
+       if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
                error(sprintf(gettext("unsupported page format %s"), $format));
        }
 
        my $content = $params{content};
                error(sprintf(gettext("unsupported page format %s"), $format));
        }
 
        my $content = $params{content};
-       if (!defined $content) {
+       if (! defined $content) {
                error(gettext("comment must have content"));
        }
        $content =~ s/\\"/"/g;
                error(gettext("comment must have content"));
        }
        $content =~ s/\\"/"/g;
@@ -101,10 +165,10 @@ sub preprocess { # {{{
        $pagestate{$page}{comments}{commentip} = $commentip;
        $pagestate{$page}{comments}{commentauthor} = $commentauthor;
        $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
        $pagestate{$page}{comments}{commentip} = $commentip;
        $pagestate{$page}{comments}{commentauthor} = $commentauthor;
        $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
-       if (!defined $pagestate{$page}{meta}{author}) {
+       if (! defined $pagestate{$page}{meta}{author}) {
                $pagestate{$page}{meta}{author} = $commentauthor;
        }
                $pagestate{$page}{meta}{author} = $commentauthor;
        }
-       if (!defined $pagestate{$page}{meta}{authorurl}) {
+       if (! defined $pagestate{$page}{meta}{authorurl}) {
                $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
        }
 
                $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
        }
 
@@ -145,68 +209,6 @@ sub preprocess { # {{{
        return $content;
 } # }}}
 
        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_'
 sub checkconfig () { #{{{
        $config{comments_commit} = 1 unless defined $config{comments_commit};
        $config{comments_pagename} = 'comment_'
@@ -219,7 +221,7 @@ sub linkcgi ($) { #{{{
        if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
 
                my $page=decode_utf8($cgi->param("page"));
        if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
 
                my $page=decode_utf8($cgi->param("page"));
-               if (!defined $page) {
+               if (! defined $page) {
                        error("missing page parameter");
                }
 
                        error("missing page parameter");
                }
 
@@ -308,7 +310,7 @@ sub sessioncgi ($$) { #{{{
        }
        my @page_types;
        if (exists $IkiWiki::hooks{htmlize}) {
        }
        my @page_types;
        if (exists $IkiWiki::hooks{htmlize}) {
-               @page_types = grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}};
+               @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
        }
 
        my $allow_author = $config{comments_allowauthor};
        }
 
        my $allow_author = $config{comments_allowauthor};
@@ -324,7 +326,7 @@ sub sessioncgi ($$) { #{{{
 
        $form->tmpl_param(username => $session->param('name'));
 
 
        $form->tmpl_param(username => $session->param('name'));
 
-       if ($allow_author and !defined $session->param('name')) {
+       if ($allow_author and ! defined $session->param('name')) {
                $form->tmpl_param(allowauthor => 1);
                $form->field(name => 'author', type => 'text', size => '40');
                $form->field(name => 'url', type => 'text', size => '40');
                $form->tmpl_param(allowauthor => 1);
                $form->field(name => 'author', type => 'text', size => '40');
                $form->field(name => 'url', type => 'text', size => '40');
@@ -341,7 +343,7 @@ sub sessioncgi ($$) { #{{{
        # it to file_pruned anyway
        my $page = $form->field('page');
        $page = IkiWiki::possibly_foolish_untaint($page);
        # it to file_pruned anyway
        my $page = $form->field('page');
        $page = IkiWiki::possibly_foolish_untaint($page);
-       if (!defined $page || !length $page ||
+       if (! defined $page || ! length $page ||
                IkiWiki::file_pruned($page, $config{srcdir})) {
                error(gettext("bad page name"));
        }
                IkiWiki::file_pruned($page, $config{srcdir})) {
                error(gettext("bad page name"));
        }
@@ -383,7 +385,11 @@ sub sessioncgi ($$) { #{{{
                        $page));
        }
 
                        $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;
 
        my $editcontent = $form->field('editcontent') || '';
        $editcontent =~ s/\r\n/\n/g;
@@ -549,10 +555,7 @@ sub pagetemplate (@) { #{{{
                }
 
                if ($shown) {
                }
 
                if ($shown) {
-                       eval q{use IkiWiki::Plugin::inline};
-                       error($@) if $@;
-
-                       my @args = (
+                       $comments = IkiWiki::preprocess_inline(
                                pages => "internal($page/${comments_pagename}*)",
                                template => 'comments_display',
                                show => 0,
                                pages => "internal($page/${comments_pagename}*)",
                                template => 'comments_display',
                                show => 0,
@@ -562,7 +565,6 @@ sub pagetemplate (@) { #{{{
                                feedfile => 'comments',
                                emptyfeeds => 'no',
                        );
                                feedfile => 'comments',
                                emptyfeeds => 'no',
                        );
-                       $comments = IkiWiki::preprocess_inline(@args);
                }
 
                if (defined $comments && length $comments) {
                }
 
                if (defined $comments && length $comments) {
@@ -603,7 +605,7 @@ sub match_postcomment ($$;@) {
        my $page = shift;
        my $glob = shift;
 
        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);
                return IkiWiki::FailReason->new("not posting a comment");
        }
        return match_glob($page, $glob);