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, $url, $email, $ip);
19 'format:s' => \$format,
20 'username:s' => \$username,
21 'subject:s' => \$subject,
28 my $dir=get_dir($pagefile);
29 my $page=get_page($pagefile);
31 IkiWiki::Plugin::comments::checkconfig();
35 $username ||= get_username();
36 $subject ||= get_subject($page, $dir);
37 $date ||= IkiWiki::Plugin::comments::commentdate();
43 die "must supply username" unless defined $username;
44 $subject ||= get_subject($page, $dir);
45 die "must supply date" unless defined $date;
49 chomp($content = join('', <STDIN>));
52 my $comment=get_comment($format, $username, $subject, $date, $url, $email, $ip, $content);
54 # For interactive use, this will yield a hash of the comment before
55 # it's edited, but that's ok; the date provides sufficient entropy
56 # to avoid collisions, and the hash of a comment does not need to
57 # match its actual content.
58 # Doing it this way avoids needing to move the file to a final
59 # location after it's edited.
60 my $location=IkiWiki::Plugin::comments::unique_comment_location($page, $comment, $dir)."._comment";
62 IkiWiki::writefile($location, $dir, $comment);
63 exec_editor("$dir/$location") if $interactive;
68 my $dir=IkiWiki::dirname($file);
69 $dir="." unless length $dir;
75 my $page=IkiWiki::basename($file);
76 $page=~s/\.[^.]+$// unless -d $file;
81 my $username = getpwuid($<);
82 $username="" unless defined $username;
87 my ($page, $dir) = @_;
88 my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir);
89 return "comment $comment_num";
93 my ($format, $username, $subject, $date, $url, $email, $ip, $content) = @_;
94 $format = defined $format ? $format = " format=$format" : q{};
95 $content = '' unless defined $content;
96 my $comment="[[!comment$format\n";
97 $comment.=" username=\"$username\"\n";
98 $comment.=" subject=\"\"\"$subject\"\"\"\n";
99 $comment.=" date=\"$date\"\n";
100 $comment.=" url=\"$url\"\n" if defined $url;
101 $comment.=" email=\"$email\"\n" if defined $email;
102 $comment.=" ip=\"$ip\"\n" if defined $ip;
103 $comment.=" content=\"\"\"\n$content\n\"\"\"]]\n";
111 if (-x "/usr/bin/editor") {
112 @editor="/usr/bin/editor";
114 if (exists $ENV{EDITOR}) {
115 @editor=split(' ', $ENV{EDITOR});
117 if (exists $ENV{VISUAL}) {
118 @editor=split(' ', $ENV{VISUAL});
120 exec(@editor, $file);