4 use lib '.'; # For use in nonstandard directory, munged by Makefile.
6 use IkiWiki::Plugin::comments;
10 die gettext("usage: ikiwiki-comment pagefile [options]") . "\n";
14 my $pagefile=shift || usage();
15 my $interactive = -t STDIN;
17 my ($format, $username, $subject, $date);
19 'format:s' => \$format,
20 'username:s' => \$username,
21 'subject:s' => \$subject,
25 my $dir=get_dir($pagefile);
26 my $page=get_page($pagefile);
28 IkiWiki::Plugin::comments::checkconfig();
32 $username ||= get_username();
33 $subject ||= get_subject($page, $dir);
34 $date ||= IkiWiki::Plugin::comments::commentdate();
37 die "must supply username" unless defined $username;
38 $subject ||= get_subject($page, $dir);
39 die "must supply date" unless defined $date;
40 chomp($content = join('', <STDIN>));
43 my $comment=get_comment($format, $username, $subject, $date, $content);
45 # For interactive use, this will yield a hash of the comment before
46 # it's edited, but that's ok; the date provides sufficient entropy
47 # to avoid collisions, and the hash of a comment does not need to
48 # match its actual content.
49 # Doing it this way avoids needing to move the file to a final
50 # location after it's edited.
51 my $location=IkiWiki::Plugin::comments::unique_comment_location($page, $comment, $dir)."._comment";
53 IkiWiki::writefile($location, $dir, $comment);
54 exec_editor("$dir/$location") if $interactive;
59 my $dir=IkiWiki::dirname($file);
60 $dir="." unless length $dir;
66 my $page=IkiWiki::basename($file);
67 $page=~s/\.[^.]+$// unless -d $file;
72 my $username = getpwuid($<);
73 $username="" unless defined $username;
78 my ($page, $dir) = @_;
79 my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir);
80 return "comment $comment_num";
84 my ($format, $username, $subject, $date, $content) = @_;
85 $format = defined $format ? $format = " format=$format" : q{};
86 $content = '' unless defined $content;
87 my $comment="[[!comment$format\n";
88 $comment.=" username=\"$username\"\n";
89 $comment.=" subject=\"\"\"$subject\"\"\"\n";
90 $comment.=" date=\"$date\"\n";
91 $comment.=" content=\"\"\"\n$content\n\"\"\"]]\n";
99 if (-x "/usr/bin/editor") {
100 @editor="/usr/bin/editor";
102 if (exists $ENV{EDITOR}) {
103 @editor=split(' ', $ENV{EDITOR});
105 if (exists $ENV{VISUAL}) {
106 @editor=split(' ', $ENV{VISUAL});
108 exec(@editor, $file);