diff --git a/IkiWiki/Plugin/comments.pm b/IkiWiki/Plugin/comments.pm index 73f8d6140..13ced6ffd 100644 --- a/IkiWiki/Plugin/comments.pm +++ b/IkiWiki/Plugin/comments.pm @@ -8,6 +8,7 @@ package IkiWiki::Plugin::comments; use warnings; use strict; use IkiWiki 2.00; +use Encode; use constant PREVIEW => "Preview"; use constant POST_COMMENT => "Post comment"; @@ -16,18 +17,96 @@ use constant CANCEL => "Cancel"; 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 => "sessioncgi", id => 'comment', call => \&sessioncgi); hook(type => "htmlize", id => "_comment", call => \&htmlize); hook(type => "pagetemplate", id => "comments", call => \&pagetemplate); hook(type => "cgi", id => "comments", call => \&linkcgi); - IkiWiki::loadplugin("inline"); IkiWiki::loadplugin("mdwn"); + IkiWiki::loadplugin("inline"); } # }}} sub htmlize { # {{{ - eval q{use IkiWiki::Plugin::mdwn}; - error($@) if ($@); - return IkiWiki::Plugin::mdwn::htmlize(@_) + my %params = @_; + return $params{content}; +} # }}} + +sub preprocess { # {{{ + my %params = @_; + my $page = $params{page}; + + my $format = $params{format}; + if (defined $format && !exists $IkiWiki::hooks{htmlize}{$format}) { + error(sprintf(gettext("unsupported page format %s"), $format)); + } + + my $content = $params{content}; + if (!defined $content) { + error(gettext("comment must have content")); + } + $content =~ s/\\"/"/g; + + $content = IkiWiki::filter($page, $params{destpage}, $content); + + if ($config{comments_allowdirectives}) { + $content = IkiWiki::preprocess($page, $params{destpage}, + $content); + } + + # no need to bother with htmlize if it's just HTML + $content = IkiWiki::htmlize($page, $params{destpage}, $format, + $content) if defined $format; + + IkiWiki::run_hooks(sanitize => sub { + $content = shift->( + page => $page, + destpage => $params{destpage}, + content => $content, + ); + }); + + # override any metadata + + if (defined $params{username}) { + my ($authorurl, $author) = linkuser($params{username}); + $pagestate{$page}{meta}{author} = $author; + $pagestate{$page}{meta}{authorurl} = $authorurl; + } + elsif (defined $params{ip}) { + $pagestate{$page}{meta}{author} = sprintf( + gettext("Anonymous (IP: %s)"), + $params{ip}); + delete $pagestate{$page}{meta}{authorurl}; + } + else { + $pagestate{$page}{meta}{author} = gettext("Anonymous"); + delete $pagestate{$page}{meta}{authorurl}; + } + + if (defined $params{subject}) { + $pagestate{$page}{meta}{title} = $params{subject}; + } + else { + delete $pagestate{$page}{meta}{title}; + } + + my $baseurl = urlto($params{destpage}, undef, 1); + my $anchor = ""; + my $comments_pagename = $config{comments_pagename}; + if ($params{page} =~ m/\/(\Q${comments_pagename}\E\d+)$/) { + $anchor = $1; + } + $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}"; + + eval q{use Date::Parse}; + if (! $@) { + my $time = str2time($params{date}); + $IkiWiki::pagectime{$page} = $time if defined $time; + } + + # FIXME: hard-coded HTML (although it's just to set an ID) + return "