+ # everything below this point is only relevant to the comments
+ # themselves
+ if (!exists $commentstate{$page}) {
+ return;
+ }
+
+ if ($template->query(name => 'commentid')) {
+ $template->param(commentid => page_to_id($page));
+ }
+
+ if ($template->query(name => 'commentuser')) {
+ $template->param(commentuser =>
+ $commentstate{$page}{commentuser});
+ }
+
+ if ($template->query(name => 'commentopenid')) {
+ $template->param(commentopenid =>
+ $commentstate{$page}{commentopenid});
+ }
+
+ if ($template->query(name => 'commentip')) {
+ $template->param(commentip =>
+ $commentstate{$page}{commentip});
+ }
+
+ if ($template->query(name => 'commentauthor')) {
+ $template->param(commentauthor =>
+ $commentstate{$page}{commentauthor});
+ }
+
+ if ($template->query(name => 'commentauthorurl')) {
+ $template->param(commentauthorurl =>
+ $commentstate{$page}{commentauthorurl});
+ }
+
+ if ($template->query(name => 'removeurl') &&
+ IkiWiki::Plugin::remove->can("check_canremove") &&
+ length $config{cgiurl}) {
+ $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
+ page => $page));
+ $template->param(have_actions => 1);
+ }
+}
+
+sub addcommenturl ($) {
+ my $page=shift;
+
+ return IkiWiki::cgiurl(do => 'comment', page => $page);
+}
+
+sub num_comments ($$) {
+ my $page=shift;
+ my $dir=shift;
+
+ my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
+ return @comments;
+}
+
+sub unique_comment_location ($$$) {
+ my $page=shift;
+
+ eval q{use Digest::MD5 'md5_hex'};
+ error($@) if $@;
+ my $content_md5=md5_hex(Encode::encode_utf8(shift));
+
+ my $dir=shift;
+
+ my $location;
+ my $i = num_comments($page, $dir);
+ do {
+ $i++;
+ $location = "$page/$config{comments_pagename}${i}_${content_md5}";
+ } while (-e "$dir/$location._comment");
+
+ return $location;
+}
+
+sub page_to_id ($) {
+ # Converts a comment page name into a unique, legal html id
+ # attribute value, that can be used as an anchor to link to the
+ # comment.
+ my $page=shift;
+
+ eval q{use Digest::MD5 'md5_hex'};
+ error($@) if $@;
+
+ return "comment-".md5_hex(Encode::encode_utf8(($page)));
+}
+