X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/47954c09ee9c200548824371a896da8bbda0443b..fd468ef9476a1d215247c1ccd0fda6747cdf0d27:/ikiwiki-comment.in?ds=sidebyside diff --git a/ikiwiki-comment.in b/ikiwiki-comment.in index 8f3db8988..174647b06 100755 --- a/ikiwiki-comment.in +++ b/ikiwiki-comment.in @@ -1,35 +1,67 @@ #!/usr/bin/perl +no lib '.'; use warnings; use strict; -use lib '.'; # For use in nonstandard directory, munged by Makefile. +use FindBin; use lib $FindBin::Bin; # For use in nonstandard directory, munged by Makefile. use IkiWiki; use IkiWiki::Plugin::comments; +use Getopt::Long; -sub usage () { - die gettext("usage: ikiwiki-comment pagefile"), "\n"; +sub usage { + die gettext("usage: ikiwiki-comment pagefile [options]") . "\n"; } sub main { - my $pagefile=shift || usage (); + my $pagefile=shift || usage(); + my $interactive = -t STDIN; + my $content; + my ($format, $username, $subject, $date, $url, $email, $ip); + GetOptions( + 'format:s' => \$format, + 'username:s' => \$username, + 'subject:s' => \$subject, + 'date:s' => \$date, + 'url:s' => \$url, + 'email:s' => \$email, + 'ip:s' => \$ip, + ) || usage(); + my $dir=get_dir($pagefile); my $page=get_page($pagefile); IkiWiki::Plugin::comments::checkconfig(); - my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir); - chomp(my $content = join('', )) unless -t STDIN; + if ($interactive) { + $format ||= 'mdwn'; + $username ||= get_username(); + $subject ||= get_subject($page, $dir); + $date ||= IkiWiki::Plugin::comments::commentdate(); + $url ||= undef; + $email ||= undef; + $ip ||= undef; + } else { + $format ||= undef; + die "must supply username" unless defined $username; + $subject ||= get_subject($page, $dir); + die "must supply date" unless defined $date; + $url ||= undef; + $email ||= undef; + $ip ||= undef; + chomp($content = join('', )); + } - my $comment=get_comment(get_username(), $comment_num, $content); + my $comment=get_comment($format, $username, $subject, $date, $url, $email, $ip, $content); - # 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. + # 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. # 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"; IkiWiki::writefile($location, $dir, $comment); - exec_editor("$dir/$location") if -t STDIN; + exec_editor("$dir/$location") if $interactive; } sub get_dir { @@ -52,13 +84,23 @@ sub get_username { return $username; } +sub get_subject { + my ($page, $dir) = @_; + my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir); + return "comment $comment_num"; +} + sub get_comment { - my ($username, $comment_num, $content) = @_; + my ($format, $username, $subject, $date, $url, $email, $ip, $content) = @_; + $format = defined $format ? $format = " format=$format" : q{}; $content = '' unless defined $content; - my $comment="[[!comment format=mdwn\n"; + my $comment="[[!comment$format\n"; $comment.=" username=\"$username\"\n"; - $comment.=" subject=\"\"\"comment $comment_num\"\"\"\n"; - $comment.=" " . IkiWiki::Plugin::comments::commentdate() . "\n"; + $comment.=" subject=\"\"\"$subject\"\"\"\n"; + $comment.=" date=\"$date\"\n"; + $comment.=" url=\"$url\"\n" if defined $url; + $comment.=" email=\"$email\"\n" if defined $email; + $comment.=" ip=\"$ip\"\n" if defined $ip; $comment.=" content=\"\"\"\n$content\n\"\"\"]]\n"; return $comment; } @@ -74,7 +116,7 @@ sub exec_editor { @editor=split(' ', $ENV{EDITOR}); } if (exists $ENV{VISUAL}) { - @editor=split(' ', $ENV{VISUAL}); + @editor=split(' ', $ENV{VISUAL}); } exec(@editor, $file); }