]> sipb.mit.edu Git - ikiwiki.git/blobdiff - doc/todo/supporting_comments_via_disussion_pages.mdwn
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / doc / todo / supporting_comments_via_disussion_pages.mdwn
index b4c8a2a4d41d516018f2742e16660fa6cf333203..50bc17ba3ff9e8077bfac56ebfc73ba3a1bef193 100644 (file)
@@ -43,7 +43,7 @@ Is this simple enough to be sensible?
 
 >> I certainly won't be fussy about how it gets implemented, I was just trying to think of the lightest weight most "wiki" solution.  :-) -- Adam.
 
->>> As a side note, the feature describe above (having a form not to add a page but to expand it in a formated way would be useful for other things when the content is short (timetracking, sub-todo list items, etc..) --[[hb]]
+>>> As a side note, the feature described above (having a form not to add a page but to expand it in a formated way) would be useful for other things when the content is short (timetracking, sub-todo list items, etc..) --[[hb]]
 
 I've been looking into this.  I'd like to implement a "blogcomments"
 plugin.  Looking at the code, I think the way to go is to have a
@@ -55,3 +55,108 @@ content to the old one.
 -- [[MarceloMagallon]]
 
 > Anything I can do to help? --[[Joey]]
+
+>> Figured it out.  Can you comment on the code below?  Thanks. -- [[MarceloMagallon]]
+
+So, I have some code, included below.  For some reason that I don't quite get it's not updating the wiki page after a submit.  Maybe it's something silly on my side...
+
+What I ended up doing is write something like this to the page:
+
+    [[blogcomment from="""Username""" timestamp="""12345""" subject="""Some text""" text="""the text of the comment"""]]
+
+Each comment is processed to something like this:
+
+    <div>
+        <dl>
+            <dt>From</dt><dd>Username</dd>
+            <dt>Date</dt><dd>Date (needs fixing)</dd>
+            <dt>Subject</dt><dd>Subject text</dd>
+        </dl>
+
+        <p>Text of the comment...</p>
+    </div>
+
+.  In this way the comments can be styled using CSS.
+
+-- [[MarceloMagallon]]
+
+# Code
+
+    #!/usr/bin/perl
+    package IkiWiki::Plugin::comments;
+
+    use warnings;
+    use strict;
+    use IkiWiki '1.02';
+
+    sub import { #{{{
+        hook(type => "formbuilder_setup", id => "comments",
+            call => \&formbuilder_setup);
+        hook(type => "preprocess", id => "blogcomment",
+            call => \&preprocess);  
+    } # }}}
+
+    sub formbuilder_setup (@) { #{{{
+        my %params=@_;
+        my $cgi = $params{cgi};
+        my $form = $params{form};   
+        my $session = $params{session};
+
+        my ($page)=$form->field('page');
+        $page=IkiWiki::titlepage(IkiWiki::possibly_foolish_untaint($page));
+
+        # XXX: This needs something to make it blog specific
+        unless ($page =~ m{/discussion$} &&
+                $cgi->param('do') eq 'edit' &&
+                ! exists $form->{title})
+        {
+            return;
+        }
+
+        if (! $form->submitted)
+        {
+            $form->template(IkiWiki::template_file("makeblogcomment.tmpl"));
+            $form->field(name => "blogcomment", type => "textarea", rows => 20,
+                cols => 80);
+            return;
+        }
+
+        my $content="";
+        if (exists $pagesources{$page}) {
+            $content=readfile(srcfile($pagesources{$page}));
+            $content.="\n\n";
+        }
+        my $name=defined $session->param('name') ?
+            $session->param('name') : gettext('Anonymous');
+        my $timestamp=time;
+        my $subject=defined $cgi->param('comments') ?
+            $cgi->param('comments') : '';
+        my $comment=$cgi->param('blogcomment');
+
+        $content.=qq{[[blogcomment from="""$name""" timestamp="""$timestamp""" subject="""$subject""" text="""$comment"""]]\n\n};
+        $content=~s/\n/\r\n/g;
+        $form->field(name => "editcontent", value => $content, force => 1);
+    } # }}}
+
+    sub preprocess (@) { #{{{
+        my %params=@_;
+
+        my ($text, $date, $from, $subject, $r);
+
+        $text=IkiWiki::preprocess($params{page}, $params{destpage},
+                IkiWiki::filter($params{page}, $params{text}));
+        $from=exists $params{from} ? $params{from} : gettext("Anonymous");
+        $date=localtime($params{timestamp}) if exists $params{timestamp};
+        $subject=$params{subject} if exists $params{subject};
+
+        $r = qq{<div class="blogcomment"><dl>\n};
+        $r .= '<dt>' . gettext("From") . "</dt><dd>$from</dd>\n" if defined $from;
+        $r .= '<dt>' . gettext("Date") . "</dt><dd>$date</dd>\n" if defined $date;
+        $r .= '<dt>' . gettext("Subject") . "</dt><dd>$subject</dd>\n"
+            if defined $subject;
+        $r .= "</dl>\n" . $text . "</div>\n";
+
+        return $r;
+    } # }}}
+    
+    1;
\ No newline at end of file