2014-10-20 18:08:07 +02:00
|
|
|
#!/usr/bin/perl
|
2016-07-28 10:50:09 +02:00
|
|
|
no lib '.';
|
2014-10-20 18:08:07 +02:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
2016-07-28 10:50:09 +02:00
|
|
|
use FindBin; use lib $FindBin::Bin; # For use in nonstandard directory, munged by Makefile.
|
2014-10-20 18:08:07 +02:00
|
|
|
use IkiWiki;
|
|
|
|
use IkiWiki::Plugin::comments;
|
2014-12-28 04:27:12 +01:00
|
|
|
use Getopt::Long;
|
2014-10-20 18:08:07 +02:00
|
|
|
|
2014-12-28 04:27:12 +01:00
|
|
|
sub usage {
|
|
|
|
die gettext("usage: ikiwiki-comment pagefile [options]") . "\n";
|
2014-10-20 18:08:07 +02:00
|
|
|
}
|
|
|
|
|
2014-12-28 01:27:55 +01:00
|
|
|
sub main {
|
2014-12-28 04:27:12 +01:00
|
|
|
my $pagefile=shift || usage();
|
|
|
|
my $interactive = -t STDIN;
|
|
|
|
my $content;
|
2014-12-28 16:02:26 +01:00
|
|
|
my ($format, $username, $subject, $date, $url, $email, $ip);
|
2014-12-28 04:27:12 +01:00
|
|
|
GetOptions(
|
|
|
|
'format:s' => \$format,
|
|
|
|
'username:s' => \$username,
|
|
|
|
'subject:s' => \$subject,
|
|
|
|
'date:s' => \$date,
|
2014-12-28 16:02:26 +01:00
|
|
|
'url:s' => \$url,
|
|
|
|
'email:s' => \$email,
|
|
|
|
'ip:s' => \$ip,
|
2014-12-28 04:27:12 +01:00
|
|
|
) || usage();
|
|
|
|
|
2014-12-28 01:27:55 +01:00
|
|
|
my $dir=get_dir($pagefile);
|
|
|
|
my $page=get_page($pagefile);
|
2014-10-20 18:08:07 +02:00
|
|
|
|
2014-12-28 01:27:55 +01:00
|
|
|
IkiWiki::Plugin::comments::checkconfig();
|
2014-10-20 18:08:07 +02:00
|
|
|
|
2014-12-28 04:27:12 +01:00
|
|
|
if ($interactive) {
|
|
|
|
$format ||= 'mdwn';
|
|
|
|
$username ||= get_username();
|
|
|
|
$subject ||= get_subject($page, $dir);
|
2014-12-28 04:45:27 +01:00
|
|
|
$date ||= IkiWiki::Plugin::comments::commentdate();
|
2014-12-28 16:02:26 +01:00
|
|
|
$url ||= undef;
|
|
|
|
$email ||= undef;
|
|
|
|
$ip ||= undef;
|
2014-12-28 04:27:12 +01:00
|
|
|
} else {
|
|
|
|
$format ||= undef;
|
|
|
|
die "must supply username" unless defined $username;
|
|
|
|
$subject ||= get_subject($page, $dir);
|
|
|
|
die "must supply date" unless defined $date;
|
2014-12-28 16:02:26 +01:00
|
|
|
$url ||= undef;
|
|
|
|
$email ||= undef;
|
|
|
|
$ip ||= undef;
|
2014-12-28 04:27:12 +01:00
|
|
|
chomp($content = join('', <STDIN>));
|
|
|
|
}
|
2014-10-20 18:08:07 +02:00
|
|
|
|
2014-12-28 16:02:26 +01:00
|
|
|
my $comment=get_comment($format, $username, $subject, $date, $url, $email, $ip, $content);
|
2014-10-20 18:08:07 +02:00
|
|
|
|
2014-12-28 04:27:12 +01:00
|
|
|
# For interactive use, this will yield a hash of the comment before
|
|
|
|
# it's edited, but that's ok; the date provides sufficient entropy
|
|
|
|
# to avoid collisions, and the hash of a comment does not need to
|
|
|
|
# match its actual content.
|
2014-12-28 01:27:55 +01:00
|
|
|
# Doing it this way avoids needing to move the file to a final
|
|
|
|
# location after it's edited.
|
|
|
|
my $location=IkiWiki::Plugin::comments::unique_comment_location($page, $comment, $dir)."._comment";
|
2014-10-20 18:08:07 +02:00
|
|
|
|
2014-12-28 01:27:55 +01:00
|
|
|
IkiWiki::writefile($location, $dir, $comment);
|
2014-12-28 04:27:12 +01:00
|
|
|
exec_editor("$dir/$location") if $interactive;
|
2014-12-28 01:27:55 +01:00
|
|
|
}
|
2014-10-20 18:08:07 +02:00
|
|
|
|
2014-12-28 01:27:55 +01:00
|
|
|
sub get_dir {
|
|
|
|
my ($file) = @_;
|
|
|
|
my $dir=IkiWiki::dirname($file);
|
|
|
|
$dir="." unless length $dir;
|
|
|
|
return $dir;
|
|
|
|
}
|
2014-10-20 18:08:07 +02:00
|
|
|
|
2014-12-28 01:27:55 +01:00
|
|
|
sub get_page {
|
|
|
|
my ($file) = @_;
|
|
|
|
my $page=IkiWiki::basename($file);
|
|
|
|
$page=~s/\.[^.]+$// unless -d $file;
|
|
|
|
return $page;
|
2014-10-20 18:08:07 +02:00
|
|
|
}
|
2014-12-28 01:27:55 +01:00
|
|
|
|
|
|
|
sub get_username {
|
|
|
|
my $username = getpwuid($<);
|
|
|
|
$username="" unless defined $username;
|
|
|
|
return $username;
|
2014-10-20 18:08:07 +02:00
|
|
|
}
|
2014-12-28 01:27:55 +01:00
|
|
|
|
2014-12-28 04:27:12 +01:00
|
|
|
sub get_subject {
|
|
|
|
my ($page, $dir) = @_;
|
|
|
|
my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir);
|
|
|
|
return "comment $comment_num";
|
|
|
|
}
|
|
|
|
|
2014-12-28 01:27:55 +01:00
|
|
|
sub get_comment {
|
2014-12-28 16:02:26 +01:00
|
|
|
my ($format, $username, $subject, $date, $url, $email, $ip, $content) = @_;
|
2014-12-28 04:27:12 +01:00
|
|
|
$format = defined $format ? $format = " format=$format" : q{};
|
2014-12-28 01:27:55 +01:00
|
|
|
$content = '' unless defined $content;
|
2014-12-28 04:27:12 +01:00
|
|
|
my $comment="[[!comment$format\n";
|
2014-12-28 01:27:55 +01:00
|
|
|
$comment.=" username=\"$username\"\n";
|
2014-12-28 04:27:12 +01:00
|
|
|
$comment.=" subject=\"\"\"$subject\"\"\"\n";
|
|
|
|
$comment.=" date=\"$date\"\n";
|
2014-12-28 16:02:26 +01:00
|
|
|
$comment.=" url=\"$url\"\n" if defined $url;
|
|
|
|
$comment.=" email=\"$email\"\n" if defined $email;
|
|
|
|
$comment.=" ip=\"$ip\"\n" if defined $ip;
|
2014-12-28 01:27:55 +01:00
|
|
|
$comment.=" content=\"\"\"\n$content\n\"\"\"]]\n";
|
|
|
|
return $comment;
|
2014-10-20 18:08:07 +02:00
|
|
|
}
|
2014-12-28 01:27:55 +01:00
|
|
|
|
|
|
|
sub exec_editor {
|
|
|
|
my ($file) = @_;
|
|
|
|
|
|
|
|
my @editor="vi";
|
|
|
|
if (-x "/usr/bin/editor") {
|
|
|
|
@editor="/usr/bin/editor";
|
|
|
|
}
|
|
|
|
if (exists $ENV{EDITOR}) {
|
|
|
|
@editor=split(' ', $ENV{EDITOR});
|
|
|
|
}
|
|
|
|
if (exists $ENV{VISUAL}) {
|
2016-02-24 23:46:11 +01:00
|
|
|
@editor=split(' ', $ENV{VISUAL});
|
2014-12-28 01:27:55 +01:00
|
|
|
}
|
|
|
|
exec(@editor, $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
main(@ARGV);
|